微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net – 如何监视SQL Server中的活动连接池?

我怀疑我的Web应用程序有连接泄漏(获取超时和最大连接达到错误).所以我想监视池中有多少数据库连接是活动的.我正在使用sql Express,所以我没有在一些帮助指南中建议的用户连接性能计数器.

我发现我也可以使用Win 2008服务器的性能监视器,但我不知道如何做到这一点.任何指导,将不胜感激.

解决方法

使用简单
SELECT * FROM sys.dm_exec_connections

要么

如果您需要其他数据,请尝试并采用此脚本

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,@Now)) + ' days'
        when datediff(hour,@Now) >=  4   then convert(varchar(10),datediff(hour,@Now)) + ' hrs'
        when datediff(minute,@Now) >=  10  then convert(varchar(10),datediff(minute,@Now)) + ' min'
        else convert(varchar(10),datediff(second,@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,p.login_time) as 'LoginTimeFloatDiff'
from    sys.sysprocesses            p   with (readpast)

原文地址:https://www.jb51.cc/aspnet/250616.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐