MSSQL锁定的死锁与阻塞
控件中国网提示:本文介绍MSSQL锁定的死锁与阻塞。控件中国网提供众多数据库相关控件供开发者选择。
死锁
--Cycle死锁--1.在Session1运行以下begin tran update Sales.Customer set TerritoryID =2 where CustomerID = 1--2.在Session2运行以下begin tran update Sales.Customer set TerritoryID =1 where CustomerID = 2--3.回到Session1运行以下update Sales.Customer set TerritoryID =1 where CustomerID = 2--4.回到Session2运行以下update Sales.Customer set TerritoryID =2 where CustomerID = 1--Conversion死锁--在Session 1,2同时运行下边代码Set Transaction Isolation Level Repeatable Readbegin tran select * from Sales.Customer where CustomerID = 2 WaitFor Delay '00:00:03' update Sales.Customer set TerritoryID =convert(nvarchar,2) where CustomerID = 1Commit Transelect * from Sales.Customer where CustomerID = 2--分布式死锁--SQL Server无法侦测的死锁USE TempdbCREATE TABLE tbTestTrigSSIS(ID INT IDENTITY(1,1), Done BIT DEFAULT 0)GOEXEC sp_configure 'show advanced options',1RECONFIGURE--测试完毕后要改回来,免得留下安全漏洞EXEC sp_configure 'xp_cmdshell',1RECONFIGUREGOcreate TRIGGER trgForInsert ON tbTestTrigSSISFOR INSERTAS--先完成交易,再执行其他的工作COMMIT TRANEXEC master.dbo.xp_cmdshell 'dtexec /FILE "E:\SSISDeadLock\Package.dtsx"'BEGIN TRANGOInsert tbTestTrigSSIS default values
阻塞与检测
制造阻塞
--Session 1
begin tran
update Sales.Customer set TerritoryID =2 where CustomerID = 1
--Session 2
select * from Sales.Customer where CustomerID = 1
查看阻塞进程
-------------------------------------------------------列出最初锁住资源,导致一连串其他进程被锁住的起始源头-----------------------------------------------------IF EXISTS(SELECT * FROM master.sys.sysprocesses WHERE spid IN (SELECT blocked FROM master.sys.sysprocesses)) --确定有进程被其他的进程锁住 SELECT spid 进程,status 状态, 登入帐号=SUBSTRING(SUSER_SNAME(sid),1,30), 使用者机器名称=SUBSTRING(hostname,1,12), 是否被锁住=CONVERT(char(3),blocked), 数据库名称 = SUBSTRING(DB_NAME(dbid),1,20),cmd 命令,waittype 等待型态 FROM master.sys.sysprocesses --列出锁住别人(在别的进程中 blocked字段出现的值),但自己未被锁住(blocked=0) WHERE spid IN (SELECT blocked FROM master.sys.sysprocesses) AND blocked=0ELSE SELECT 'No Blocked Session(s)'
经常出现的是,在sysprocesses视图中 status是'sleeping',waittype字段是0x0000,打开事务数open_tran大于0,一般都是交易已经激活但迟迟没有结束,就可能是程序没有管理好交易管理
select * from master.sys.sysprocesses where status = 'sleeping' and waittype=0x0000 and open_tran > 0
05可以通过sys.dm_tran_locks视图查看,每一笔记录都代表已经授予锁定,注意资源resource和请求request,sys.dm_os_waiting_tasks视图提供时间统计谁锁定谁sys.dm_exec_requests视图以及sys.dm_exec_sql_text(r.sql_handle)函数
select t1.resource_type as [资源锁定类型] ,db_name(resource_database_id) as [数据库名] ,t1.resource_associated_entity_id as [锁定的对象] ,t1.request_mode as [等待者需求的锁定类型] ,t1.request_session_id as [等待者sid] ,t2.wait_duration_ms as [等待时间] ,(select text from sys.dm_exec_requests as r cross apply sys.dm_exec_sql_text(r.sql_handle) where r.session_id = t1.request_session_id) as [等待者要执行的批次] ,(select substring(qt.text,r.statement_start_offset/2+1, (case when r.statement_end_offset = -1 then datalength(qt.text) else r.statement_end_offset end - r.statement_start_offset)/2+1) from sys.dm_exec_requests as r cross apply sys.dm_exec_sql_text(r.sql_handle) as qt where r.session_id = t1.request_session_id) as [等待者正要执行的语法] ,t2.blocking_session_id as [锁定者sid] ,(select text from sys.sysprocesses as p cross apply sys.dm_exec_sql_text(p.sql_handle) where p.spid = t2.blocking_session_id) as [锁定者的语法] from sys.dm_tran_locks as t1, sys.dm_os_waiting_tasks as t2where t1.lock_owner_address = t2.resource_address
研究下sp_who2和sp_lock存储过程
--Session 1use adventureworksgoBEGIN TRANSACTION --启动交易,执行修改陈述式 UPDATE HumanResources.Employee SET ManagerID=4 WHERE EmployeeID=2--Session 2use adventureworksgoSELECT * FROM HumanResources.EmployeeWHERE EmployeeID=2GOexec sp_lock 55select db_name(5) 'db',object_name(869578136) 'object',(select name from sys.indexes where object_id=869578136 and index_id=5) 'index_name'--index_id是根据sp_lock的IndId字段DBCC TraceOn(3604)DBCC Page (5,1,1731,3) --5是DBID,1是集群索引,大于1是非集群,1731是根据sp_lock的Resource字段