Search This Blog

Friday, March 12, 2010

Understanding Connection Pooling in .NET

Connection Pooling can increase the database performance to a huge extent. This article discusses what Connection Pooling actually is, how it can boost the performance of database access and how we can create a Connection Pool in .NET and add and remove connections to it.

Connecting to the database is resource intensive and a relatively slow operation in an application but the most crucial of them all. A Connection Pool is a container of open and reusable connections. A Connection Pool is released from the memory when
the last connection to the database is closed. The basic advantage of using Connection Pooling is an improvement of performance and scalability while the main disadvantage is that one or more database connections, even if they are currently not being used, are kept open. The Data Providers in ADO.NET have Connection Pooling turned on by default; if you need to turn it off, specify Pooling = false in the connection string being used. Connection Pooling gives you an idle, open, reusable connection instead of opening a new one every time a connection request to the database is made. When the connection is closed or disposed, it is returned to the pool and remains idle until a request for a new connection comes in. If we use Connection Pooling efficiently, opening and closing of connections to the database becomes less resource expensive. This article discusses what Connection Pooling is all about and how Connection Pooling can be used efficiently to boost the performance and scalability of applications.

How does a Connection Pool work?
Connection pools are actually containers that contain open and reusable connections. Multiple pools can exist in the same application domain at the same point in time, but Connection Pools cannot be shared across application domains. Note that one pool is created per unique connection string. A Connection Pool is created the first time a request for a connection to the database comes in with a unique connection string. Note that if another request comes in with a different connection string for the database, another Connection Pool would be created. Hence, we have one Connection Pool per connection string and not per database. 

The following code listings below illustrate this.
Listing 1
// A new pool is created.
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = 
"Server=localhost;Database=test;User ID=joydip;Password=joydip;Trusted_Connection=False";
Listing 2
// A new pool is created as the connection strings differ.
SqlConnection conn = new SqlConnection();
sqlConnection.ConnectionString = 
"Server=localhost;Database=test;User ID=test;Password=test;Trusted_Connection=False";
Listing 3
// The connection string is the same as in Listing 1 so no new pool is created.
SqlConnection conn = new SqlConnection();
sqlConnection.ConnectionString = 
"Server=localhost;Database=test;User ID=joydip;Password=joydip;Trusted_Connection=False";
When a request for a new connection comes in the connection is served from the Connection Pool without creating a new one, i.e., the connections are re-used without creating new ones.  Therefore, it improves the performance and scalability of your applications. When your application closes an open connection, it is returned to the pool where it waits until a reconnect time out period expires. This is the period within which it waits to connect to the same database using the same credentials. If none comes in within this period, the connection to the database is closed and the connection instance is removed from the pool.
When a new Connection Pool is created, the connection objects are placed in the pool and the pool and the connections contained in it are made available for immediate use. The pool can house connections up to the maximum limit as specified in the connection string that was used to connect to the database. Connections are removed from the pool when they remain inactive for a long period of time or have outlived its specified life time or server connectivity.
A Connection Pool is maintained internally by the Connection Pool Manager. When a request for a subsequent connection comes in, the Connection Pool Manager searches the pool for the availability of a free connection and returns it to the application if one is available. The following points elaborate how the Connection Pool Manager works- the operations that it performs when a request for a new connection comes to it.
·         If any unused connection is available, it returns one.
·         If all connections are used up, a new connection is created and added to the pool.
·         If the number of connections reaches the maximum number of connections in the pool, the requests are queued until a connection becomes free for reuse.
Connection Pooling is controlled and the parameters passed to a connection string that basically comprises the following:
·         Connect Timeout
·         Min Pool Size
·         Max Pool Size
·         Pooling
In order to use Connection Pooling efficiently, remember to close the connections once you are done with it so that it returns to the pool.

Improving Connection Pooling Performance
We should always open the connections late and release them early; in other words, immediately after we are done using it. Connections should be opened only at the time when they are actually required.  The connections should not be acquired prior to its usage as it would decrease the number of available connections in the Connection Pool and, hence, have detrimental effects to the operation of the Connection Pool and the application's performance. The connections should be explicitly released immediately when we are done using it. This would facilitate better Connection Pooling as the connection would be returned to the pool and be available for reuse. The following snippet of code shows how we should open and close connections in our applications efficiently.
Listing 4
SqlConnection sqlConnection = new SqlConnection(connectionString);
  //Some Code
The code in Listing 4 can be simplified further by using the "using" keyword as shown in the code snippet below.
Listing 5
using(SqlConnection sqlConnection = new SqlConnection(connectionString))
  //Some Code
Note that using the "using" keyword as shown in Listing V above, generates the try-finally block implicitly.
The following points illustrate the measures that can be taken for better utilization of the Connection Pool.
·         Always open connections when needed and close it immediately when you are done using it.
·         Close the user-defined transactions before closing the related connections.
·         Ensure that there is at least one connection open in the pool to maintain the Connection Pool.
·         Avoid using connection pooling if integrated security is being used.
Connection Pooling can be monitored in one of the following ways:
·         Executing the sp_who or sp_who2 stored procedures
·         Using the SQL Server Profiler
·         Using the Performance Monitor and Performance Counters

A Connection Pool, a container of connection objects, remains active as long as there are active or open connections in it. When a request for new connection comes in with a connection string, a new Connection Pool is allocated. We can improve the performance and scalability of our applications by using the same connection string in our applications. However, we should use Connection Pooling appropriately as using it inappropriately might have negative effects to the overall performance of our applications. MSDN says, "Connection pooling is a powerful functionality that can improve your applications' performance. But if you aren't a good lifeguard, your connection pools can become a detriment instead of a benefit." This article has discussed Connection Pooling and how we can use it efficiently for improving the performance and scalability of our applications.

No comments:

Post a Comment

Leave your suggession & comment...