Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net connection pooling

Tags:

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?

like image 406
Erez Avatar asked Mar 09 '11 10:03

Erez


People also ask

What is .NET pooling?

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).

What is connection pooling in Entity Framework?

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).

What is connection pooling used for?

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.

When should you not use connection pooling?

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.


2 Answers

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.

like image 98
Øyvind Bråthen Avatar answered Oct 07 '22 14:10

Øyvind Bråthen


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.

like image 44
jgauffin Avatar answered Oct 07 '22 16:10

jgauffin