Monday, May 19, 2014

SQL DB stuck in Single User Mode


I recently ran into an issue with SQL 2012 database. One of the automated process changed database into a single user mode and then failed. Luckily this database was only a QA database so bringing it backup was not a priority. I tried to bring it back by

   1:  ALTER DATABASE [DB_Name] SET MULTI_USER;
   2:  GO




I would get an error

Msg 1205, Level 13, State 68, Server XXXXXXXXXXX, Line 1
Transaction (Process ID XX) was deadlocked on lock resources with another process
and has been chosen as the deadlock victim. Rerun the transaction.
Msg 5069, Level 16, State 1, Server XXXXXXXXXXX, Line 1
ALTER DATABASE statement failed.

I ran sp_whoisactive with @show_sleeping_spids = 2 but couldn’t see which SPID was connected to the database. Then researched on the internet and found that I can trying to use sp_who2 and find the spids. There were too many results to go through. I found this neat script
use master
GO

select 
    d.name, 
    d.dbid, 
    spid, 
    login_time, 
    nt_domain, 
    nt_username, 
    loginame
from sysprocesses p 
    inner join sysdatabases d 
        on p.dbid = d.dbid
where d.name = '[DB_Name]'
GO

This scripts picks up all the sessions connected to the database. I tried to kill one of the process and got

Msg 6107, Level 14, State 1, Line 1
Only user processes can be killed.


So no solution till I ran across this article by Pearl Knows on SQL Server Central http://goo.gl/z7EXUT. Great article.

I was able to use the article to get out of this jam.

I used High Deadlock Priority to get connection.
USE [master]
SET DEADLOCK_PRIORITY HIGH
ALTER DATABASE [DB_Name] SET MULTI_USER WITH NO_WAIT
ALTER DATABASE [DB_Name] SET MULTI_USER WITH ROLLBACK IMMEDIATE



Hopefully this will help you get out of a jam.

Thanks!

3 comments: