Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 371 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When restoring a backup, how do I disconnect all active connections?

#1
My SQL Server 2005 doesn't restore a backup because of active connections. How can I force it?
Reply

#2
Restarting SQL server will disconnect users. Easiest way I've found - good also if you want to take the server offline.

But for some very wierd reason the 'Take Offline' option doesn't do this reliably and can hang or confuse the management console. Restarting then taking offline works

Sometimes this is an option - if for instance you've stopped a webserver that is the source of the connections.
Reply

#3
None of these were working for me, couldn't delete or disconnect current users. Also couldn't see any active connections to the DB. Restarting SQL Server (Right click and select Restart) allowed me to do it.
Reply

#4
This code worked for me, it kills all existing connections of a database.
All you have to do is change the line Set @dbname = 'databaseName' so it has your database name.

Use Master
Go

Declare @dbname sysname

Set @dbname = 'databaseName'

Declare @spid int
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname)
While @spid Is Not Null
Begin
Execute ('Kill ' + @spid)
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname) and spid > @spid
End

after this I was able to restore it
Reply

#5
To add to advice already given, if you have a web app running through IIS that uses the DB, you may also need to *stop* (not recycle) the app pool for the app while you restore, then re-start. Stopping the app pool kills off active http connections and doesn't allow any more, which could otherwise end up allowing processes to be triggered that connect to and thereby lock the database. This is a known issue for example with the Umbraco Content Management System when restoring its database
Reply

#6
You want to set your db to single user mode, do the restore, then set it back to multiuser:

ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK AFTER 60 --this will give your current connections 60 seconds to complete

--Do Actual Restore
RESTORE DATABASE YourDB
FROM DISK = 'D:\BackUp\YourBaackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:\Data\YourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:\Data\YourLDFFile.ldf'

/*If there is no error in statement before database will be in multiuser
mode. If error occurs please execute following command it will convert
database in multi user.*/
ALTER DATABASE YourDB SET MULTI_USER
GO

Reference : Pinal Dave (

[To see links please register here]

)

Official reference:

[To see links please register here]

Reply

#7
None of the above worked for me. My database didn't show any active connections using Activity Monitor or sp_who. I ultimately had to:

- Right click the database node
- Select "Detach..."
- Check the "Drop Connections" box
- Reattach

Not the most elegant solution but it works and it doesn't require restarting SQL Server (not an option for me, since the DB server hosted a bunch of other databases)
Reply

#8
I ran across this problem while automating a restore proccess in SQL Server 2008.
My (successfull) approach was a mix of two of the answers provided.

First, I run across all the connections of said database, and kill them.

DECLARE @SPID int = (SELECT TOP 1 SPID FROM sys.sysprocess WHERE dbid = db_id('dbName'))
While @spid Is Not Null
Begin
Execute ('Kill ' + @spid)
Select @spid = top 1 spid from master.dbo.sysprocesses
where dbid = db_id('dbName')
End

Then, I set the database to a single_user mode

ALTER DATABASE dbName SET SINGLE_USER

Then, I run the restore...

RESTORE DATABASE and whatnot

Kill the connections again

(same query as above)

And set the database back to multi_user.

ALTER DATABASE dbName SET MULTI_USER

This way, I ensure that there are no connections holding up the database before setting to single mode, since the former will freeze if there are.
Reply

#9
### SQL Server Management Studio 2005

When you right click on a database and click `Tasks` and then click `Detach Database`, it brings up a dialog with the active connections.

[![Detach Screen][1]][1]

By clicking on the hyperlink under "Messages" you can kill the active connections.

You can then kill those connections without detaching the database.

More information [here][2].

### SQL Server Management Studio 2008

The interface has changed for SQL Server Management studio 2008, here are the steps (via: [Tim Leung][3])

1. Right-click the server in Object Explorer and select 'Activity Monitor'.
2. When this opens, expand the Processes group.
3. Now use the drop-down to filter the results by database name.
4. Kill off the server connections by selecting the right-click 'Kill Process' option.

[1]:

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#10
I prefer to do like this,

alter database <database> set offline with rollback immediate

and then restore your database.
after that,

alter database <database> set online with rollback immediate
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through