Reputation:
I got the error:
error connecting: 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.
It comes whenever I stop the IIS after a long time waiting for response in browser. If I am trying to wait then I got the error that An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
but the error I got who are written very old. I just write a new command who not got any exception. [I think the first run error got error first so all new error never can give exception].
So what I need to do to solve this issue.
Upvotes: 14
Views: 26919
Reputation: 238078
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;...");
con.Open();
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:
con.Close();
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;..."))
{
con.Open();
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.
Upvotes: 32