简体   繁体   English

SQL Server连接计数问题

[英]SQL Server connection count issue

I am using SQL Server 2008 Enterprise + .Net 3.5 + C# + ADO.Net. 我正在使用SQL Server 2008 Enterprise + .Net 3.5 + C#+ ADO.Net。 I am using the following SQL statement to monitor connection number, is it correct? 我正在使用以下SQL语句监视连接号,是否正确? If yes, my confusion is, one connection from ADO.Net client maps to only one connection in the following statement? 如果是,我的困惑是,在下面的语句中,来自ADO.Net客户端的一个连接映射为仅一个连接? Or one ADO.Net connection could maps to multiple connections here? 还是一个ADO.Net连接可以在这里映射到多个连接?

SELECT  *  FROM sys.dm_os_performance_counters WHERE object_name = 'SQLServer:General Statistics'

(Monitor User Connections row) (“监视用户连接”行)

thanks in advance, George 预先感谢乔治

Use SELECT * FROM sys.dm_exec_connections to find all the connections. 使用SELECT * FROM sys.dm_exec_connections查找所有连接。 The client_net_address has the client address so you can track down the origin of connections. client_net_address具有客户端地址,因此您可以跟踪连接的来源。

Use SELECT * FROM sys.dm_exec_sessions to find all the sessions (sessions in general map 1 to 1 with connections unless MARS is used). 使用SELECT * FROM sys.dm_exec_sessions查找所有会话(除非使用MARS ,否则会话通常与连接建立1到1映射)。 The program_name column will contain the value of the application name you passed in in the connection string and allows you to identify your own connections. program_name列将包含您在连接字符串中传入的应用程序名称的值,并允许您标识自己的连接。

Use SELECT * FROM sys.dm_exec_requests to see all the current executing batches (requests). 使用SELECT * FROM sys.dm_exec_requests查看所有当前正在执行的批处理(请求)。

The performance counter would only give you one value, the number of current connections: 性能计数器只会给您一个值,即当前连接数:

SELECT  cntr_value
FROM sys.dm_os_performance_counters 
WHERE object_name = 'SQLServer:General Statistics'
  and counter_name = 'User Connections'

Would this work for your needs? 可以满足您的需求吗? I'm confused if you're trying to count the number of connections. 如果您要计算连接数,我会感到困惑。 Your question seems to say no, where your comment implies yes to me. 您的问题似乎说不,您的评论对我意味着是。

Sp_who2 'Active'

By default, the underlying SQL Server driver code uses a connection pool. 默认情况下,基础SQL Server驱动程序代码使用连接池。 You'll find that the number of physical connections "owned" by your application will grow over time to the current limit, but this is different from the number that are "in use". 您会发现,应用程序“拥有”的物理连接数将随着时间增长到当前限制,但这与“正在使用”的数不同。

This avoids renegotiating security, etc. on each link, speeding up your application's database access. 这样可以避免重新协商每个链接上的安全性等,从而加快了应用程序对数据库的访问。

As mentioned by @sgmarshall, use the sp_who2 stored procedure to determine what each of the connections are currently doing. 如@sgmarshall所述,使用sp_who2存储过程来确定每个连接当前正在做什么。

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

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