SQL Server 2005 - 关闭休眠连接

发布于 2024-07-10 12:41:44 字数 115 浏览 5 评论 0原文

我的开发服务器上似乎有一个应用程序,它有很多打开的连接(它们应该在那里,但使用了一些错误的数据层来打开它们,忘记关闭它们)。 我只是希望它们关闭,这样我就可以让其他应用程序在服务器上运行。 如何强制关闭所有连接?

I seem to have an app on my Dev server that has lots of open connections (they should be there, but some bad data layer was used to open them, that forgot to close them). I just want them closed so I can keep other apps running on the server. How can I force all the connections to close?

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

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

发布评论

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

评论(4

走走停停 2024-07-17 12:41:44

使用以下脚本来终止来自特定主机/登录名的非活动会话。 您可以从计划的作业中使用它,当然您的首要任务应该是修复您的应用程序层。

SET NOCOUNT ON;

DECLARE @host VARCHAR(50), @login NVARCHAR(128);

SET @host = 'fooHost'; --NULL to kill sessions from all hosts.
SET @login = 'fooLogin';

DECLARE @cmd NVARCHAR(255);
DECLARE @possition INT, @total INT, @selSpid SMALLINT;
DECLARE @spidInfo TABLE
(
    [id] INT IDENTITY(1,1),
    spid SMALLINT,
    loginame NVARCHAR(128)
);

INSERT @spidInfo(spid, loginame)
SELECT session_id, login_name 
FROM sys.dm_exec_sessions
WHERE is_user_process = 1 AND [status] = 'sleeping' AND 
    login_name = @login AND [host_name] = COALESCE(@host, [host_name]);

SELECT @total = @@IDENTITY, @selSpid = 0, @possition = 0;

WHILE @possition < @total
    BEGIN
        SELECT TOP 1 @selSpid = spid, @possition = [id]
        FROM @spidInfo
        WHERE [ID] > @possition

        SET @cmd = N'KILL ' + CAST(@selSpid AS NVARCHAR(10));
        EXEC sp_executesql @cmd;
        PRINT 'SessionId = ' + CAST(@selSpid AS NVARCHAR(10)) + '[' + @login + 
            '] killed by ' + system_user + ' at ' + CAST(GETDATE() AS VARCHAR(50));
    END;

IF (@total = 0)
    PRINT 'No sessions owned by user ' + '[' + @login + ']';

Use the following script to kill inactive sessions from a specific host / login. You could use it from a scheduled job, of course your priority should be to fix your app tier.

SET NOCOUNT ON;

DECLARE @host VARCHAR(50), @login NVARCHAR(128);

SET @host = 'fooHost'; --NULL to kill sessions from all hosts.
SET @login = 'fooLogin';

DECLARE @cmd NVARCHAR(255);
DECLARE @possition INT, @total INT, @selSpid SMALLINT;
DECLARE @spidInfo TABLE
(
    [id] INT IDENTITY(1,1),
    spid SMALLINT,
    loginame NVARCHAR(128)
);

INSERT @spidInfo(spid, loginame)
SELECT session_id, login_name 
FROM sys.dm_exec_sessions
WHERE is_user_process = 1 AND [status] = 'sleeping' AND 
    login_name = @login AND [host_name] = COALESCE(@host, [host_name]);

SELECT @total = @@IDENTITY, @selSpid = 0, @possition = 0;

WHILE @possition < @total
    BEGIN
        SELECT TOP 1 @selSpid = spid, @possition = [id]
        FROM @spidInfo
        WHERE [ID] > @possition

        SET @cmd = N'KILL ' + CAST(@selSpid AS NVARCHAR(10));
        EXEC sp_executesql @cmd;
        PRINT 'SessionId = ' + CAST(@selSpid AS NVARCHAR(10)) + '[' + @login + 
            '] killed by ' + system_user + ' at ' + CAST(GETDATE() AS VARCHAR(50));
    END;

IF (@total = 0)
    PRINT 'No sessions owned by user ' + '[' + @login + ']';
作妖 2024-07-17 12:41:44

除了手动终止连接之外,您还可以

  • 处置连接。 也就是说,以某种方式检索 Connection 对象并对其调用 .Close 和 .Dispose()。 使用“using”将是理想的选择,因为它会自动为您调用 .Dispose() 。
  • 回收您的应用程序池。

Other than killing your connections manually, you can

  • Dispose of the connections. That is, retrieve the Connection objects somehow and call .Close and .Dispose() on them. Using "using" would be ideal since it calls .Dispose() automatically for you.
  • Recycle your application pool.
春风十里 2024-07-17 12:41:44

使用 sysprocesses 中的last_batch 列来确定它是否真正处于活动状态。
SPID> 50(或者 >= 50?)以避免杀死系统 SPID。

将其与您想要的睡眠时间进行比较并杀死 spid。

你必须循环遍历。

DECLARE @kill_id smallint 
DECLARE spid_cursor CURSOR FOR
select spid from sysprocesses 
where dbid = > 4 and last_batch < dateadd(hour, -24, getdate()) and spid >= 50

OPEN spid_cursor

FETCH NEXT FROM spid_cursor INTO @kill_id

WHILE (@@FETCH_STATUS = 0)
BEGIN
-- Kill the current spid here
-- KILL @kill_id <---This line will not work

-- Get the next cursor row
FETCH NEXT FROM spid_cursor INTO @kill_id
END 

CLOSE spid_cursor

DEALLOCATE spid_cursor

Use the last_batch column from sysprocesses to work out if it's really active or not.
SPID > 50 (or is it >= 50?) to avoid killing system SPIDs.

Compare this to your desired sleep time and KILL spid.

You'll have to loop through.

DECLARE @kill_id smallint 
DECLARE spid_cursor CURSOR FOR
select spid from sysprocesses 
where dbid = > 4 and last_batch < dateadd(hour, -24, getdate()) and spid >= 50

OPEN spid_cursor

FETCH NEXT FROM spid_cursor INTO @kill_id

WHILE (@@FETCH_STATUS = 0)
BEGIN
-- Kill the current spid here
-- KILL @kill_id <---This line will not work

-- Get the next cursor row
FETCH NEXT FROM spid_cursor INTO @kill_id
END 

CLOSE spid_cursor

DEALLOCATE spid_cursor
一萌ing 2024-07-17 12:41:44

首先运行它来查找有问题的数据库......

SELECT DB_NAME(dbid) as 'Database Name', 
COUNT(dbid) as 'Total Connections' 
FROM master.dbo.sysprocesses WITH (nolock)
WHERE dbid > 0
GROUP BY dbid
SELECT @@MAX_CONNECTIONS AS 'Max Allowed Connections'

然后运行它来终止与所需数据库的连接

USE master
go

DECLARE @dbname sysname

SET @dbname = 'Events'

DECLARE @spid int
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)
WHILE @spid IS NOT NULL
BEGIN
EXECUTE ('KILL ' + @spid)
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid > @spid
END

First run this to find the offending database.....

SELECT DB_NAME(dbid) as 'Database Name', 
COUNT(dbid) as 'Total Connections' 
FROM master.dbo.sysprocesses WITH (nolock)
WHERE dbid > 0
GROUP BY dbid
SELECT @@MAX_CONNECTIONS AS 'Max Allowed Connections'

Then run this to kill the connections to the desired DB

USE master
go

DECLARE @dbname sysname

SET @dbname = 'Events'

DECLARE @spid int
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)
WHILE @spid IS NOT NULL
BEGIN
EXECUTE ('KILL ' + @spid)
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid > @spid
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文