From my understanding, .Net will pool SqlConnection
objects where the connection string is the same by default. Will the connection still be pooled if I call the Dispose method?
This question is asked under the context of an ASP.NET application that at times makes many hits to the database in a single PageLoad
event. I want the connections to be pooled, but would like confirmation that Closing and Disposing of the connection once the data operation is complete does not interfere with .NET's handling of the connection pool.
No, the SQL provider will pool the underlying connections to SQL Server (with separate pools based on the connection string).
The
SqlConnection
objects will gain a pooled connection (or create a new one, etc, based on what's configured for pooling, whether a pooled connection is available) whenOpen
is called, and will release the connection whenClose
orDispose
is called.The two concepts (
SqlConnection
objects, and actual connections to SQL Server) are distinct, but obviously somewhat related.When using connection pooling, closing a
SqlConnection
simply tells the connection pool that you are done with it. The pool will then decide whether or not to actually close the connection, or to reuse it.From the MSDN docs:
The MSDN documentation on SQL Server Connection Pooling says
and
Also, it is the connection pooler that will determine if the connections are dropped from the pool