views:

784

answers:

3

Hello everyone,

I am using SQL Server 2008 Enterprise + C# + ADO.Net + .Net 3.5. I am using connection pool at ADO.Net client side (the default behavior of ADO.Net). I am using sp_who2 or sys.dm_exec_connections to find active connections numbers (let me know if my method to find active connection numbers are wrong).

If each time after I create and open a new ADO.Net connection object, I will have a matched close statement to close the ADO.Net connection object instance after using the connection object (to execute store procedure), I am wondering in this scenario, the active connection number should always be 0 after I close all connection from my ADO.Net client application, and if show active connection number > 0, it should be very weird?

thanks in advance, George

+5  A: 

Close will free the connection to be available for the connection pool. It doesn't mean the physical connection is closed.

Also make sure the .close is in a finally, so it executes both on the regular flow and when an exception occurs.

eglasius
Do you mean when I am using sp_who2 or sys.dm_exec_connections to find active connections numbers, I am finding physical connections and not connections to ADO.Net connection pool?
George2
"Also make sure the .close is in a finally" -- sure, thanks for reminder. Any ideas to my question above?
George2
Yes. Basically the connection pool is a set of connections that are maintained in the client side, for the server there is no difference.
eglasius
So, you mean even if I close the connection at my C# code, ADO.Net client side connection pool keep connections alive, so when I execute sp_who2 or sys.dm_exec_connections to find active connections numbers, the active connection numbers are still > 0 (occupied by connection pool)?
George2
Thanks, my concern is if each time I create a new connection from connection pool in my ADO.Net client code, if connection pool cannot find an available connection to reuse, connection pool capabilty of connection number will increase (create a new physical connection to the server), is that true? If yes, connection number will increase for ever even if connection pool will be used (my previous understanding is, connection pool will control upper bound of connections to be allocated, so using connection pool will not make physical connection number increase for ever)? Any comments?
George2
There is a MaxPoolSize property that will cap the number of 'real' connections against the database. If you try to create more connections in your c# code you will get an exception.
idstam
@George2 how idstam said, there is a maximum pool size. On the failure scenario, more specifically "If the maximum pool size has been reached and no usable connection is available, the request is queued. The pooler then tries to reclaim any connections until the time-out is reached (the default is 15 seconds). If the pooler cannot satisfy the request before the connection times out, an exception is thrown.". The link I added later on an update to my post explains has more info on it.
eglasius
Thanks idstam and Freddy, the last question, I am using sp_who2 or sys.dm_exec_connections to find active connections numbers, is that the correct method?
George2
+2  A: 

Make sure that you properly close or dispose the SqlConnection. Disposing a connection will automatically close it.

using(SqlConnection connection = new SqlConnection(connectionString))
{
} //The connection will close automatically here

But you can be explicit and still do a .Close() before the end of the using.

You can also clear all pending connection that have the status Awaiting Command by calling

SqlConnection.ClearAllPools();
Pierre-Alain Vigeant
"If you have no control on the connection opening and closing" -- what do you mean no control? I write my code to create/open connection object instance, and I can close it when no need.
George2
Well you could have been using a third-party dll that poorly handle connections.
Pierre-Alain Vigeant
Thanks, my concern is if each time I create a new connection from connection pool in my ADO.Net client code, if connection pool cannot find an available connection to reuse, connection pool capabilty of connection number will increase (create a new physical connection to the server), is that true? If yes, connection number will increase for ever even if connection pool will be used (my previous understanding is, connection pool will control upper bound of connections to be allocated, so using connection pool will not make physical connection number increase for ever)? Any comments?
George2
"using a third-party dll that poorly handle connections" -- if I can get the SqlConnection object instance, I can close it, correct (even if for 3rd party DLL)? Why do you think I have no control? :-)
George2
Look, I'm not gonna argue on the wording. I am not insulting you, I was using an introduction to `ClearAllPools`. I'll change the answer.
Pierre-Alain Vigeant
1. ClearAllPool means when we call Close method, the physical connection of the connection pool to SQL Server is reclaimed? 2. If yes, what is the benefit of using ClearAllPools with Conneciton pool, keeping connection open is a basic principle of optimization using connection pool? If connection is closed, no performance benefit. Any comments?
George2
+1  A: 

Not so, the ADO.NET connection pool will return your connection to the pool after Close is called. The connection may not really be closed at this point. To force the connection to be closed try clearing the pool before closing. Try this and you should see the connection close from the server.

var conn = new SqlConnection("a server goes here");
        conn.Open();

        SqlConnection.ClearAllPools();
        conn.Close();
Steve
1. ClearAllPool means when we call Close method, the physical connection of the connection pool to SQL Server is reclaimed? 2. If yes, what is the benefit of using ClearAllPools with Conneciton pool, keeping connection open is a basic principle of optimization using connection pool? If connection is closed, no performance benefit. Any comments?
George2
1. Yes, calling Close will not return the connection to the pool.2. I use ClearAllPools then Close with Sql Server Express. I will attach and detach databases from my program. In order to copy a database file, it has to be detached. I use ClearAllPools with Close to release the connection so that I can detach and copy the MDF files. The connection pool will provide performance only clear it if you have to for some reason.
Steve
Great reply! I have marked your reply as answered.
George2