导航

.net sqlserver 关于连接池没事释放的注意项

Posted on 2008-08-23 22:59  Heclei  阅读(866)  评论(0编辑  收藏  举报
CAUTION   It is recommended that you always close the Connection when you are finished using it in order for the connection to be returned to the pool. This can be done using either the Close or Dispose methods of the Connection object. Connections that are not explicitly closed might not be added or returned to the pool. For example, a connection that has gone out of scope but that has not been explicitly closed will only be returned to the connection pool if the maximum pool size has been reached and the connection is still valid.

 

Note   Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. In a finalizer, only release unmanaged resources that your class owns directly. If your class does not own any unmanaged resources, do not include a Finalize method in your class definition. For more information, see Programming for Garbage Collection.

 

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

 

注意:在使用完成之后建议使用Close或Dispose关闭数据链接,确保链接可以返回链接池中。链接没有立即关闭可能是因为没有添加或返回到链接池中。例如:当一个链接在执行域结束后没有被关闭,那么它将在链接数达到最大值或链接无效时才返回数据库链接池中。

 

Note:不要在Finalize method(析构函数)中使用Close或者Dispose来释放Connection,DataReader或其他托管对象。在析构函数中,仅释放在对象中直接使用的非托管资源。如果你的对象中没有使用非托管资源,不要定义析构函数。