Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does DbConnection.Open always open a new database connection or can it reuse one of the connection pool?

I'm unsure at which level the connection pool is implemented in .NET. When I call

using(var connection = new SqlConnection(connectionString))
{
    connection.Open();

Am I surely opening a new connection? Or could I possibly be reusing an active connection? The connection pool present in SqlConnection can be absent in other DbConnection implementations?

like image 676
Jader Dias Avatar asked Jan 17 '23 13:01

Jader Dias


1 Answers

Connection pooling happens automatically, unless you specify otherwise. If you scroll down to the section "Controlling Connection Pooling with Connection String Keywords" in the first link below, you'll see that the default for "pooling" is true.

http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx

Connection pools get created without any intervention by you, as long as the connection string is exactly the same (uppercase/lowercase matters in this point.)

The same can be said for the OleDbConnection and Connection Pooling.
http://msdn.microsoft.com/en-us/library/ms254502.aspx

like image 126
David Avatar answered Jan 31 '23 00:01

David