Skip to main content

Resolving the “Exclusive Access Could Not Be Obtained” Error During SQL Server Database Restore

Resolving “Exclusive Access Could Not Be Obtained” Error in SQL Server

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:

  1. Open the Restore Database window.
  2. Go to the Options tab.
  3. 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

Popular posts from this blog

Fixing the “Can’t Stop DynamicsAxBatch” Error During Admin User Provisioning in Dynamics 365 F&O

 If you're working with Microsoft Dynamics 365 for Finance and Operations (D365 F&O) and encounter the dreaded error “Can’t stop DynamicsAxBatch” while using the Admin User Provisioning Tool , you're not alone. This guide walks you through a quick and effective solution to resolve the issue and get back to work without delays. 🛠️ What Is the Admin User Provisioning Tool? The Admin User Provisioning Tool is used to register a user as an administrator on a D365 F&O instance—typically for development and customization purposes. You provide your Azure AD credentials (email address) to gain admin access. ❌ The Problem: “Can’t Stop DynamicsAxBatch” Error While registering via the Admin Provisioning Tool or attempting to stop the service manually through Windows Services , you might encounter an error stating: “Can’t stop DynamicsAxBatch” This error prevents you from proceeding with the registration process.  The Solution: Step-by-Step Fix  Step 1: Try Stoppi...

How to get batch header and recurrence information in service class for sysoperation frame work in D365 F&O

  BatchHeader batchHeader         = this.getCurrentBatchHeader();         int         timeUnitsSelected   = 0;         if(this.isExecutingInBatch())         {             RefRecId batchRecId = batchHeader.parmBatchHeaderId();             BatchJob batchJob;             select firstonly batchJob             where batchJob.RecId == batchRecId;                      SysRecurrenceData recurrenceData = batchjob.recurrenceData;             SysRecurrenceUnit timeUnits = conPeek(recurrenceData, 7);             timeUnitsSelected = timeUnits;         }

Table Keys: Surrogate, Alternate, Replacement, Primary, and Foreign

Primary key A primary key is one type of key. The other type of key is an alternate key. There is a maximum of one primary key per table , whereas a table can have several alternate keys . The primary key is usually the type of key that other tables, called child tables, refer to when a foreign key field in those other tables need a relational identifier. Starting in Microsoft Dynamics AX 2012 the primary key for every new table is always enforced by an index that has exactly one field. The one field is usually an incremented number or a completely meaningless number that is generated by the system. For new tables the default is a primary key based on the RecId field. This is represented as the surrogate key in the user interface. The following table describes the  PrimaryIndex   property and other major properties that are related to keys. Property Description PrimaryIndex The drop-down list contains the surrogate key plus every index on the table that has its  Alternate...