Saturday, March 24, 2012

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

The error means the connection pool is out of connections. The usual cause is forgetting to close a connection after you use it, like:

var con = new MySqlConnection("Server=YourDB;Database=YourDb;...");
var com = con.CreateCommand();
com.CommandText = "select * from YourTable";
This code forgets to close the connection, so the pool has one less connection. You could fix this by adding:

at the end. But if the query throws an exception, the Close will be skipped, and you're still down 1 connection.

A much better way is the using statement:

using (var con = new MySqlConnection("Server=YourDB;Database=YourDb;..."))
var com = con.CreateCommand();
com.CommandText = "select * from YourTable";
You can troubleshoot this easily by adding max pool size at the end of your connection tring, like:

Server=YourDB;Database=YourDbUid=YourUser;Pwd=YourPwd;max pool size=1;
This gives you a pool size of 1, instantly triggering an error condition if you forget to free up your one connection.

No comments:

Post a Comment