Solved

KILLING spids Automatically

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

786 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