Solved

KILLING spids Automatically

Posted on 2009-05-18
2
874 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now