Resolving the “Exclusive Access Could Not Be Obtained” Error in SQL Server
When attempting to restore a database in Microsoft SQL Server Management Studio (SSMS), you might encounter this error:
Exclusive access could not be obtained because the database is in use.
Full error message:
Restore of database 'AxDB' failed.
Microsoft.Data.SqlClient.SqlError: Exclusive access could not be obtained because the database is in use.
This occurs when the database is still being accessed by other sessions, preventing the restore operation. Below are several effective approaches to fix the issue.
1️⃣ Close Existing Connections in SSMS
You can close existing database connections directly from SSMS:
- Open the Restore Database window.
- Go to the Options tab.
- Check: Close existing connections to destination database.
2️⃣ Set Database to SINGLE_USER Mode
This disconnects all existing sessions immediately.
ALTER DATABASE [AxDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
After the restore, set it back:
ALTER DATABASE [AxDB] SET MULTI_USER;
Note: This is fast but force-disconnects users.
3️⃣ Take the Database Offline & Restore
This method works when other approaches fail due to background connections.
ALTER DATABASE [AxDB] SET OFFLINE WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE [AxDB] FROM DISK = 'path_to_backup_file';
ALTER DATABASE [AxDB] SET ONLINE;
Advantage: Ensures no hidden sessions block the restore.
4️⃣ Restart SQL Server Service (Last Resort)
Restarting SQL Server disconnects all users and sessions across the instance. This should only be used when:
- It’s a development/test environment, not production.
- You have approval for downtime.
✅ Summary
The “Exclusive Access Could Not Be Obtained” error happens due to active sessions connected to the target database. Using methods like SINGLE_USER mode, taking the DB offline, or closing connections usually resolves the issue.
By following these steps, you can quickly regain exclusive access and complete the restore successfully.
Comments
Post a Comment