Avamar: SQL restore failing with "Database is already open and can only have one user at a time"
Summary: Knowledge base (KB) article that provides a solution to SQL database restore failure when users access the database.
Symptoms
SQL database restore to the original location is failing with errors similar to the following display:
avsql Error <40258>: sqlconnectimpl_smo::get_database_data_names Microsoft.SqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-SQL statement or batch. ---> System.Data.SqlClient.SqlException: Database 'Avamar1' is already open and can only have one user at a time.
Cause
One or more users or connections are accessing the database that is being restored to. The original database cannot be overwritten if there are users or connections accessing the database.
Resolution
Change the status of the database from MultiUser to Single-User mode.
Using SQL Server Management Studio
To set a database to single-user mode:
-
In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
-
Right-click the database to change, and then click Properties.
-
In the Database Properties dialog box, click the Options page.
-
From the Restrict Access option, select Single.
-
If other users are connected to the database, an Open Connections message appears. To change the property and close all other connections, click Yes.
Using Transact-SQL
To set a database to single-user mode:
-
Connect to the Database Engine.
-
From the Standard bar, click New Query.
-
Copy and paste the following example into the query window and click the "Execute" button. This example sets the database to
SINGLE_USERmode to obtain exclusive access. The example then sets the state of the Avamar1 database toREAD_ONLYand returns access to the database to all users. The termination optionWITH ROLLBACK IMMEDIATEis specified in the firstALTER DATABASEstatement. This causes all incomplete transactions to be rolled back and any other connections to the Avamar1 database to be immediately disconnected.
SQLCopy
USE master; GO ALTER DATABASE Avamar1 SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO ALTER DATABASE Avamar1 SET READ_ONLY; GO ALTER DATABASE Avamar1 SET MULTI_USER; GO
Reference: https://docs.microsoft.com/en-us/sql/relational-databases/databases/set-a-database-to-single-user-mode?view=sql-server-2017
Once that is done, retry the SQL database restore to the original location.