数据库死锁查询及处理
生活随笔
收集整理的這篇文章主要介紹了
数据库死锁查询及处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
創建存儲過程sp_who_lock,查詢死鎖的進程
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-- 找到臨時表的記錄數select @intCountProperties = Count(1),@intCounter = 1 from #tmp_lock_whoIF @@ERROR <> 0 RETURN @@ERRORif @intCountProperties=0select '現在沒有阻塞和死鎖信息' as 'message'-- 循環開始while @intCounter = @intCountPropertiesbegin-- 取第一條記錄select @spid = spid,@bl = blfrom #tmp_lock_who where Id = @intCounterbeginselect '進程號SPID:'+ CAST(@spid AS VARCHAR(10))+ '被'+ '進程號SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其當前進程執行的SQL語法如下'DBCC INPUTBUFFER (@bl )end-- 循環指針下移set @intCounter = @intCounter + 1enddrop table #tmp_lock_whoreturn 0 end View Code下面我們自己構建一個死鎖進程:
BEGIN TRANSACTION--開始事務update T_Users set UserName='00000' where UserId='123'WAITFOR DELAY '01:00'; --指定1點執行 View Code執行查詢語句:
select * from T_Users where UserId='123'這時會發現一直在執行查詢。得不到查詢結果,我們執行第一步創建的存儲過程sp_who_lock.得到結果如下:
此時我們只需執行
kill 53然后再執行查詢語句就可以得到結果了。
?
轉載于:https://www.cnblogs.com/XiaoZone/p/9511815.html
總結
以上是生活随笔為你收集整理的数据库死锁查询及处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mui框架中dialog框的实现
- 下一篇: csrf 攻击和防御