Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

KILLING spids Automatically

Posted on 2009-05-18
2
Medium Priority
?
897 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 1000 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 1000 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

721 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