关闭 SQL 连接但打开的连接不断增加

发布于 2024-11-07 19:31:50 字数 1118 浏览 5 评论 0原文

我有以下方法:

public DataSet GetDataSet( string sp, params SqlParameter[] parameters ) {
DataSet ds = new DataSet();

using ( SqlConnection conn = new SqlConnection(
        ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString
    ) ) {
    using ( SqlCommand cmd = new SqlCommand() ) {
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = sp;

        if ( parameters != null ) {
            foreach ( SqlParameter parm in parameters ) {
                cmd.Parameters.Add( parm );
            }
        }

        if ( conn.State == ConnectionState.Closed ) {
            conn.Open();
        }

        using ( SqlDataAdapter da = new SqlDataAdapter( cmd ) ) {
            da.Fill( ds );
        }
    }
}

return ds; }

我注意到多次调用此方法(大约 50 次)时会创建多个连接。 我通过在 SQL 中执行此查询来检查这一点:

SELECT DB_NAME(dbid) as 'DbNAme', COUNT(dbid) as 'Connections' from master.dbo.sysprocesses with (nolock) WHERE dbid > 0 GROUP BY dbid

调用上述方法时,连接数不断增加。难道它不应该一遍又一遍地使用相同的连接(连接池)而不是创建新的连接吗?

I have the following method:

public DataSet GetDataSet( string sp, params SqlParameter[] parameters ) {
DataSet ds = new DataSet();

using ( SqlConnection conn = new SqlConnection(
        ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString
    ) ) {
    using ( SqlCommand cmd = new SqlCommand() ) {
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = sp;

        if ( parameters != null ) {
            foreach ( SqlParameter parm in parameters ) {
                cmd.Parameters.Add( parm );
            }
        }

        if ( conn.State == ConnectionState.Closed ) {
            conn.Open();
        }

        using ( SqlDataAdapter da = new SqlDataAdapter( cmd ) ) {
            da.Fill( ds );
        }
    }
}

return ds; }

I've noticed that multiple connections are created when calling this method multiple times (about 50 times).
I've checked this by executing this query in SQL:

SELECT DB_NAME(dbid) as 'DbNAme', COUNT(dbid) as 'Connections' from master.dbo.sysprocesses with (nolock) WHERE dbid > 0 GROUP BY dbid

The number of connections keeps incrementing when calling the above method. Shouldn't it use the same connection over and over again (connection pooling) instead of creating new ones?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆光下的微笑 2024-11-14 19:31:50

此链接解释了连接汇集得很好。如果你想了解整件事,你应该阅读这本书,它非常好。

连接池减少了数量
新连接必须的时间
打开。池化者保留所有权
的物理连接。它管理
通过保持一组连接
每个给定的活动连接
连接配置。每当一个
用户在连接上调用 Open,
pooler 寻找可用的
池中的连接。如果汇集
连接可用,它返回它
给调用者而不是打开一个新的
联系。当应用程序调用
关闭连接,池化器
将其返回到活动池集合
连接而不是关闭它。
一旦连接返回
池,它已准备好在
下一次公开电话会议。

This link explains connection pooling very well. If you want to understand the whole thing you should read this one it is very good.

Connection pooling reduces the number
of times that new connections must be
opened. The pooler maintains ownership
of the physical connection. It manages
connections by keeping alive a set of
active connections for each given
connection configuration. Whenever a
user calls Open on a connection, the
pooler looks for an available
connection in the pool. If a pooled
connection is available, it returns it
to the caller instead of opening a new
connection. When the application calls
Close on the connection, the pooler
returns it to the pooled set of active
connections instead of closing it.
Once the connection is returned to the
pool, it is ready to be reused on the
next Open call.

软的没边 2024-11-14 19:31:50

尝试增加 web.config 中连接字符串的最大池大小,如下所示

<add name="ConString" connectionString="SERVER=localhost;DATABASE=databasename;UID=username;PWD=password;Pooling=true;Max Pool Size=100;"/>

或您定义的任何位置。它解决了问题,但暂时为了永久解决方案搜索您的代码,可能您没有关闭连接

希望它会帮助您

try to increase maximum pool size in you connection string in web.config like this

<add name="ConString" connectionString="SERVER=localhost;DATABASE=databasename;UID=username;PWD=password;Pooling=true;Max Pool Size=100;"/>

or wherever you defined it.it solves the problem but temporarily for permanent solution search your code probably you did not close the connection

hope it will help you

迷爱 2024-11-14 19:31:50

连接池并不意味着它将重用连接。由于建立 SQL 连接的成本很高,因此连接池保持固定的最大打开连接数,当您在连接上调用 .Close() 时,它只是返回到池中,然后就可以当在新实例上调用 Open() 时,将其传递给新连接。

该机制内置于 SqlConnection 类中,这就是它对用户透明发生的原因;简而言之:只要您正确使用连接(就像您所做的那样),您就不必担心打开的连接数量。

Connection pooling doesn't mean that it will reuse the connection. Since it is expensive to establish a SQL connection the connection pool keeps a fixed maximum of connections opens, and when you call .Close() on the connection it is simply returned to the pool which then is able to pass it to a new connection when Open() is invoked on a new instance.

This mechanism is built into the SqlConnection class which is why it happens transparently to the user; in short: You shouldn't worry about the number of open connections as long as you are using the connections correctly (as you do).

无声静候 2024-11-14 19:31:50

填充数据集后尝试关闭连接。使用语句释放对象但不会关闭连接。

Try to close connection after filling dataset. Using statment releases object but it doesnt close connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文