i Dispose an SqlConnection object, but of of course it isn't really closed. i need closed connection to not hold locks on database objects. How can i prevent closed connections from holding locks?
Explanation of the above sentance for those who don't know:
When you close an ADO or ADO.NET connection, you aren't actually severing the connection to SQL Server. The ADO/ADO.NET infrastructure keeps the connection around in case you want to use it again. The connections are kept lingering around in what's called "The Connection Pool".
After a few minutes of not being used, the connection will be actually closed. Although, not really. TCP/IP has it's own method for keeping TCP connections open for a few more minutes (in the "CLOSE_WAIT" state). This is done in case you ask to open a TCP connection to the same IP:Port again. If so, it can use that already open TCP connection.
With connection pooling and SQL Server, the connection is still established to SQL Server. Each connection has a database context that it is sitting in. As long as a connection is sitting in that database: it holds a shared database (S-DB) lock on that database.
A Shared-Database lock simply means, "Don't delete this database while i'm in it please."
How can i prevent it from holding a shared lock on my database, while keeping the benefits of connection pooling?
My ad-hoc solution right now is every time a developer called Dispose:
connection.Dispose()
change it into a call to a global helper function:
Database.DisposeConnection(connection);
which changes the database context to master:
public static void DisposeConnection(SqlConnection connection)
{
//Stop holding a database lock - in my database at least
ADOHelper.ExecuteNonQuery(connection, "USE master");
connection.Dispose();
}
It solves my immediate problem; closed connections aren't holding a lock on my database.
But now i'm worried that connection pooling will have its brain scrambled - because i switched database contexts behind its back.
In case anyone didn't know, or thought otherwise:
From the SDK:
Close and Dispose are functionally equivalent.