简体   繁体   English

如何监视SQL Server中的活动连接池?

[英]How to monitor active connection pool in SQL Server?

I'm suspecting that my web application has connection leaks (getting the timeout and max connection reached error). 我怀疑我的Web应用程序有连接泄漏(获得超时和最大连接到达错误)。 So I wanted to monitor how many database connections are active in the pool. 所以我想监视池中活动的数据库连接数。 I'm using the SQL Express so I don't have User Connections performance counter as suggested in some help guides. 我正在使用SQL Express,因此我没有一些帮助指南中建议的用户连接性能计数器。

I do found that I could also use Performance Monitor of Win 2008 server but I've no idea how to do so. 我发现我也可以使用Win 2008服务器的性能监视器,但我不知道该怎么做。 Any guide on that would be appreciated. 任何指南都将不胜感激。

Use simple 使用简单

SELECT * FROM sys.dm_exec_connections

OR 要么

Try and adopt this script in case if you need some other data 如果您需要其他数据,请尝试并采用此脚本

declare @now    datetime
set     @now    = getdate()
set nocount off
select  p.spid                      as  spid
    ,   rtrim(p.loginame)           as  SQLUser
    ,   rtrim(p.nt_username)        as  NTUser
    ,   rtrim(p.nt_domain)          as  NTDomain
    ,   rtrim(case
        when p.blocked <> 0 then 'BLOCKED'
        else p.status
        end)                        as status
    ,   case 
        when p.blocked is null or p.blocked = 0 then ''     
        else convert(varchar(10),p.blocked)
        end                         as  BlockedBySpid
    ,   rtrim(p.cmd)                as  CurrentCommand
    ,   case when p.dbid = 0 then '' else rtrim(db_name(p.dbid)) end    as  DBName
    ,   isnull(rtrim(p.program_name),'')        as  ProgramName
    ,   cast( cast(p.waittype as int) as nvarchar(10)) as  CurrentWaitType
    ,   p.waittime              as  CurrentWaitTime
    ,   p.lastwaittype              as  LastWaitType
    ,   rtrim(p.waitresource)       as  LastWaitResource
    ,   p.open_tran                 as  OpenTransactionCnt
    ,   p.cpu                       as  CPUTime
    ,   convert(bigint, p.physical_io) as   DiskIO
    ,   p.memusage                  as  MemoryUsage
    ,   p.hostprocess               as  HostProcess
    ,   rtrim(p.hostname)           as  HostName
    ,   p.login_time                as  LoginTime
    ,   p.last_batch                as  LastBatchTime
    ,   p.net_address               as  NetAddress
    ,   ltrim(rtrim(p.net_library)) as  NetLibrary
    ,   case 
        when    lower(p.status) not in ('sleeping', 'background', 'dormant', 'suspended') 
        or      p.open_tran > 0
        or      p.blocked   > 0
        or      upper(ltrim(rtrim(p.cmd))) like 'WAITFOR%'
        then    'Y'
        else    'N'
        end                         as  Active
    ,   case
        when    p.net_address <> '' --  Non system processes
        and     p.program_name not like 'SQLAgent - %'
        then 'N'
        else 'Y'
        end                         as  SystemProcess
    ,   case 
        when p.last_batch = '19000101'      then 'n/a'
        when datediff(day,      p.last_batch, @now) >   2   then convert(varchar(10),datediff(day,      p.last_batch, @now)) + ' days'
        when datediff(hour,     p.last_batch, @now) >=  4   then convert(varchar(10),datediff(hour,     p.last_batch, @now)) + ' hrs'
        when datediff(minute,   p.last_batch, @now) >=  10  then convert(varchar(10),datediff(minute,   p.last_batch, @now)) + ' min'
        else convert(varchar(10),datediff(second, p.last_batch, @now)) + ' sec'
        end                         as  TimeSinceLastBatch
    ,   p.kpid                      as  InternalKPID
    ,   case    
        when    (lower(p.status) in ('background', 'dormant') 
        and     p.open_tran <= 0
        and     p.blocked   <= 0
        and     upper(ltrim(rtrim(p.cmd))) not like 'WAITFOR%'
        ) or (
        lower(p.status) like '%sleeping%'
        )
        then    0
        else    p.kpid
        end                         as  kpid
    , (convert(nvarchar,p.spid) + '.' + case    
                                        when    (lower(p.status) in ('background', 'dormant') 
                                        and     p.open_tran <= 0
                                        and     p.blocked   <= 0
                                        and     upper(ltrim(rtrim(p.cmd))) not like 'WAITFOR%'
                                        ) or (
                                        lower(p.status) like '%sleeping%'
                                        )
                                        then    '0'
                                        else    convert(nvarchar,p.kpid)
                                        end) + '.' + convert(nvarchar,convert(float, p.login_time)) as SessionLifeTimeKey                
    ,   convert(float, p.login_time) as 'LoginTimeFloatDiff'
from    sys.sysprocesses            p   with (readpast)

This will give number of connections per database, per user: 这将为每个用户提供每个数据库的连接数:

SELECT 
    DB_NAME(dbid) as DBName, 
    COUNT(dbid) as NumberOfConnections,
    loginame as LoginName
FROM
    sys.sysprocesses
WHERE 
    dbid > 0
GROUP BY 
    dbid, loginame
;

You can execute built-in stored procedure 您可以执行内置存储过程

exec sp_who
exec sp_who2

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

相关问题 SQL Server连接字符串的连接池设置 - Connection pool setting of SQL Server connection string SQL连接池超时设置和SQL Server远程查询设置 - SQL Connection Pool timeout setting and SQL Server remote query setting 如何解决 ASP.NET 和 SQL 服务器之间的连接池问题? - How can I solve a connection pool problem between ASP.NET and SQL Server? ASP.NET应用程序池和SQL Server数据连接 - Asp.net application pool and sql server data connection 修复连接池最大化/查找到 SQL 服务器的打开连接 - Fixing Connection Pool maxing out / Finding open connections to SQL Server 从inproc切换到SQL Server会给我SQL超时(无连接最大池大小,键入数据集) - Switched from inproc to SQL Server gives me SQL timeouts (no connection max pool size, typed datasets) 如何关闭活动的SQL Server连接? - how to close active SQL Server connections? 如何为SQL Server 2012创建连接字符串? - How to create connection string for SQL Server 2012? 如何与远程数据库sql服务器建立连接? - How to establish connection to remote database sql server? 在 ASP.NET 网站中,超过 100 个与 SQL 服务器数据库的活动连接是否不正常? - Is more than 100 active connection to SQL server db not normal in an ASP.NET website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM