The Database Could Not Be Exclusively Locked to Perform the Operation (Error 5030)

🚨Part of the SQL Server Errors series, the exact messages and what actually causes them.

Msg 5030  ·  Level 16  ·  State 2
The database could not be exclusively locked to perform the operation. Msg 5030, Level 16, State 2
Never end a session with the database still in SINGLE_USER. An application pool takes the one available slot within seconds, and then you are locked out of your own database with no way in except killing the holder.

Something is connected to the database, and the operation you asked for needs everyone out. Renames, collation changes, SET SINGLE_USER, detach and several ALTER DATABASE options all need an exclusive lock. The connection in the way is very often your own.


See Who Is Actually In There

SELECT  s.session_id,
        s.login_name,
        s.host_name,
        s.program_name,
        s.status,
        s.last_request_end_time
FROM    sys.dm_exec_sessions AS s
WHERE   s.database_id = DB_ID(N'YourDb')
ORDER   BY s.last_request_end_time;

Read the program_name column before you kill anything. Three answers come up constantly:

program_nameWhat it really is
Microsoft SQL Server Management StudioA query window someone left open, possibly yours
Microsoft SQL Server Management Studio - QueryObject Explorer, holding its own connection
Your application’s nameA live connection pool, which will reconnect the moment you clear it

The Two Traps

Your own window counts. If your query window’s context is the database you are altering, you are the blocker. Switch to master first, then run the statement:

USE master;
GO
ALTER DATABASE YourDb SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

Object Explorer counts too, and it reconnects on its own. Expanding the database node in SSMS is enough to take a connection, which is why the operation fails again thirty seconds after you cleared everything.

WITH ROLLBACK IMMEDIATE is what makes this work: it rolls back every open transaction in that database rather than waiting. That is a data-losing action for anyone mid-transaction, so it belongs in a maintenance window, not in a working day.


Then Do the Work, Then Put It Back

USE master;
GO
ALTER DATABASE YourDb SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE YourDb MODIFY NAME = YourDbArchive;
GO
ALTER DATABASE YourDbArchive SET MULTI_USER;

Never leave a database in SINGLE_USER at the end of a session. An application connection pool will take the one available slot within seconds and you will be locked out of your own database.


Locked Out After Setting Single User

It happens, and the recovery is not obvious, because you cannot get in to fix it.

SELECT s.session_id, s.login_name, s.program_name
FROM   sys.dm_exec_sessions AS s
WHERE  s.database_id = DB_ID(N'YourDb');

KILL 57;   -- the session holding the single slot

Run that from master, kill the holder, and immediately run SET MULTI_USER before anything else reconnects. If you are racing an application pool, stop the application or take the database offline rather than losing the race repeatedly.


What Not To Do

Do not put SET SINGLE_USER WITH ROLLBACK IMMEDIATE in a scheduled job as a way of making a maintenance script reliable. It converts other people’s committed work into rolled-back work on a timer, and the job’s own history will not record what it destroyed.


Related Scripts


Common Questions

I closed every window and it still fails.
Object Explorer holds its own connection and reconnects on its own. Expanding the database node is enough to take one. Run the statement from a window whose context is master, and trust sys.dm_exec_sessions rather than the SSMS interface.
Is WITH ROLLBACK IMMEDIATE safe?
It is decisive, not safe. It rolls back every open transaction in that database rather than waiting for them, so it belongs in a maintenance window. Other people’s in-flight work is what gets rolled back.
I set SINGLE_USER and now I cannot get back in.
An application pool has taken the one available connection. Find the holder in sys.dm_exec_sessions from master, KILL it, then run SET MULTI_USER immediately. If you keep losing the race, stop the application or take the database offline.

Related Scripts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *