SQL Server-create database failed

本文介绍了解决SQL Server在创建新数据库时遇到的与model数据库锁冲突的问题。通过查询检查model数据库是否被其他会话使用,并提供了解决方案,如释放相关会话锁以成功创建新数据库。

Error messages as below:

Could not obtain exclusive lock on database 'model'. Retry the operation later.

CREATE DATABASE failed. Some file names listed could not be created. Check related errors. (Microsoft SQL Server, Error: 1807)

 

From: https://blogs.msdn.microsoft.com/poojakamath/2014/12/23/could-not-obtain-exclusive-lock-on-database-model-retry-the-operation-later-create-database-failed/

Cause:

By design when we create a database we need exclusive lock on model. 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, and 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.

 

Solutions:

(1)use below query to check is model db is being used by other sessions:

Use master
GO

IF
EXISTS(SELECT request_session_id FROM
sys.dm_tran_locks
WHERE resource_database_id =
DB_ID('Model'))
PRINT
'Model Database being used by some other session'
ELSE
PRINT
'Model Database not used by other session'

(2)you can also check what kind of statements  are using model

SELECT request_session_id FROM

sys.dm_tran_locks WHERE resource_database_id =DB_ID('Model');

DBCC INPUTBUFFER(spid)

(3)kill those spid to release the session and then you can create a new database

kill spid

转载于:https://www.cnblogs.com/roseHLF/p/7216509.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值