Solved

KILLING spids Automatically

Posted on 2009-05-18
2
885 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with a query 3 37
Need sql in string 2 29
Options for Linking SQL tables to Access 2013 9 33
SQL Server code help needed 14 27
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

762 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