dispose close
We are always hearing that we should dispose of a database connection rather than just closing it because a database connection is an unmanaged resource (SqlConnection, OledbConnection, OdbcConnection). So you would think the following code makes sense:
我们总是听到我们应该处置数据库连接而不是仅仅关闭它,因为数据库连接是不受管的资源(SqlConnection,OledbConnection,OdbcConnection)。 因此,您认为以下代码有意义:
using(SqlConnection con= new SqlConnection())
{
//open database conncetion
//do database transaction
//close connection
con.Close();
}
At first glance it looks like nice, clean code. We are closing all the resources we are opening as well as disposing (the using block will make sure that Dispose will be called for con object) the same to make sure there will be no memory leaks.
乍一看,它看起来像是不错的干净代码。 我们将关闭所有正在打开以及处置的资源(使用代码块将确保将对con对象调用Dispose)相同,以确保不会发生内存泄漏。
But when we go inside SqlConnection.Dispose() method using reflector, we find that we are again calling the same Close() method.
但是,当我们使用反射器进入SqlConnection.Dispose()方法时,我们发现我们再次调用了相同的Close()方法。
protected override void Dispose(bool disposing)
{
if (disposing)
{
this._userConnectionOptions = null;
this._poolGroup = null;
this.Close();
}
this.DisposeMe(disposing);
base.Dispose(disposing);
}
Dispose is doing much more than simply closing the connection, – like making _poolGroup null. So the better approach to managing connections is:
Dispose所做的不只是简单地关闭连接,还包括使_poolGroup为空。 因此,管理连接的更好方法是:
using(SqlConnection con= new SqlConnection())
{
//open database conncetion
//do database transaction
//close connection
}
Now the Dispose() method will make sure that connection will be closed.
现在,Dispose()方法将确保关闭连接。
In addition, if we are simply calling con.Close(), as Close() we will leave the connection to the connection pool which will result in better performance. And then we can use Dispose() to remove the connection from the pool.
另外,如果我们只是简单地调用con.Close(),则将其作为Close()保留在连接池中,这将导致更好的性能。 然后,我们可以使用Dispose()从池中删除连接。
Following is the proper implementation of CloseConnection() which is called by the Close() method:
以下是由Close()方法调用的CloseConnection()的正确实现:
internal virtual void CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
{
Bid.PoolerTrace("<prov.DbConnectionInternal.CloseConnection|RES|CPOOL> %d# Closing.\n", this.ObjectID);
if (connectionFactory.SetInnerConnectionFrom(owningObject, DbConnectionOpenBusy.SingletonInstance, this))
{
try
{
DbConnectionPool pool = this.Pool;
Transaction enlistedTransaction = this.EnlistedTransaction;
if ((null != enlistedTransaction) && (enlistedTransaction.TransactionInformation.Status != TransactionStatus.Active))
{
this.DetachTransaction(enlistedTransaction);
}
if (pool != null)
{
pool.PutObject(this, owningObject);
}
else
{
this.Deactivate();
this.PerformanceCounters.HardDisconnectsPerSecond.Increment();
this._owningObject.Target = null;
if (this.IsTransactionRoot)
{
this.SetInStasis();
}
else
{
this.PerformanceCounters.NumberOfNonPooledConnections.Decrement();
if (base.GetType() != typeof(SqlInternalConnectionSmi))
{
this.Dispose();
}
}
}
}
finally
{
connectionFactory.SetInnerConnectionEvent(owningObject, DbConnectionClosedPreviouslyOpened.SingletonInstance);
}
}
}
So my take on this is: If performance is not an issue, you can still use Connection.Dispose(). But if you want better performance use the Connection.Close() method I have described.
因此,我的看法是:如果性能不是问题,您仍然可以使用Connection.Dispose()。 但是,如果您想获得更好的性能,请使用我已经描述的Connection.Close()方法。
dispose close