Fix SQL Server Error 3101 (Exclusive Access Could Not Be Obtained)

8 min read

SQL Server error 3101 stops a restore because other connections still use the database. Msg 3101 comes with Msg 3013 so the restore ends before it finishes.

Microsoft shows this exact message on its SSMS restore page. This guide finds the sessions that block the restore and gives four fixes from the safest to the most forceful.

Quick SummarySQL Server error 3101 means a restore could not get exclusive access to the database. Another session still uses the database and your own query window counts as a session. Query sys.dm_exec_sessions to find the sessions and close them. Select Close existing connections in the SSMS restore dialog or set single user mode in a script. Restore under a new name when the sessions must remain connected. Take a tail log backup first when the database uses the full recovery model.

What Does Error 3101 Say and Why Does It Stop a Restore?

Error 3101 says the restore could not get exclusive access because the database is in use. Error 3013 follows as the generic message that the restore is ending abnormally.

Msg 3101, Level 16, State 1, Line 1
Exclusive access could not be obtained because the database is in use.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

According to Microsoft documentation for the SSMS restore dialog, you can see this message when you skip the Close existing connections option. Microsoft states that an offline restore forces users off after a short delay. Practitioners report that error 3101 still appears when one session remains attached to the database.

Do You Know?Your own query window can cause error 3101. A window that shows OrdersDB in the database list holds a session inside that database.

Which Cause Matches Your Restore?

Match what you see to the first column and start with the method in the last column.

What you see Likely cause Start with
The session query lists other logins or applications Users and applications remain connected Method 1
Only your own session appears Your query window or Object Explorer sits in the database Method 1
You restore with the SSMS dialog The dialog leaves existing connections open Method 2
You restore with a script The script never changes the access mode Method 3
Applications reconnect at once A new session starts before the restore begins Method 3
The sessions cannot be closed The live database must remain open Method 4

How to Fix SQL Server Error 3101?

Every example uses the database OrdersDB and the backup OrdersDB.bak. Replace the names and the paths with your own values.

Method 1: Find and Close the Sessions That Use the Database

Use this method first because it shows who holds the database. Microsoft documents the columns of sys.dm_exec_sessions. The database_id column shows the current database of every session.

SELECT session_id, login_name, host_name, program_name, status, last_request_end_time
FROM sys.dm_exec_sessions
WHERE database_id = DB_ID(N'OrdersDB')
  AND is_user_process = 1
  AND session_id <> @@SPID;

Microsoft states that you see all sessions only with VIEW SERVER STATE on SQL Server 2019 and earlier. SQL Server 2022 and later need VIEW SERVER PERFORMANCE STATE. Without the permission the query shows your own session only.

Close the sessions in the applications first. End a session from SQL Server only after you check what it does.

KILL 57;

Replace 57 with the session_id from the query. The session rolls back its open transactions before it ends. Switch your own window to master with USE master before you start the restore.

Method 2: Select Close Existing Connections in SSMS

Use this method when you restore with the dialog. Microsoft states that the option sets the database to single user mode before the restore and back to multi user mode when the restore ends.

  1. Right click Databases in Object Explorer and select Restore Database.
  2. Select the backup file on the General page.
  3. Open the Options page and tick Close existing connections to destination database.
  4. Tick Take tail-log backup before restore when the database uses the full recovery model.
  5. Click OK.

Tick Overwrite the existing database (WITH REPLACE) only when SQL Server refuses to restore over the database. Microsoft warns that REPLACE overrides important safety checks.

Method 3: Set Single User Mode in the Restore Script

Use this method when you restore with T-SQL or when applications reconnect at once. Start the script from master so your own session remains outside the database.

USE master;
GO
ALTER DATABASE OrdersDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE OrdersDB
FROM DISK = N'D:\Backup\OrdersDB.bak'
WITH RECOVERY;
GO
ALTER DATABASE OrdersDB SET MULTI_USER;
GO

Microsoft states that ALTER DATABASE waits for all users to disconnect unless you add a termination clause. ROLLBACK IMMEDIATE ends the open transactions at once. Execute the last statement when the restore fails and the database remains in single user mode.

SELECT name, user_access_desc FROM sys.databases WHERE name = N'OrdersDB';

Public reports show error 5064 when another session grabs the single connection before your restore starts. Execute the ALTER DATABASE and the RESTORE in one script to shorten the gap. Microsoft adds that AUTO_UPDATE_STATISTICS_ASYNC must be OFF before single user mode because the background statistics thread holds a connection.

Method 4: Restore Under a New Name

Use this method when the sessions must remain connected. A new name needs no exclusive access to the existing database so the live database works as before.

RESTORE FILELISTONLY FROM DISK = N'D:\Backup\OrdersDB.bak';

RESTORE DATABASE OrdersDB_Copy
FROM DISK = N'D:\Backup\OrdersDB.bak'
WITH MOVE N'OrdersDB' TO N'D:\Data\OrdersDB_Copy.mdf',
     MOVE N'OrdersDB_log' TO N'D:\Data\OrdersDB_Copy_log.ldf',
     RECOVERY;

RESTORE FILELISTONLY shows the logical file names for the MOVE clauses. A wrong path or a missing folder permission causes another failure. Fix SQL Server error 3156 covers it. Point the application to OrdersDB_Copy after the check.

Heads UpMicrosoft warns that restoring over a database in the full or bulk logged model without a tail log backup can lose committed work. Fix SQL Server error 3159 shows how to take the tail log backup. Never use these methods on master, model or msdb because Microsoft documents a special restore procedure for the system databases.

Quick Answers on Error 3101

Does Error 3101 Mean the Backup Is Damaged?

No. Error 3101 reports open connections on the database. A damaged backup shows other errors, like error 3241 or error 3203.

Why Does Error 3101 Appear When Nobody Else Uses the Database?

Your own query window can hold a session in the database. Switch every open window to master and execute the session query from Method 1 to confirm.

Is KILL Safe for a Session That Uses the Database?

The session rolls back its open transactions before it ends. A large transaction needs time to roll back. Check the session before you use KILL.

Does ROLLBACK IMMEDIATE Delete Data From the Database?

No. ROLLBACK IMMEDIATE ends the incomplete transactions of the connected users. The restore then replaces the data in the database with the data from the backup.

Can You Fix Error 3101 for the master Database?

No. Microsoft documents a special procedure for a damaged master database. Read the Microsoft page on backup and restore of system databases before you start.

How to Avoid Error 3101 on the Next Restore?

Write the restore as one script that starts in master and sets single user mode. Stop the application connection pool before a planned restore.

Test the script on a copy first. A tested script saves time when a restore fails. Restore database with NORECOVERY shows the next step when you restore a chain of backups.

Who, How and WhyAndrew Jackson, Technical Content Specialist, wrote this guide. The error text, the SSMS option, the single user rules and the session columns follow the Microsoft Learn pages for restoring a backup with SSMS, ALTER DATABASE SET options, RESTORE statements and sys.dm_exec_sessions. The T-SQL was checked for syntax against Microsoft documentation and was not executed on a live server. The cause of error 3101 in practice and the text of error 5064 come from public practitioner reports. The guide exists because error 3101 looks the same when an application holds the database and when your own window holds it. The two cases need different fixes.

Key Takeaways

  • SQL Server error 3101 means a restore could not get exclusive access to the database.
  • Your own query window counts as a session in the database.
  • sys.dm_exec_sessions shows every session that uses the database.
  • Close existing connections in SSMS sets single user mode for you.
  • Microsoft warns that a restore over a full recovery database without a tail log backup can lose committed work.
  • Restoring under a new name avoids the conflict with live sessions.