SqlConnection.Dispose internals





0/5 (0 vote)
SqlConnection.Dispose internals
Today, I was just looking at the IL of SqlConnection
class, so I found few things which are interesting to me and hopefully to you as well.
SqlConnection.Dispose()
shall callClose()
method internally, so if you’re usingUsing(){}
block to open a connection, then you need notClose
it explicitly.SqlConnection.Dispose()
method also callsSqlConnection.DisposeMe(bool)
method internally, althoughSqlConnection.DisposeMe(bool)
method is empty, but this method isprivate
to this class. The reason I guess they have done this way is for futuristic purposes.
There are quite a few differences I could spot out in the IL with respect to Dipose()
and Close()
methods:
Close()
method is actually anabstract
method inDBConnection
class. It is overridden and madevirtual
inSqlConnection
class, which gives the consumer code more flexibility in customizing theClose
operation if at all needed.- There is no
Dispose()
method inDBConnection
class but instead it's avirtual
method inSqlConnection
class. Close()
method actually only closes the connection object via Native interface with the DB and tries to clean it up. Internally, it usesDBConnectionInternal.CloseConnection
method to actually close the transaction with the DB by using many APIs.- On the other hand,
Dispose()
method will not close any connection explicitly, but callsClose()
method. Along with it, it also callsDisposeMe()
method internally.
Happy coding!
P.S: Your comments/votes are much appreciated.