根据我的理解,默认情况下,.Net 将合并连接字符串相同的 SqlConnection
对象。如果我调用 Dispose 方法,连接是否仍会被合并?
这个问题是在 ASP.NET 应用程序的上下文中提出的,该应用程序有时会在单个 PageLoad
事件中多次点击数据库。我希望将连接放入池中,但希望确认在数据操作完成后关闭和处理连接不会干扰 .NET 对连接池的处理。
最佳答案
使用连接池时,关闭 SqlConnection
只是告诉连接池您已完成它。然后池将决定是否实际关闭连接,或重新使用它。
来自MSDN docs :
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.
关于.net - SqlConnection的Dispose方法会干扰连接池吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6765733/