Quantcast
Channel: User shenn - Stack Overflow
Viewing all articles
Browse latest Browse all 38

Having an issue with connection pools staying open (closing connections explicitly)

$
0
0

I have been getting an error on my application that the max pool size was reached:

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

We have an internal function which returns a DataReader. Inside of this function, we are using the ExecuteReader method, which is given the value CommandBehavior.CloseConnection. After reading the data we need, we are calling both .Close and .Dispose on the DataReader which is returned by this function.

On a side note, I also tried implementing a different method in which I did the using tag on my connection and still saw this as "Sleeping" in the database.

I ran the query below to get the connections which are still in sleeping status. At any time we are seeing about 40 or so listed as "sleeping". I grabbed the SQL Script being used here, traced this back to where they were being used in the application and triple checked the DataReaders are being closed.

SELECT sp.spid , sp.hostname , sp.program_name , sp.loginame ,sp.login_time, sp.status , CAST(text AS VARCHAR(1000)) AS SQLScript FROM sys.sysprocesses sp CROSS APPLY sys.dm_exec_sql_text (sp.sql_handle) where program_name='.Net SqlClient Data Provider' and loginame='[LoginNameForSql]' order by SQLScript

Is there anything else I need to do to make sure these connections are being closed? Our team is extremely confused because all of us have double and triple checked that our readers are closed and diposed after using them.

Function to get Data Reader:

        SqlConnection conn = OpenConnection();        SqlDataReader rdr = null;        //    try        //    {        SQL = strSQL;        CommandBehavior intBehavior = CommandBehavior.CloseConnection;        using (var cmd = conn.CreateCommand())        {            cmd.CommandText = strSQL;            cmd.CommandTimeout = Timeout;            rdr = cmd.ExecuteReader(intBehavior);            cmd.Parameters.Clear();        }      return rdr;

using said function:

       var rdr = FunctionReturningDataReader();        if (rdr != null)        {            if (rdr.HasRows)            {                while (rdr.Read())                {                }            }         }            rdr.Close();            rdr.Dispose();

EDIT: I increased the max pool size to 200 and haven't seen this error in our application since 3 days ago.


Viewing all articles
Browse latest Browse all 38

Trending Articles