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

ado.netasp.net

I'm getting this error every few days. I won't see the error for a few days then I'll get a flurry of 20 or so all with in a minute or so.

I've been very thorough going throw my code so that I'm using this basic setup for my DB access.

try
{
  myConnection.Open();
  mySqlDataAdapter.Fill(myDataTable);
  myConnection.Close();
}
Catch (Exception err)
{
  if (myConnection.State != ConnectionState.Closed) myConnection.Close();
  throw err;
}

The way I understand it this should execute my queries and immediately release the connection back to the pool but if something goes wrong with the query then I catch the excpetion close my connection then throw the error up, which eventually gets trapped at the application level and logs and emails me the error.

Even using this throughout my code I'm still running across the issue. What can I do to diagnose the root cause of the issue?

Best Answer

The issue is the number of pooled connections you can have in the pool.

In your connection string, you can add the "Max Pool Size=100" attribute to increase the size of your pool. However it sounds like you are concurrently running a significant number of SQL queries, all of which are long running. Perhaps you should look at ways to either shorten the queries or run them sequentially through a single connection.