patternMinor
SET SINGLE_USER WITH ROLLBACK IMMEDIATE only disconnects sessions when account is a database user
Viewed 0 times
rollbackwithsetuseraccountsingle_userdatabasewhendisconnectsonly
Problem
The following code creates a user that is able to restore a database:
This works fine and the user can restore the database, however if I want to set the database to
Session 1
Session 2
Eventually, session 2 times out with
The following
The
The Microsoft Article for SET SINGLE_USER states
To quickly obtain exclusive access, the code sample uses the
termination option WITH ROLLBACK IMMEDIATE. This will cause all
incomplete transactions to be rolled back and any other connections to
the AdventureWorks2012 database to be immediately disconnected.
and
Requires ALTER permission on the database.
My
CREATE LOGIN RestoreUser WITH PASSWORD = 'MyPassword'
ALTER SERVER ROLE dbcreator ADD MEMBER RestoreUserThis works fine and the user can restore the database, however if I want to set the database to
SINGLE_USER and rollback any existing connections, the restore command is blocked and fails:Session 1
/* SELECT from a table and leave the SSMS window open, leaving a sleeping SPID */
USE AdventureWorks2014
SELECT * FROM Person.PersonSession 2
EXECUTE AS LOGIN = 'RestoreUser'
ALTER DATABASE AdventureWorks2014 SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE AdventureWorks2014 FROM DISK = 'C:\Test\AW14.bak' WITH REPLACE
REVERTEventually, session 2 times out with
Msg 5061, Level 16, State 1, Line 3
ALTER DATABASE failed because a lock could not be placed on database 'AdventureWorks2014'. Try again later.
Msg 5069, Level 16, State 1, Line 3
ALTER DATABASE statement failed.
Msg 3101, Level 16, State 1, Line 5
Exclusive access could not be obtained because the database is in use.
Msg 3013, Level 16, State 1, Line 5
RESTORE DATABASE is terminating abnormally.The following
sp_whoisactive screenshot shows that the command is blocked:The
sp_whoisactive lock report for session 59 shows
The Microsoft Article for SET SINGLE_USER states
To quickly obtain exclusive access, the code sample uses the
termination option WITH ROLLBACK IMMEDIATE. This will cause all
incomplete transactions to be rolled back and any other connections to
the AdventureWorks2012 database to be immediately disconnected.
and
Requires ALTER permission on the database.
My
RestoreUser account has the correct permissions (dbcreator server role gives ALTER ANY DATABASE permissions) but I don't know why the command is blocked, the first quote suggests all other connections would be disconnected.Solution
As Dan Guzman originally noted, the
If the
If there is contention (another connection to the database), there is a check to see if the security principal executing the command can connect to the database.
When this check fails, the
Msg 5061, Level 16, State 1, Line 10
ALTER DATABASE failed because a lock could not be placed on database 'AdventureWorks2017'. Try again later.
Msg 5069, Level 16, State 1, Line 10
ALTER DATABASE statement failed.
The check succeeds when:
I don't know why this check exists, or why it only occurs if there are blocking connections. There might be a good reason, or it might be an inaccurate or obsolete test.
In some ways, it is odd that a session can acquire the single
Note also the same error occurs for the same reason if you try to set the database to
On the other hand, one could argue that
All that aside, the the
sp_WhoIsActive output indicates the RESTORE is blocked, not the ALTER DATABASE command. Even so:If the
SET SINGLE_USER command is uncontended, there is no issue. The connection acquires the single SESSION level shared database lock that prevents anyone else connecting to the database, and life is good.If there is contention (another connection to the database), there is a check to see if the security principal executing the command can connect to the database.
When this check fails, the
ALTER DATABASE command fails and an error message like the following is returned:Msg 5061, Level 16, State 1, Line 10
ALTER DATABASE failed because a lock could not be placed on database 'AdventureWorks2017'. Try again later.
Msg 5069, Level 16, State 1, Line 10
ALTER DATABASE statement failed.
The check succeeds when:
- The principal has
CONNECT ANY DATABASEpermission; or
- The principal has a related user in the database; or
- The guest user in the database has
CONNECTpermission.
I don't know why this check exists, or why it only occurs if there are blocking connections. There might be a good reason, or it might be an inaccurate or obsolete test.
In some ways, it is odd that a session can acquire the single
SESSION shared database lock associated with single user when the principal has no right to connect to the database.Note also the same error occurs for the same reason if you try to set the database to
MULTI_USER when it is already in that state and there are other users connected to the database.On the other hand, one could argue that
ALTER ANY DATABASE does not quite fulfil the requirement you quoted for ALTER permission on the specific database. Having ALTER permission on the database implies a user in that database because that permission can only be assigned to users, not logins. That is quite persuasive, but doesn't explain why it should only be tested when contention occurs.All that aside, the the
ALTER DATABASE command in your example does throw an error, but you don't check for it, or test the database is in single-user mode before continuing with the restore.Context
StackExchange Database Administrators Q#311122, answer score: 4
Revisions (0)
No revisions yet.