1
votes

I'm observing following errors while connecting SQL Server from c# application whenever application pool get recycled. I've checked when this errors received there was only 20 connection open in the database despite actual limit is 200(that I set in web.config).

Also, connections are properly closed in code so that would not be an issue. Please note that it doesn't occur every time when pool recycled but happens during a day when we have traffic on our system.

System.InvalidOperationException: 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. at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.SqlClient.SqlConnection.Open()

2

2 Answers

0
votes

Every time you Open the Connection, make sure that you have to close it.

Max pool size was reached

0
votes

Try this:

    SqlConnection cn = new SqlConnection(strCn);
    try
    {
        using (SqlCommand cmd = new SqlCommand("select * from xxxx", cn))
        {
            cn.Open();

            //do something

            cn.Close();
        }
    }
    catch (Exception exception)
    {
        cn.Close();
        throw exception;
    }