数据库死锁查询及处理
生活随笔
收集整理的這篇文章主要介紹了
数据库死锁查询及处理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
創(chuàng)建存儲(chǔ)過程sp_who_lock,查詢死鎖的進(jìn)程
create procedure sp_who_lock WITH ENCRYPTION as begindeclare @spid int,@bl int,@intTransactionCountOnEntry int,@intRowcount int,@intCountProperties int,@intCounter intcreate table #tmp_lock_who (id int identity(1,1),spid smallint,bl smallint)IF @@ERROR <> 0 RETURN @@ERRORinsert into #tmp_lock_who(spid,bl) select spid,blocked from sysprocesses where blocked <> 0IF @@ERROR <> 0 RETURN @@ERROR-- 找到臨時(shí)表的記錄數(shù)select @intCountProperties = Count(1),@intCounter = 1 from #tmp_lock_whoIF @@ERROR <> 0 RETURN @@ERRORif @intCountProperties=0select '現(xiàn)在沒有阻塞和死鎖信息' as 'message'-- 循環(huán)開始while @intCounter = @intCountPropertiesbegin-- 取第一條記錄select @spid = spid,@bl = blfrom #tmp_lock_who where Id = @intCounterbeginselect '進(jìn)程號(hào)SPID:'+ CAST(@spid AS VARCHAR(10))+ '被'+ '進(jìn)程號(hào)SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其當(dāng)前進(jìn)程執(zhí)行的SQL語法如下'DBCC INPUTBUFFER (@bl )end-- 循環(huán)指針下移set @intCounter = @intCounter + 1enddrop table #tmp_lock_whoreturn 0 end View Code下面我們自己構(gòu)建一個(gè)死鎖進(jìn)程:
BEGIN TRANSACTION--開始事務(wù)update T_Users set UserName='00000' where UserId='123'WAITFOR DELAY '01:00'; --指定1點(diǎn)執(zhí)行 View Code執(zhí)行查詢語句:
select * from T_Users where UserId='123'這時(shí)會(huì)發(fā)現(xiàn)一直在執(zhí)行查詢。得不到查詢結(jié)果,我們執(zhí)行第一步創(chuàng)建的存儲(chǔ)過程sp_who_lock.得到結(jié)果如下:
此時(shí)我們只需執(zhí)行
kill 53然后再執(zhí)行查詢語句就可以得到結(jié)果了。
?
轉(zhuǎn)載于:https://www.cnblogs.com/XiaoZone/p/9511815.html
總結(jié)
以上是生活随笔為你收集整理的数据库死锁查询及处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mui框架中dialog框的实现
- 下一篇: linux cmake编译源码,linu