简体   繁体   中英

Connection timeout VS IIS

I get this exception in my error log..

The timeout period elapsed prior to obtaining a connection from the pool.

Restarting the IIS makes me to login back to my app.. so where do you think the issue is? Is it in IIS property setting?

Code is something like this:

MySqlConnection connection = null;
try
{
    connection = MySqlConnectionHandler.OpenConnection();
    string strText =string.Empty;
    strText = "<<sql statement>>";
    using (MySqlCommand cmd = new MySqlCommand(strText, connection))
    {
        cmd.Parameters.Add(new MySqlParameter("ID", MySqlDbType.Int32));
        cmd.Parameters["ID"].Value = 100;

        cmd.ExecuteNonQuery();
    }
}
finally
{
    MySqlConnectionHandler.CloseConnection(connection);
}

My CloseConnection method internally has

if (con != null)
{
    con.Close();
    con.Dispose();
}

That almost always means you aren't calling Dispose() on your connections, and you have saturated the pool until GC runs - which it probably won't if your app is now dead. Essentially, you have opened connections successively over a period of time (without ever giving them back), and now there are none left. Solution: give them back to the pool when you are done.

Whenever you use something like a connection, use a using statement, or some other mechanism to ensure it is disposed promptly when you are done with it. The same applies to anything IDisposable , but connections are particularly vulnerable to this.

Since you say you do call Dispose()...

I would try two things. Temporarily increase your connection timeout to see if that's the issue:

MySqlCommand.CommandTimeout = 45; //in seconds

This will slow your application so you should only use it for troubleshooting. If it is you need to optimize your SQL query.

Alternately, your connection pool may be exceeded, so you may need to bump that up.

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