Understanding Connection Pooling

For any web application establishing connection of database is most time consuming & slow ( which is always depends upon network speed & memory)

Generally we are using at the most 1-3 connections for an application and we are opening the same connections repeatedly and closing the same ,we can avoid the memory overhead and time for connection establishment by using .Net Supported Connection Pooling. Connection Pooling concept is used irrespective of the number of the user.

For all data providers in ADO.NET by default Connection Pooling is enabled, we can explicitly disable is by adding Pooling = false to Connection String.

Let us understand the basic concept of connection pooling – When application is accessed first time then at that time in memory Container is getting created for maintaining the Opened & used connections.When new user give a call to OPEN connection , it(Connection pool manager) will check if the connection is

pooled (i.e. already opened ) and then if available then it returns the same instead of creating the new.On the other hand when certain call give call to CLOSE connection then instead of closing the connection it will maintain the connection in the Pool ,so that it can able to provide the same to next OPEN call for the same connection.A Connection Pool is released from the memory when the last connection to the database is closed.

SqlConnection sqlConnection = new SqlConnection();

sqlConnection.ConnectionString =

“Data Source=dataSource; Initial Catalog=mydatabase; User ID=userID;   Password=password;Connection Timeout=60; Min Pool Size=5; Max Pool Size=40;”

// A Connection Pool is created when call to Open() is given.

sqlConnection.Open();

As per MSDN,Each connection pool is associated with a distinct connection string. When a new connection is opened, if the connection string is not an exact match to an existing pool, a new pool is created. Connections are pooled per process, per application domain, per connection string and when using integrated security, per Windows identity.Only connections with the same configuration can be pooled. ADO.NET keeps several pools concurrently, one for each configuration. Connections are separated into pools by connection string, as well as by Windows identity when integrated security is used.

We can control the Connection pooling with SQL Server by mainly using following parameters of the ConnectionString ( Create Table) Connect Timeout/Connection Timeout – Default = 15 seconds ,controls the wait period in seconds when a new connection is requested, if this timeout expires, an exception will be thrown.

Parameter Name

Default

Used For

Max Pool Size

100

Specifies the maximum size of your connection pool. Most Web sites do

not use more than 40 connections , but it depends on how long your database operations take to complete.

Min Pool Size

0

Initial number of connections that will be added to the pool upon its creation.

however, you may chose to set this to a small number such as 5 if your application needs consistent response times even after it was idle for hours. In this case the first user requests won’t have to wait for those database                 connections to establish.

Pooling

true

Controls if your connection pooling on or off. When true, the Connection object is taken from pool, or if necessary, is created and added to the appropriate pool. known values are true, false, yes, and no.

Connection Lifetime

0

Value zero means maximum connection timeout.

Connection Reset

true

Determines whether the database connection is reset when being drawn from the pool

Enlist

true

When true, the pool manager automatically enlists the connection in the creation thread’s current transaction context.

Check MSDN for Connection String Parameters for Pooling.

Let is consider One Example

SqlConnection sqlConnection = new SqlConnection();

sqlConnection.ConnectionString =

“Data Source=dataSource; Initial Catalog=mydatabase; User ID=userID; Password=password;Connection Timeout=60; Min Pool Size=5; Max Pool Size=40;”

// First Connection Pool is created when call to Open() is given.

sqlConnection.Open();

SqlConnection sqlConnection1 = new SqlConnection();

sqlConnection1.ConnectionString =

“Data Source=dataSource1; Initial Catalog=mydatabase1; User ID=userID; Password=password;Connection Timeout=60; Min Pool Size=5; Max Pool Size=40;”

// Second Connection Pool is created when call to Open() is given because ConnectionString is different from first ConnectionString

sqlConnection1.Open();

SqlConnection sqlConnection2 = new SqlConnection();

sqlConnection2.ConnectionString =

“Data Source=dataSource; Initial Catalog=mydatabase; User ID=userID; Password=password;Connection Timeout=60; Min Pool Size=5; Max Pool Size=40;”

sqlConnection2.Open();

// sqlConnection2 is assigned with existing Connection Pool A as the same connection string is used.


Take a note of following points,Usefull to get Connection Pool performance effectively

1) Connection Pooling concept mainly depends on the concept of reusable Connection Strings,Connections with same connectionstrings are to kept in a connection pool.So for this reason if we are going use the same connection throughout the code , create it in the Web.config and access it from there ( ref previous blog entry)By using this we can use the same connectionstring without bothering it will be the exact same as previous used, bcoz any change in the connection string causes new connection pool creation.

2) Open the connection only when it is required just before using the connection, because when we open the connection then connection is taken from the previous created pool or new pool is getting created and after opening the connection if we are not using the connection then resources are getting waisted also connections will close only after actual use and leads to the loss of a connection in pool , that might cause creation of new pool or thowing Exception “MAX Pool Size Reached” at time of heavy traffic.

3)Close the connection explicitly by calling Close() after the activity is done or after the tranasction is completed.Closing connection is important beacause when we give call to Close() connection ,connection is being passed/added to connection pool so that other one can use it. Missing the connection close sometimes leads to connection leaks, as number of open connection increased and max pool size obtained.  If you think you are lazy enough to close the connection then open all the connections inside the “using” loop .

using (SqlConnection connection = new SqlConnection(“Data Source=dataSource; Initial Catalog=mydatabase; User ID=userID; Password=password;Connection

Timeout=60; Min Pool Size=5; Max Pool Size=40;”))

{

connection.Open();

}

4)Avoid connection pooling when we are planning to use Basic or Windows Authentication,Connections are pooled according to the connection string plus the user identity.so in the above case we will get one pool per user,which is not available to other users leads to poor performance by leaving large number of connections open while consuming memory.

As per MSDN –

Pool fragmentation is a common problem in many Web applications where the application can create a large number of pools that are not freed until the process exits. This leaves a large number of connections open while consuming memory, resulting in poor performance.

Pool Fragmentation Due to Many Databases

Many Internet service providers host several Web sites on a single server. They may use a single database to confirm a Forms authentication login and then open a connection to a specific database for that user or group of users. The connection to the authentication database gets pooled and used by everyone.However, there is a separate pool of connections to each database, thus increasing the number of connections to the server.

This is also a side effect of the application design. There is a relatively simple way to avoid this side effect, however, without compromising security when connecting to SQL Server. Instead of connecting to a separate database for each user or group, connect to the same database on the server and then execute the Transact-SQL USE statement to change to the desired database. The following code fragment demonstrates creating an initial connection to the master

database and then switching to the desired database specified in the databaseName string variable.

// Assumes that command is a SqlCommand object.

using (SqlConnection connection = new SqlConnection(

“Server=MSSQL1;uid=xxx;pwd=xxx;database=master”))

{

connection.Open();

command.ExecuteNonQuery(“USE ” + databaseName);

}

Detail Ref – MSDN


Advertisements

Resolve ORA-03113: end-of-file on communication channel = Check following settings !!!

At the time of resolving ORA-03113 before considering that threre is an error in our code check for following , you can save your time for debugging code for any leak.

1) Newtork issue – something might be wrong in connection faulty connection , check LAN settings.

2) Sever machine where application hosted is getting restarted or under maintenance or some patch running , this is largly occured reason for this exception . !!!!

3)Problem with Connection Pooling ,in correct handling of more than one connection at time.

4)Check that machines firewall that is ending the connection after certain period of time also check idle time out session out setttings.

5) Go to your sqlnet.ora file and check for the SQLNET.EXPIRE_TIME settings Set Value greater than 0,use parameter SQLNET.EXPIRE_TIME to specify a the time interval which specified in minutes, to send a probe to verify that client/server connections are active. Setting a value greater than 0 ensures that connections are not left open indefinitely, due to an abnormal client termination. If the probe finds a terminated connection, or a connection that is no longer in use, it returns an error, causing the server process to exit. This parameter is primarily intended for the database server, which typically handles multiple connections at any one time.

you can find details at Oracle

6) Check for your Select queries ,if they are produsing output more than your Default Buffer Size then also we can find this error coming increase Buffer Size

Last but not least … if u think u tried all possibilities  Contact Oracle Metalink !!!

There can be many more reasons or checklist available for this error I was just writing which I experienced while using Oracle with  .Net .. I update post gradually after finding new !!!!