4 comments

  1. Didn’t know that it does that, I use the using construct a lot when communicating with a database. Might be worthwhile to check if using close instead of the using block increases performance in my apps 🙂

    W.Meints

  2. Wat version of .NET do you refer to? I’ve checked the .NET 2.0 SqlConnection and I can NOT confirm your story. DbConnection does not implement (override) Dispose. base.Dispose calls Component.Dispose and there only site management and event raising takes place.

    Marc Jacobi

  3. Erno,

    This is utter nonsense! When Dispose calls Close() the underlying connection is put back in the pool.

    SqlConnection derives from DbConnection, which does not even override Dispose, but inherits this from System.ComponentModel.Component!

    Also, Dispose is called regardless of whether you call myConnection.Dispose() or whether this is done for you by the Garbage Collector when it calls the destructor method Finalize() on your myConnection object after loses scope.

    The only reason for using Close() is if you want to reopen the connection later. After using Dispose() you should not use the object anymore.

    The advantage of using Dispose() is that *you* can determine when you want to release resources used by your object rather than waiting for the GC to do this. When a class is IDisposable you should ALWAYS call Dispose() or use using(), there is never a reason for not doing this. This is a best practice, which is also why FxCop has a rule to check this.

    Gerard van der Land

  4. Marc, Gerard, you are right, I was wrong.
    Some remarks:
    1. it was not my intend to reuse the managed objects but the unmanaged objects in the connectionpool.
    2. I think this is a change in behaviour from 1.1 to 2.0 -> I’ll investigate.
    Thanks!

    ernow

Comments are closed.