开发者

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.Op开发者_开发技巧enConnection();
    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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜