简体   繁体   中英

SQL Server: Max pool size was reached

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

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

Max pool size was reached

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM