I don't get what is the syntax difference between regular connection and connection pool.
When I'm using the using
key such as:
using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); command.ExecuteNonQuery(); }
Is this the way to perform a connection pool?
Connection pooling allows you to reuse connections rather than create a new one every time the ADO.NET data provider needs to establish a connection to the underlying database. Connection pooling behavior can be controlled by using connection string options (see the documentation for your data provider).
Context pooling works by reusing the same context instance across requests; this means that it's effectively registered as a Singleton, and the same instance is reused across multiple requests (or DI scopes).
Using connection pools helps to both alleviate connection management overhead and decrease development tasks for data access. Each time an application attempts to access a backend store (such as a database), it requires resources to create, maintain, and release a connection to that datastore.
You reuse a prior database connection, in a new context to avoid the cost of setting up a new database connection for each request. The primary reason to avoid using database connections is that you're application's approach to solving problems isn't structured to accommodate a database connection pool.
You can read about connection pooling here.
Basically, as long as the connection string is the same (including case), connections will be taken from the same connection pool.
You do not control the connection pool with connections but with the connection string. Most ADO providers use pooling per default.
The using
statement is used to call the Dispose
method of the object (in this case the connection class). By doing so, the connection is either returned to the pool or being disconnected depending of the connection string configuration.
You should also be aware of that connections are not returned to the pool directly if distributed transactions are being used (TransactionScope
in .Net 4). The connections are returned when the transaction have been completed/rolled back.
If you are not using using
, you should make sure that you call Connection.Close()
as soon as possible. Especially if your application is under some form of load.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With