Solved

KILLING spids Automatically

Posted on 2009-05-18
2
889 Views
Last Modified: 2012-05-07
My company has recently adopted a policy where idle Citrix connection are terminated during the middle of the night.  Most of these disconnected sessions have been using an application based on SQL Server, and the SQL Server connection persists after the Citrix connection has been dropped.

I have been researching the periodic removal of idle processes in SQL, using KILL spid.  I have read that processes with a cmd of 'AWAITING COMMAND' should NOT be killed.  However, I have never found anything which actually says WHY they shouldnt be killled, or the dangers...

As the user has been diconnected, these processes are orphaned.  Is there any functionality in SQL server to drop idle connections?  Is it really naughty to KILL these orphaned spids?

Thanks,

- Michael  
0
Comment
Question by:mjs082969
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
ID: 24412837
First, I would do a bit of research to make sure that these connections should be killed.  I would do some testing to identify spids that are associated with the citrix connection, kill that connection, and see if the sql connection goes away.

I can't imagine any situation where I would warrant a solution where you loop through spids and kill them...not really a great idea, but could be used as a last resort.  You could just use a cursor to do it.  Its easy to write.
0
 
LVL 15

Assisted Solution

by:MohammedU
MohammedU earned 250 total points
ID: 24414264
What are you trying to accomplish by killing idle connections?
If the server connection is closed and you still have the connection then it is called orphaned but not when the sql recieves the acknowlegement from the client...
You can kill these connections as along as you don't need them and I will go by filtering hostname....

Check the following article...
http://blogs.msdn.com/sql_protocols/archive/2006/03/09/546852.aspx


DECLARE @MSSEARCH_SPID VARCHAR(5)
DECLARE @STMT VARCHAR(500)

--use a cursor to loop through the sysprocesses table

DECLARE MSS_CURSOR CURSOR FOR
SELECT SPID
FROM MASTER.DBO.SYSPROCESSES
WHERE LASTWAITTYPE = 'MSSEARCH'
AND DATEDIFF(MINUTE,LOGIN_TIME,LAST_BATCH) > 60

--open a cursor

OPEN MSS_CURSOR
FETCH NEXT FROM MSS_CURSOR INTO @MSSEARCH_SPID

--use a while loop to fetch records from the cursor

WHILE @@FETCH_STATUS=0
BEGIN

--use the SQL KILL command

SET @STMT='KILL ' + @MSSEARCH_SPID
PRINT @STMT
EXEC(@STMT)

FETCH NEXT FROM MSS_CURSOR INTO @MSSEARCH_SPID
END

--close the cursor

CLOSE MSS_CURSOR
DEALLOCATE MSS_CURSOR
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Use SSRS to email customers? 4 29
Access PS SQLSERVER from powershell 1 30
T-SQL Query 9 35
Need SQL Query improved for multiple column joins 2 9
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question