tags:

views:

247

answers:

6

Does the using statement really call the close method when used with a database connection object? The MSDN documentation says it ensures that the Dispose method is called but makes no mention of close. I see posts on Stack Overflow where people say that it does both. Does someone have a concrete answer one way or another on this from Microsoft or other solid proof that it does?

+2  A: 

Calling the Close method will call Dispose. It doesn't matter which one is called.

A concrete example is the FileStream.Close() method:

"This implementation of Close calls the Dispose method passing a true value."

-- http://msdn.microsoft.com/en-us/library/aa328800.aspx

maxyfc
+8  A: 

Here is the "Dispose" method of the SqlConnection class:

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

As you can see, it does indeed call Close()

DancesWithBamboo
+1  A: 

Any Class can have a Dispose method if it implements the IDisposable interface.

MSDN says: "The primary use of this interface is to release unmanaged resources."

It is left to the implementor to decide what exactly that means.

In the case of a DBConnection, disposing also means closing connection...

qux
So it's assumed that whoever implements IDisposable is going to call close internally on the Dispose method, right? This seems a bit subjective...
James
A: 

Close and Dispose do the same thing. They added Close simply for readability since to say that you "Closed the connection" is more natural.

AaronLS
+4  A: 

If you use a using statement the connection will be closed, it makes no sense that a object that implements IDisposable, will remain open after it is garbage collected...

But Close() and Dispose() aren't the same thing:

  • Dispose() always calls Close() implicitly.
  • Dispose() clears the ConnectionString, Close() doesn't.
  • If you're going to re-open the connection again, you should Close() not Dispose()

And if you choose to use Dispose() don't call it direcly, the using statement it's the best way to do it.

CMS
+1  A: 

Bear in mind though that all Close() does is release the connection back to the connection pool. You will still notice an active connection to the database in SQL Server. If you need to ensure that this is closed as well, you might want to consider ClearAllPools or ClearPool

From MSDN Connection pooling reduces the number of times that new connections need to be opened. The pooler maintains ownership of the physical connection. It manages connections by keeping alive a set of active connections for each given connection configuration. Whenever a user calls Open on a connection, the pooler looks to see if there is an available connection in the pool. If a pooled connection is available, it returns it to the caller instead of opening a new connection. When the application calls Close on the connection, the pooler returns it to the pooled set of active connections instead of actually closing it. Once the connection is returned to the pool, it is ready to be reused on the next Open call.

Abhijeet Patel
+1 Good to know, does the connection return to the pool after a certain timeout of inactivity?
James