简体   繁体   English

如果没有调用SqlConnection.Close()或SqlConnection.Dispose(),它会是什么?

[英]What will it be if SqlConnection.Close() or SqlConnection.Dispose() is not called?

I just found that one ASP.NET web application in my company would connect to a SQL Server 2008 R2 database to read/write data, but in some part of codes, both SqlConnection.Close() and SqlConnection.Dispose() are never called after using the connection. 我刚刚发现我公司的一个ASP.NET Web应用程序将连接到SQL Server 2008 R2数据库以读取/写入数据,但在代码的某些部分,从不调用SqlConnection.Close()SqlConnection.Dispose()使用连接后。

From MSDN, SqlConnection Class : 从MSDN, SqlConnection类

If the SqlConnection goes out of scope, it won't be closed. 如果SqlConnection超出范围,则不会关闭。 Therefore, you must explicitly close the connection by calling Close or Dispose. 因此,您必须通过调用Close或Dispose显式关闭连接。

I use sp_who and find that more and more connections are created as I perform some database access via the web application. 我使用sp_who并发现当我通过Web应用程序执行某些数据库访问时,会创建越来越多的连接。 But after a few minutes, some connections seems dropped. 但几分钟后,一些连接似乎被取消了。

Is there anything .Net runtime done to handle such case that SqlConnection.Close() and SqlConnection.Dispose() are not called? 是否有任何.Net运行时完成处理这样的情况,不调用SqlConnection.Close()SqlConnection.Dispose() And is there any potential problems which affect the performance and stability? 是否存在影响性能和稳定性的潜在问题?

It will go OK and you won't get any error for some time. 它会好的,你不会在一段时间内得到任何错误。 As your application runs and each time you open a SQL connection and keep it non closed, after sometime you SQL connection pool will got full and system throws connection pool overflow error as you try to open a new SQL connection. 当您的应用程序运行时,每次打开SQL连接并使其保持非关闭状态,在某段时间后,当您尝试打开新的SQL连接时,SQL连接池将变满并且系统会引发连接池溢出错误。

So always keep a connection close code within your function. 因此,请始终在函数中保持连接关闭代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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