如何强制的SqlConnection到身体接近,而使用连接池? [英] How to force a SqlConnection to physically close, while using connection pooling?

查看:196
本文介绍了如何强制的SqlConnection到身体接近,而使用连接池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白,如果我实例化一个SqlConnection对象,我真的抓住从一个连接池的连接。当我调用open(),它会打开连接。如果我调用SqlConnection对象上的关闭()或Dispose()方法,它返回到连接池。

I understand that if I instantiate a SqlConnection object, I am really grabbing a connection from a connection pool. When I call Open(), it will open the connection. If I call the Close() or Dispose() method on that SqlConnection object, it is returned to the connection pool.

不过,这并没有真正告诉我,如果真的关闭,或者我还是要对数据库的有效连接。

However, that doesn't really tell me if it's really closed, or if I still have an active connection to the database.

我如何强制的SqlConnection收于网络层,或者至少告诉它关闭时?

How can I force a SqlConnection to close at the network level, or at least tell when it closes?

示例:

using(SqlConnection conn = new SqlConnection(DBConnString)) {

   conn.Open();
   SqlCommand cmd = conn.CreateCommand();
   ...
   cmd.ExecuteReader(CommandBehavior.CloseConnection);
   ...
}

  • 在第一次运行:300毫秒
  • 第二次运行:100毫秒
  • 第三轮:100毫秒
  • 在等待很长一段时间(30分钟)后:300毫秒
    • First run: 300 ms
    • Second run: 100 ms
    • Third run: 100 ms
    • After waiting a long time (30 minutes): 300 ms
    • 如果连接被利闭合,在第二和第三运行也应300毫秒。但我知道,连接没有真正封闭的运行(我检查了SQL Server的活动监视器)。它并不需要额外的200毫秒执行身份验证的/ etc。

      If the connection was TRULY closing, the second and third runs should also be 300 ms. But I know that the connection is not truly closed for those runs (I checked the SQL Server's activity monitor). It doesn't take the extra 200ms to perform authentication/etc.

      我如何强制连接到真正的亲密?

      How do I force the connection to truly close?

      创意

      • 是否CommandBehavior.CloseConnection工作? (显然不是?)
      • 设置是否在连接字符串工作最大池大小= 0? (这将是一个得不偿失的解决方案)
      • 是否Dispose()方法的工作?

      引用

      • Article on Connection Pooling
      • Here's another one that tells us that Close() doesn't really close the connection.
      • An article on pros and cons connection pooling

      推荐答案

      也许<一href="https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.clearpool"><$c$c>SqlConnection.ClearPool

      这篇关于如何强制的SqlConnection到身体接近,而使用连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆