I've had a suspicion that a database connection used in one of our applications is not always closed. I went to see the code and I've found a class DataProvider that has SqlConnection object. The connection is opened in the constructor of this class and closed in it's Dispose method (don't judge that, i know keeping an open connection is evil, it's just not my code and it's not the point of the question anyway). Dispose method is implemented like this:
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if (_conn != null)
_conn.Close();
}
_disposed = true;
}
}
The question is: does it always guarantee that the connection is closed? And is this code right? I think there should be _conn.Dispose() called - am I right and could it affect not closing the connection (probably not)?