October 4, 2009 at 10:32 am
Comments posted to this topic are about the item Kill Active connections
October 21, 2009 at 5:20 am
Hi
This is quicker:
alter database [db_name] set single_user with rollback immediate
go
October 21, 2009 at 7:09 am
the original script might kill connections that are active, what about new incoming connections, they will still be created.
With the set single_user also it is not possible to achieve the desired results, for e.g. in a farm environment where you have 10 web front ends connecting to a single sql cluster instance.
October 21, 2009 at 7:19 am
Thanks for the script. It's useful for me.
October 21, 2009 at 8:11 am
Here is a variation of the kill script that can be used on any database whether or not it is the current database. To use, set the @dbname variable to the name of the database where the connections are to be killed.
declare @dbname sysname, @spid int, @cnt int, @sql varchar(2048)
set @dbname = 'targetDB'
select @spid = min(spid), @cnt = count(*)
from master.dbo.sysprocesses
where dbid = db_id(@dbname) and spid != @@spid
print @dbname + ' connection process(es) to kill: ' + rtrim(@cnt)
while @spid is not null
begin
print 'kill connection process ' + rtrim(@spid)
set @sql = 'kill ' + rtrim(@spid)
exec(@sql)
select @spid = min(spid), @cnt = count(*)
from master.dbo.sysprocesses
where dbid = db_id(@dbname)
and spid != @@spid
end
print 'done'
October 21, 2009 at 9:36 am
This script will save me a lot of time when restoring test databases. It's simply and replaces a process I was doing manually.
Thanks.
October 21, 2009 at 3:47 pm
I agree, I think the ALTER DATABASE <databaseName> SET SINGLE_USER WITH ROLLBACK IMMEDIATE is more elegant and precludes the risk of active connections occurring while the cursor is killing the ones it knew about at the time the cursor was opened.
October 21, 2009 at 4:03 pm
Setting the database to single user do not prevent someone from making a connection. It just kills the current connections, and prevent users from making more than one new connection.
If all you need to do is get everyone out of the database to do a restore, this will do it:
use master
alter database [db_name] set offline with rollback immediate
October 21, 2009 at 5:33 pm
use master
alter database [db_name] set offline with rollback immediate
I will try this next time I replace a test DB with a copy of production. Makes sense.
Does a restore automatically set the database online? I'll test when I try it.
October 21, 2009 at 5:41 pm
After setting a db to offline it wont be available for a restore, however, you put all statements in a single transaction
--Step1
alter database <dbname>
set offline
with rollback immediate -- this is the part that kills the connections
--Step2
alter database <dbname> set online
restore database <dbname> <restore parameters>
Credit: This was actually given to me by an on call DBA. Nice alternative instead of having to shutdown IIS on all my web servers to do a db restore./b]
October 22, 2009 at 9:21 am
anand.ramanan (10/21/2009)
After setting a db to offline it wont be available for a restore...
That is not true. You can restore over a database when it is offline.
December 23, 2009 at 2:34 am
There is another option that you can use to kill all active connection the database and make database read only
ALTER DATABASE <mydb> SET READ_ONLY WITH ROLLBACK IMMEDIATE
Abhijit - http://abhijitmore.wordpress.com
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply