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.Related pillar: Blocking & Locking

Msg 5030 means another connection has the database open. Find that session before retrying the operation.


What Msg 5030 Says

Msg 5030  ·  Level 16  ·  State 2
The database YourDb could not be exclusively locked to perform the operation.
⚡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, detach and several ALTER DATABASE options all need an exclusive lock. On SQL Server 2025 the message names the database it could not lock; the same sentence without the name, which is how it is usually quoted, is the same error. SET SINGLE_USER is the odd one out: without a termination clause it waits for the lock rather than failing, and WITH NO_WAIT gives up with Msg 5070 instead. The connection in the way is very often your own.

SSMS Messages tab showing Msg 5030, Level 16, State 2: the database zz_db could not be exclusively locked to perform the operation, while a second window sits in the database

Find the Sessions Holding the Database Open

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. 3 answers come up constantly:

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

Check Your Query Window and Object Explorer

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

Microsoft’s reference covers setting a database to single-user mode in full, with one prerequisite worth reading before you get locked out: if AUTO_UPDATE_STATISTICS_ASYNC is on, the background statistics thread takes its own connection to the database and can hold the single slot, so check it is off first.


Perform the Change and Restore Multi-User Access

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.


Recover When Single-User Mode Locks You Out

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.


Avoid Scheduling SINGLE_USER with ROLLBACK IMMEDIATE

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.


Frequently Asked 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.
Which statements raise 5030, and what do the states mean?
Measured on SQL Server 2025 with one other session sitting in the database: ALTER DATABASE ... MODIFY NAME raises 5030 with State 2 and ALTER DATABASE ... COLLATE raises it with State 5, both immediately. SET SINGLE_USER does not raise it: without a termination clause it waits for the other session to leave, and WITH NO_WAIT fails with Msg 5070 instead. DROP DATABASE with a session inside fails with Msg 3702. Same cause every time, different message.

Where To Go Next

If the database still cannot be locked, use these to find or clear the connections, or recover if it is already in single-user mode:

Comments

Leave a Reply

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