简体   繁体   中英

Using statement and Close methods

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?

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()

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.

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.

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...

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM