Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extreme wait-time when taking a SQL Server database offline

People also ask

Why does it take so long to take a SQL database offline?

It means that some connection is still active, due to some user or client application still accessing the DB. So one way is TO CHECK the drop all active connections in the UI. This will terminate connections, rollback their transactions, and allow only privileged users to connect.

How do I ensure high availability in SQL Server?

High Availability (HA) is the solution\process\technology to make the application\database available 24x7 under either planned or un-planned outages. Mainly, there are five options in MS SQL Server to achieve\setup high availability solution for the databases.

What is the difference between detach and take offline?

The difference is that detach deletes database metadata from SQL Server i.e. database file information, status information and all the other details that we see in sys. databases view. On the other hand taking database offline retains database metadata in SQL server system views.


After some additional searching (new search terms inspired by gbn's answer and u07ch's comment on KMike's answer) I found this, which completed successfully in 2 seconds:

ALTER DATABASE <dbname> SET OFFLINE WITH ROLLBACK IMMEDIATE

(Update)

When this still fails with the following error, you can fix it as inspired by this blog post:

ALTER DATABASE failed because a lock could not be placed on database 'dbname' Try again later.

you can run the following command to find out who is keeping a lock on your database:

EXEC sp_who2

And use whatever SPID you find in the following command:

KILL <SPID>

Then run the ALTER DATABASE command again. It should now work.


There is most likely a connection to the DB from somewhere (a rare example: asynchronous statistic update)

To find connections, use sys.sysprocesses

USE master
SELECT * FROM sys.sysprocesses WHERE dbid = DB_ID('MyDB')

To force disconnections, use ROLLBACK IMMEDIATE

USE master
ALTER DATABASE MyDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE

Do you have any open SQL Server Management Studio windows that are connected to this DB?

Put it in single user mode, and then try again.


In my case, after waiting so much for it to finish I had no patience and simply closed management studio. Before exiting, it showed the success message, db is offline. The files were available to rename.


execute the stored procedure sp_who2

This will allow you to see if there is any blocking locks.. kill their should fix it.


In SSMS: right-click on SQL server icon, Activity Monitor. Open Processes. Find the processed connected. Right-click on the process, Kill.


anytime you run into this type of thing you should always think of your transaction log. The alter db statment with rollback immediate indicates this to be the case. Check this out: http://msdn.microsoft.com/en-us/library/ms189085.aspx

Bone up on checkpoints, etc. You need to decide if the transactions in your log are worth saving or not and then pick the mode to run your db in accordingly. There's really no reason for you to have to wait but also no reason for you to lose data either - you can have both.