posted 5 years ago
Thankfully not. C# connection pools are among the worst code ever written. Is it a connection? Is it a connection pool? What happens when you dispose of a connection? You really don't know. We've had numerous issues in our persistence layer because we closed connections when we should have left them open, and left them open when we should have closed them.
Java makes this explicit with the DataSource interface. A DataSource is a source of connections that may be pooled or not. When you have an application that needs to access a database, make sure that you configure a DataSource somewhere, and then inject that into the classes that access the database. A database operation consists of getting a connection from the data source, running queries on it and then ALWAYS disposing the connection once you're done. You can easily do this with the try-with-resources syntax. When the connection is disposed, the data source is free to either close the underlying connection, or return it to the pool for later use.
Here is an example: