常看到死鎖的問題,一般都是kill進(jìn)程,但如果不查出引起死鎖的原因,死鎖會(huì)時(shí)常發(fā)生 
可以通過查找引起死鎖的的操作,就可以方便的解決死鎖,現(xiàn)將日常解決問題的方法總結(jié),也許對(duì)大家有幫助 
1/死鎖發(fā)生時(shí),通過如下語(yǔ)法,查詢出引起死鎖的操作
use master 
go 
declare @spid int,@bl int 
declare s_cur cursor for  
select  0 ,blocked 
from (select * from sysprocesses where  blocked>0 ) a  
where not exists(select * from (select * from sysprocesses where  blocked>0 ) b  
where a.blocked=spid) 
union select spid,blocked from sysprocesses where  blocked>0 
open s_cur 
fetch next from s_cur into @spid,@bl 
while @@fetch_status = 0 
begin 
if @spid =0  
            select '引起數(shù)據(jù)庫(kù)死鎖的是: '+ cast(@bl as varchar(10)) + '進(jìn)程號(hào),其執(zhí)行的sql語(yǔ)法如下' 
else 
            select '進(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語(yǔ)法如下' 
dbcc inputbuffer (@bl ) 
fetch next from s_cur into @spid,@bl 
end 
close s_cur 
deallocate s_cur 
exec sp_who2 
2/查找程序/數(shù)據(jù)庫(kù),此t_sql語(yǔ)法在什么地方使用 
3/分析找到的,并解決問題 
eg:
/* 
-------------------------------------------------------  
引起數(shù)據(jù)庫(kù)死鎖的是: 71進(jìn)程號(hào),其執(zhí)行的sql語(yǔ)法如下 
eventtype      parameters eventinfo                                         
-------------- ---------- ------------------------------------------------  
language event 0           
select * from test 
insert test values(1,2) 
(所影響的行數(shù)為 1 行) 
dbcc 執(zhí)行完畢。如果 dbcc 輸出了錯(cuò)誤信息,請(qǐng)與系統(tǒng)管理員聯(lián)系。 
                                                                                
------------------------------------------------------------------------------  
進(jìn)程號(hào)spid:64被進(jìn)程號(hào)spid:71阻塞,其當(dāng)前進(jìn)程執(zhí)行的sql語(yǔ)法如下 
eventtype      parameters eventinfo                                         
-------------- ---------- ------------------------------------------------  
language event 0           
select * from test 
insert test values(1,2) 
(所影響的行數(shù)為 1 行) 
dbcc 執(zhí)行完畢。如果 dbcc 輸出了錯(cuò)誤信息,請(qǐng)與系統(tǒng)管理員聯(lián)系。 
                                                                                
------------------------------------------------------------------------------  
進(jìn)程號(hào)spid:65被進(jìn)程號(hào)spid:64阻塞,其當(dāng)前進(jìn)程執(zhí)行的sql語(yǔ)法如下 
eventtype      parameters eventinfo                                                                                           
-------------- ---------- --------------------------------------------------------------------------------------------------  
language event 0          begin tran 
select * from test with (holdlock) 
waitfor time '12:00' 
select * from test 
commit 
(所影響的行數(shù)為 1 行)
dbcc 執(zhí)行完畢。如果 dbcc 輸出了錯(cuò)誤信息,請(qǐng)與系統(tǒng)管理員聯(lián)系。 
                                                                                
------------------------------------------------------------------------------  
進(jìn)程號(hào)spid:73被進(jìn)程號(hào)spid:64阻塞,其當(dāng)前進(jìn)程執(zhí)行的sql語(yǔ)法如下 
eventtype      parameters eventinfo                                                                                           
-------------- ---------- --------------------------------------------------------------------------------------------------  
language event 0          begin tran 
select * from test with (holdlock) 
waitfor time '12:00' 
select * from test 
commit 
(所影響的行數(shù)為 1 行)
dbcc 執(zhí)行完畢。如果 dbcc 輸出了錯(cuò)誤信息,請(qǐng)與系統(tǒng)管理員聯(lián)系。 
*/ 
新聞熱點(diǎn)
疑難解答
圖片精選