C# 在 Dispose 方法中关闭数据库连接是否正确?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1016800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does closing a database connection in Dispose method is right?
提问by agnieszka
I've had a suspicion that a database connection used in one of our applications is not always closed. I went to see the code and I've found a class DataProvider
that has SqlConnection
object. The connection is opened in the constructor of this class and closed in it's Dispose
method (don't judge that, I know keeping an open connection is evil, it's just not my code and it's not the point of the question anyway). The Dispose
method is implemented like this:
我怀疑我们的一个应用程序中使用的数据库连接并不总是关闭。我去看了代码,发现了一个DataProvider
有SqlConnection
对象的类。连接在此类的构造函数中打开并在它的Dispose
方法中关闭(不要判断,我知道保持打开的连接是邪恶的,这不是我的代码,无论如何这不是问题的重点)。该Dispose
方法是这样实现的:
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if (_conn != null)
_conn.Close();
}
_disposed = true;
}
}
The question is:
Does it always guarantee that the connection is closed?
Is this code right?
问题是:
它是否总是保证连接是关闭的?
这段代码对吗?
I think there should be _conn.Dispose()
called - am I right and could it affect not closing the connection (probably not)?
我认为应该_conn.Dispose()
调用 - 我是对的,它是否会影响不关闭连接(可能不会)?
采纳答案by Cédric Rup
Dispose is never called automatically.
Dispose 永远不会自动调用。
The connection will not be closed until the Dispose method of your object is explicitly called, or if your class in used in a using() block
在显式调用对象的 Dispose 方法之前,或者如果您的类在 using() 块中使用,连接不会关闭
A safer way is to call the dispose method in your finalizer and ensure the finalizer is suppressed when the Dispose method is called.
更安全的方法是在终结器中调用 dispose 方法,并确保在调用 Dispose 方法时抑制终结器。
This articlepresent the correct way to implement the pattern
本文介绍了实现该模式的正确方法
Hope it helps !
希望能帮助到你 !
Cédric
塞德里克
回答by Hath
conn.Dispose();
will also close the connection, so can't hurt changing it to follow the dispose pattern.
conn.Dispose();
也将关闭连接,因此更改它以遵循处置模式不会有什么坏处。
But there functionally equivalent so there must be a problem else where.
但是在功能上是等效的,所以其他地方肯定有问题。
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx
If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose. Close and Dispose are functionally equivalent. If the connection pooling value Pooling is set to true or yes, the underlying connection is returned back to the connection pool. On the other hand, if Pooling is set to false or no, the underlying connection to the server is closed.
如果 SqlConnection 超出范围,它不会被关闭。因此,您必须通过调用 Close 或 Dispose 显式关闭连接。Close 和 Dispose 在功能上是等效的。如果连接池值 Pooling 设置为 true 或 yes,则底层连接返回到连接池。另一方面,如果 Pooling 设置为 false 或 no,则与服务器的底层连接将关闭。