For years, I've experienced very weird problems on all my web applications that connect to a SQL server.
The problem is that if something happens to the database server (server restart or other problem), de web app stops working from that point on, even if the database server is alive and well afterwards.
What happens is that every ADO.NET operation (ExecuteNonQuery, CreateReader, BeginTransaction, ...) fails with a InvalidOperationException: "Invalid operation. The connection is closed". It seems that a call to SqlConnection.Open() retrieves a connection from the application pool which is... closed!
According to the documentation, the connection pool should automatically remove severed connections from the connection pool, but apparantly a closed connection isn't regarded as "severed", so the call to SqlConnection.Open() happily returns a closed connection, assuming it is open, without checking this.
My current workaround is to check for the state of the connection right after opening it:
using (SqlConnection connection = new SqlConnection( connectionString ))
{
connection.Open();
if (connection.State != ConnectionState.Open)
{
SqlConnection.ClearAllPools();
connection.Open();
}
// ...
}
This workaround seems to work for now, but I don't feel comfortable doing this.
So my questions are:
- Why does SqlConnection.Open() return closed connections from the connection pool?
- Is my workaround valid?
- Is there a better way to handle this?
I did some similar research into connection pooling a while ago, for a slightly different reason, but hopefully will be of some use. What I found is:
Connections are automatically removed from the pool, my findings were that this typically occurred within a few minutes after it was last used. So, it may be a timing issue - and they are being cleared up, but not before the connections are attempted to be reused again.
These were some articles I looked at, at the time:
Sql Server Google Group
Using Connection Pooling in ASP.NET
Edit:
It does sound odd that the bad connection stays in the pool forever - are you sure it definitely does, and it's not just multiple bad connections? If you are sure then it sounds like those connections aren't being released properly within your code. This is another very good article I read a while ago, that says (quote):
We've seen the same problem from C++ using ADO. A few years ago, after working with Microsoft Support, we also implemented similar retry logic in the code and reset the connection pool which resolved the problem.
If there is a better workaround the folks at Microsoft Support either didn't know it, or weren't sharing (At that time anyways).