Do I have to Close() a SQLConnection before it gets disposed?
Asked Answered
G

8

127

Per my other question here about Disposable objects, should we call Close() before the end of a using block?

using (SqlConnection connection = new SqlConnection())
using (SqlCommand command = new SqlCommand())
{
    command.CommandText = "INSERT INTO YourMom (Amount) VALUES (1)";
    command.CommandType = System.Data.CommandType.Text;

    connection.Open();
    command.ExecuteNonQuery();

    // Is this call necessary?
    connection.Close();
}
Gainer answered 28/7, 2009 at 18:21 Comment(0)
P
120

Since you have a using block, the Dispose method of the SQLCommand will be called and it will close the connection:

// System.Data.SqlClient.SqlConnection.Dispose disassemble
protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}
Pd answered 28/7, 2009 at 18:26 Comment(1)
Does this._poolGroup = null; means that the connection is not getting back to the connection pool ? so i'll have n-1 connections ?Act
W
28

Disassembly of SqlConnection from using .NET Reflector:

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }

    this.DisposeMe(disposing);
    base.Dispose(disposing);
}

It calls Close() inside of Dispose()

Weka answered 28/7, 2009 at 18:28 Comment(2)
@statenjason: could you please say that how do you take advantage of using disassemblers line .net reflector?Byington
@Byington just download .NET Reflector, run reflector.exe, and you can open any .net DLL (including the standard library). It provides you with a tree structure similar to Visual Studio's object browser, however, you can right click on any class or method and click "disassemble" it will then return the source to you in C# or VB, whichever you've selected in the options.Weka
T
24

The using keyword will close the connection correctly so the extra call to Close is not required.

From the MSDN article on SQL Server Connection Pooling:

"We strongly recommend that you always close the connection when you are finished using it so that the connection will be returned to the pool. You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside a using statement in C#"

The actual implementation of SqlConnection.Dispose using .NET Reflector is as follows:

// System.Data.SqlClient.SqlConnection.Dispose disassemble
protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}
Tory answered 30/10, 2009 at 17:56 Comment(2)
+1 for MSDN link - I like reflector\ILspy like the next guy, but the docs are where I'd like to go to find my answers.Maltz
In the year 2024 the whole thing is open source. github.com/microsoft/referencesource/blob/master/System.Data/…Parton
S
5

Using Reflector, you can see that the Dispose method of SqlConnection actually does call Close();

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}
Shani answered 28/7, 2009 at 18:29 Comment(0)
C
4

No, calling Dispose() on SqlConnection also calls Close().

MSDN - SqlConnection.Dispose()

Collative answered 28/7, 2009 at 18:23 Comment(0)
P
3

No, having the Using block calls Dispose() for you anyway, so there is no need to call Close().

Perionychium answered 28/7, 2009 at 18:23 Comment(3)
Sorry, I should have said that for most objects that implement IDisposable and have a Close() method, calling Close() ends up calling Dispose() behind the scenes for you anyway.Perionychium
Isn't that the other way around - Dispose() calls Close(), not vice-versa?Chang
It's both, usually. For some reason they decided to implement that Close would call Dispose as well. For a SqlConnection this isn't a big deal, but StreamWriters will throw an exception if you close and then Dispose them. My guess would be they won't change that behaviour simply because it's what people have now come to expect.Watery
C
2

No, it is not necessary to Close a connection before calling Dispose.

Some objects, (like SQLConnections) can be re-used afer calling Close, but not after calling Dispose. For other objects calling Close is the same as calling Dispose. (ManualResetEvent and Streams I think behave like this)

Cloven answered 28/7, 2009 at 18:26 Comment(0)
G
1

No, the SqlConnection class inherits from IDisposable, and when the end of using (for the connection object) is encountered, it automatically calls the Dispose on the SqlConnection class.

Goddord answered 28/7, 2009 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.