Kill Active connections

  • abmore

    Hall of Fame

    Points: 3628

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

  • JP de Jong-202059

    SSCommitted

    Points: 1674

    Hi

    This is quicker:

    alter database [db_name] set single_user with rollback immediate

    go

  • anand.ramanan

    Mr or Mrs. 500

    Points: 587

    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.

  • Chadwick-788357

    Old Hand

    Points: 394

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

  • gorthog

    Ten Centuries

    Points: 1004

    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'

  • tflooder

    Valued Member

    Points: 60

    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.

  • Jason Carlson

    Grasshopper

    Points: 21

    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.

  • Michael Valentine Jones

    SSC Guru

    Points: 64818

    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

  • tflooder

    Valued Member

    Points: 60

    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.

  • anand.ramanan

    Mr or Mrs. 500

    Points: 587

    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]

  • Michael Valentine Jones

    SSC Guru

    Points: 64818

    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.

  • Abhijit More

    SSCertifiable

    Points: 6897

    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 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply