Deadlock after setting Single user mode - sql-server

We have some scripts that are executed during the installation of our application.
One of them is used for setting Database Collation, the other one is used for setting compatibility level. We use the following construction for these scripts:
ALTER DATABASE [DB_NAME] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [DB_NAME] SET COMPATIBILITY_LEVEL = DB_COMPATIBILITY_LEVEL
-- ALTER DATABASE [DB_NAME] COLLATE 'Collation Name'
GO
ALTER DATABASE [DB_NAME] SET MULTI_USER
GO
The question is:
Is it possible for some background process to get an access after setting single user mode? In this case our script couldn't be executed. I saw this issue and it seems similar, so I decided to ask a question.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/950c9b85-67f9-4272-8dff-14aa3590bc8a/single-user-mode-session-lost-after-backgound-processes-jump-in?forum=sqldatabaseengine
This issue was related to restoring database.
alter database [test-db] set single_user with rollback immediate; --This sql is run using test-db
use master;restore database [test-db] from database_snapshot = 'snapshot_test-db';
alter database [test-db] set multi_user;
But I don't fully understand the difference between restoring and altering the database in the single user mode.
In the first case will be enough to set the database offline.
Any help will be appreciated.
Thanks in advance.

Related

Why renaming a database is not possible

I am trying to rename a database through SSMS and getting this below error:-
I am not realizing from the additional information
You need to perform such statements as below.
USE master;
GO
ALTER DATABASE MyTestDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE MyTestDatabase MODIFY NAME = MyTestDatabaseCopy ;
GO
ALTER DATABASE MyTestDatabaseCopy SET MULTI_USER
GO
It can be done in SSMS.
1.right click your database name.
2. properties
3. Options.
5. Restrict Access Select single.
then repeat the step to change again into MULTI_USER After renaming has been done.
You need to close all existing connections before to do that..
For this you can
use SP_WHO and Kill all open connections to your database
OR
Take database offline: Right Click -> Tasks -> Take Offline -> Check Drop All Active Connections

Unable to drop database in SQL Server 2012

I have created a sample database earlier and now I want to delete that database, but it is not getting deleted. I searched online but I didn't find any solution which is working.
Using T-SQL, I tried:
USE [Sample]
ALTER DATABASE [Sample]
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE [Sample]
Using the GUI, I am getting this below error:
I closed existing connection then also this is happening and this is my local machine. Please help me here!
use this code:
USE MASTER
GO
ALTER DATABASE Sample
SET multi_user WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE Sample
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
DROP DATABASE Sample
GO
Close your SSMS , open a new instance and then try the following, Copy paste the whole thing into a new query window and execute at once:
USE [master];
GO
ALTER DATABASE [Sample] --<-- go back into Multi-user mode
SET MULTI_USER;
GO
ALTER DATABASE [Sample] --<-- Will disconnect everyone 1st
SET SINGLE_USER -- and will leave the database in single user mode
WITH ROLLBACK IMMEDIATE;
GO
USE [Sample]; -- quickly grab that single connection before any
GO -- other process does
USE [master]; -- Connect to master db, connection
GO -- This also means disconnect Sample DB
DROP DATABASE [Sample] -- At this point there should be no active connections
GO -- to this database and can be dropped
Another way to delete the Database (without coding)
In Microsoft SQL Server Management Studio, Right-click the Database that you want to delete (In your case, Sample Database) and then Select Properties.
Select the 'Options' Page.
Under the other option, Find the 'Restrict user' option under 'State'.
Change it value MULTI_USER to SINGLE_USER
Then Press Okay and Try again (Or right-click the database again and click Delete)
Use this Code should help.
ALTER DATABASE [dbname]
SET SINGLE_USER --or RESTRICTED_USER
WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE [dbname];
GO

SQL Server 2012 unable to turn off Snapshot Isolation

Using the code below I'm unable to turn off snapshot isolation. I'm using a SQL Server 2012 box. I can create brand new empty db, turn snapshot isolation on, but I can't turn it back off.
The "allow_snapshot_isolation OFF" line just spins it's wheels.
ALTER DATABASE SNAP SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE SNAP SET allow_snapshot_isolation OFF
ALTER DATABASE SNAP SET read_committed_snapshot off
ALTER DATABASE SNAP SET MULTI_USER
Are you sure that no other transactions were ran on the database? Remeber about implicit transactions which are used eg. by JDBC drivers (when you setAutocommit to false).
Snapshot isolation cannot be turn off if any of the previous transactions is pending. This is, because it has to be sure that any other transaction will not try to use previous row versions. However, it is possible to make queries spanning through more than one database so setting snapshot isolation is not only taking care of transaction on one database.
You can check if this is the case by using sp_who2 and SELECT * FROM sys.sysprocessesand searching for your altering process. sp_who2 will be showing that the process is suspended and by using sys.sysprocesses you will probably find out that its lastwaiting type is 'DISABLE_VERSIONING'.
So your solution is to rollback all transactions (or close conections in case of implicit transactions).
Be awared, that if you are using a connection pooling from some program, you may have, for example, 40 implicit transactions opened in two or three dbs. If they were opened after the snapshot mode had been turned on then turning it off will be impossible until they end.
I followed below for the same issue I was getting
USE master;
GO
ALTER DATABASE MYDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE MYDB MODIFY NAME = MYDB_1;
GO
ALTER DATABASE MYDB_1 SET READ_COMMITTED_SNAPSHOT OFF;
GO
ALTER DATABASE MYDB_1 MODIFY NAME = MYDB;
GO
ALTER DATABASE MYDB SET MULTI_USER;
I ran the MSSQL query below to turn off ALLOW_SNAPSHOT_ISOLATION for master database:
ALTER DATABASE master SET ALLOW_SNAPSHOT_ISOLATION OFF;
GO;
Then, I got this error below:
SNAPSHOT ISOLATION is always enabled in this database.
And, I ran the MSSQL query below to turn on READ_COMMITTED_SNAPSHOT for master database:
ALTER DATABASE master SET READ_COMMITTED_SNAPSHOT ON;
GO;
Then, I got this error below:
Option 'READ_COMMITTED_SNAPSHOT' cannot be set in database 'master'.
Because the documentation says below:
The ALLOW_SNAPSHOT_ISOLATION option is automatically set ON in the
master and msdb databases, and cannot be disabled.
Users cannot set the READ_COMMITTED_SNAPSHOT option ON in master,
tempdb, or msdb.

How can I set ALLOW_SNAPSHOT_ISOLATION ON on SQL Server without restarting the DB server?

I'm trying to run the following statement on a SQL server database:
ALTER DATABASE myDB
SET READ_COMMITTED_SNAPSHOT ON
Query Analyzer just keeps spinning and nothing returns. I've read about other people experiencing this problem and that it should normally return fairly quickly, and the only way they've found around it is to restart the SQL Server service. Unfortunately, I don't have that option. Is there any way to get this statement to run without restarting the service?
Looks like my search parameters weren't too good, came up with an answer now. The code below seems to do the trick:
ALTER DATABASE myDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE myDB SET READ_COMMITTED_SNAPSHOT ON;
ALTER DATABASE myDB SET MULTI_USER;

How programmatically enable READ COMMITTED SNAPSHOT in SQL Server?

I need to programmatically enable READ COMMITTED SNAPSHOT in SQL Server. How can I do that?
I recommend switching to single-user mode first. That ensures you're the only connection. Otherwise, the query might be suspended.
From: http://msdn.microsoft.com/en-us/library/ms175095.aspx
When setting the
READ_COMMITTED_SNAPSHOT option, only
the connection executing the ALTER
DATABASE command is allowed in the
database. There must be no other open
connection in the database until ALTER
DATABASE is complete.
So, use this SQL:
ALTER DATABASE <dbname> SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE <dbname> SET READ_COMMITTED_SNAPSHOT ON;
ALTER DATABASE <dbname> SET MULTI_USER;
ALTER DATABASE [dbname] SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK AFTER 20 SECONDS

Resources