gpt4 book ai didi

.net - 超过 100 个连接到 sql server 2008 处于 "sleeping"状态

转载 作者:行者123 更新时间:2023-12-04 01:52:32 28 4
gpt4 key购买 nike

我在这里遇到了大麻烦,在我的服务器上。

我有一个 ASP .net web(框架 4.x)在我的服务器上运行,所有的事务/选择/更新/插入都是用 ADO.NET 进行的。

问题是,在使用一段时间后(几次更新/选择/插入),当使用此查询检查 sql server 上的连接时,有时我会在“ sleep ”状态下获得 100 多个连接:

SELECT 
spid,
a.status,
hostname,
program_name,
cmd,
cpu,
physical_io,
blocked,
b.name,
loginame
FROM
master.dbo.sysprocesses a INNER JOIN
master.dbo.sysdatabases b ON
a.dbid = b.dbid where program_name like '%TMS%'
ORDER BY spid

我一直在检查我的代码并在每次建立连接时关闭,我要测试新类,但我担心问题没有解决。

它假设连接池,保持连接重新使用它们,但直到我看到不要总是重新使用它们。

除了在使用它们后检查关闭所有打开的连接之外,还有什么想法吗?

已解决 (现在我只有一个关于“ sleep ”状态的美好联系):

除了 的回答者大卫·斯特拉顿 ,我想分享这个链接,它有助于很好地解释连接池是如何工作的: http://dinesql.blogspot.com/2010/07/sql-server-sleeping-status-and.html

简而言之,您需要关闭每个连接(sql 连接对象),以便连接池可以重新使用该连接并使用相同的 connectinos 字符串,以确保强烈建议使用 webConfig 之一。

小心使用 dataReaders 你应该关闭它的连接(这让我很生气)。

最佳答案

听起来像是连接池。

从这里:http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx

A connection pool is created for each unique connection string. When a pool is created, multiple connection objects are created and added to the pool so that the minimum pool size requirement is satisfied. Connections are added to the pool as needed, up to the maximum pool size specified (100 is the default). Connections are released back into the pool when they are closed or disposed.



为确保您不会创建不必要的池,请确保每次连接时都使用完全相同的连接字符串 - 将其存储在 .config 文件中。

如果您愿意,您还可以减少最大池大小。

实际上,我建议您阅读上面链接的整篇文章。它讨论了清除池,并为您提供了正确使用池的最佳实践。

编辑 - 第二天添加

由于连接池的工作方式,服务器上的池在那里。根据上面链接的文档:

The connection pooler removes a connection from the pool after it has been idle for a long time, or if the pooler detects that the connection with the server has been severed. Note that a severed connection can be detected only after attempting to communicate with the server. If a connection is found that is no longer connected to the server, it is marked as invalid. Invalid connections are removed from the connection pool only when they are closed or reclaimed.



这意味着服务器本身最终会清理这些池,如果它们仍然未使用。如果未清除,则意味着服务器认为连接仍在使用中,并且正在挂起以提高您的性能。

换句话说,除非您发现问题,否则我不会担心。连接池正在按照它应有的方式进行。

如果您真的想清除池,请再次按照文档:

Clearing the Pool

ADO.NET 2.0 introduced two new methods to clear the pool: ClearAllPools and ClearPool. ClearAllPools clears the connection pools for a given provider, and ClearPool clears the connection pool that is associated with a specific connection. If there are connections being used at the time of the call, they are marked appropriately. When they are closed, they are discarded instead of being returned to the pool.



但是,如果要调整池化,则可以修改连接字符串。请参阅此页面,并搜索“池”一词:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx

或者,您可以招募 DBA 来协助和设置服务器级别的池。这是题外话,但 ServerFault.com 可能有人在那里提供帮助。

关于.net - 超过 100 个连接到 sql server 2008 处于 "sleeping"状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7561186/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com