Quantcast
Channel: SQL Archives - SQL Authority with Pinal Dave
Viewing all articles
Browse latest Browse all 594

SQL SERVER – FIX: Error 1807 Could not obtain exclusive lock on database ‘model’. Retry the operation later – Part 2

$
0
0

I have always mentioned in the blogs that the most sought out blogs would be around troubleshooting or around specific error messages. These blogs are the biggest source of page views from various search engines. I love getting these error messages and an opportunity for me to learn something new. Here is my first blog written in the year 2010 and I still get questions around the blog via emails. FIX: Error: 1807 Could not obtain exclusive lock on database ‘model’. Retry the operation later. Let us learn to fix error 1807 in this blog post.

The blog is still relevant in SQL Server 2016 and the basics don’t change much between versions. Here is the error 1807.

Msg 1807, Level 16, State 3, Line 1
Could not obtain exclusive lock on database ‘model’. Retry the operation later.
Msg 1802, Level 16, State 4, Line 1
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

SQL SERVER - FIX: Error 1807 Could not obtain exclusive lock on database 'model'. Retry the operation later – Part 2 model-in-use-01-800x169

There is already a connect item on Microsoft site that you can use for reference.

And as per Microsoft:

The exclusive lock on the model database is a necessary step to create a new database. When SQL Server creates a new database, “The SQL Server uses a copy of the model database to initialize the database and its metadata”. Also, users could create, modify, drop objects in the Model database. As such, it is important to exclusively lock the model database to prevent copying the data in change from the Model database. Otherwise, there is no guarantee that the content copied from the Model database are consistent and valid.

Why I am Writing Part 2 for Error 1807?

I realized that the solution given in an earlier blog is not working for most of my readers. Earlier solution was “Disconnect and Reconnect your SQL Server Management Studio’s session.”

If there is someone else grabbing the connection to model database, then above would not work. So, we need to do below:

  1. Find who is having connection? Below query can help in that.
IF EXISTS (
		SELECT request_session_id
		FROM sys.dm_tran_locks
		WHERE resource_database_id = DB_ID('model')
		)
BEGIN
	PRINT 'Model Database in use!!'

	SELECT *
	FROM sys.dm_exec_sessions
	WHERE session_id IN (
			SELECT request_session_id
			FROM sys.dm_tran_locks
			WHERE resource_database_id = DB_ID('model')
			)
END
ELSE
	PRINT 'Model Database not in used.'
  1. Kill the connection. Below query would provide KILL command which we can run to kill ALL connections which are using model database.
SELECT 'KILL ' + CONVERT(varchar(10), l.request_session_id)
 FROM sys.databases d, sys.dm_tran_locks l
 WHERE d.database_id = l.resource_database_id
 AND d.name = 'model'

Now, I feel the answer is more complete and this error can be easily mitigated. Do let me know if you ever encountered this error in your environments. More you share, more we learn together.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

The post SQL SERVER – FIX: Error 1807 Could not obtain exclusive lock on database ‘model’. Retry the operation later – Part 2 appeared first on Journey to SQL Authority with Pinal Dave.


Viewing all articles
Browse latest Browse all 594

Trending Articles