Kill Active connections

  • Comments posted to this topic are about the item Kill Active connections

  • Hi

    This is quicker:

    alter database [db_name] set single_user with rollback immediate

    go

  • 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.

  • Thanks for the script. It's useful for me.

  • 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'

  • 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.

  • 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.

  • 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

  • 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.

  • 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]

  • 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.

  • 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