找到死鎖的程序並取消該死鎖的儲存過程

2022-04-28 17:09:08 字數 3149 閱讀 1291

--鄒建  

create   proc   sp_lockinfo  

@kill_lock_spid   bit=1,             --是否殺掉阻塞的程序,1   殺掉,   0   僅顯示  

@show_spid_if_nolock   bit=1,   --如果沒有阻塞的程序,是否顯示正常程序資訊,1   顯示,0   不顯示  

@dbname   sysname=''                     --如果為空,則查詢所有的庫,如果為null,則查詢當前庫,否則查詢指定庫  

as  

set   nocount   on  

declare   @count   int,@s   nvarchar(2000),@dbid   int  

if   @dbname=''   set   @dbid=db_id()   else   set   @dbid=db_id(@dbname)  

select   id=identity(int,1,1),標誌,  

程序id=spid,執行緒id=kpid,塊程序id=blocked,資料庫id=dbid,  

資料庫名=db_name(dbid),使用者id=uid,使用者名稱=loginame,累計cpu時間=cpu,  

登陸時間=login_time,開啟事務數=open_tran,程序狀態=status,  

工作站名=hostname,應用程式名=program_name,工作站程序id=hostprocess,  

網域名稱=nt_domain,網絡卡位址=net_address  

into   #t   from(  

select   標誌='阻塞的程序',  

spid,kpid,a.blocked,dbid,uid,loginame,cpu,login_time,open_tran,  

status,hostname,program_name,hostprocess,nt_domain,net_address,  

s1=a.spid,s2=0  

from   master..sysprocesses   a   join   (  

select   blocked   from   master..sysprocesses    

where   blocked>0  

and(@dbid   is   null   or   dbid=@dbid)  

group   by   blocked  

)b   on   a.spid=b.blocked    

where   a.blocked=0  

and(@dbid   is   null   or   dbid=@dbid)  

union   all  

select   '|_犧牲品_>',  

spid,kpid,blocked,dbid,uid,loginame,cpu,login_time,open_tran,  

status,hostname,program_name,hostprocess,nt_domain,net_address,  

s1=blocked,s2=spid  

from   master..sysprocesses   a    

where   blocked<>0  

and(@dbid   is   null   or   dbid=@dbid)  

)a   order   by   s1,s2  

select   @count=@@rowcount  

if   @count=0   and   @show_spid_if_nolock=1  

begin  

insert   #t  

select   標誌='正常的程序',  

spid,kpid,blocked,dbid,db_name(dbid),uid,loginame,cpu,login_time,  

open_tran,status,hostname,program_name,hostprocess,nt_domain,net_address  

from   master..sysprocesses  

where   @dbid   is   null   or   dbid=@dbid  

order   by   spid  

set   @count=@@rowcount  

end  

if   @count>0  

begin  

create   table   #t1(id   int   identity(1,1),a   nvarchar(30),b   int,eventinfo   nvarchar(255))  

declare   tb   cursor   local  

for  

select   n'insert   #t1   exec(''dbcc   inputbuffer('+rtrim(程序id)+')'')  

if   @@rowcount=0   insert   #t1(a)   values(null)  

'+case   when   @kill_lock_spid=1   and   標誌=n'阻塞的程序'  

then   'kill   '+rtrim(程序id)   else   ''   end  

from   #t  

open   tb  

fetch   tb   into   @s  

while   @@fetch_status=0  

begin  

exec(@s)  

fetch   tb   into   @s  

end  

close   tb  

deallocate   tb  

select   a.*,程序的sql語句=b.eventinfo  

from   #t   a   join   #t1   b   on   a.id=b.id  

order   by   a.id  

end  

set   nocount   off  

go  

程序的死鎖

產生死鎖的四個條件同時具備 互斥條件 不可搶占條件 占有且申請條件 迴圈等待條件 為什麼會有死鎖 若干程序競爭 有限資源,又 推進順序不當,從而構成無限迴圈等待的局面,這種狀態叫做死鎖。所謂死鎖是指多個程序迴圈等待它方占有的資源而無限期的僵持下去的局面。死鎖原因 一種原因是系統提供的資源太少,遠不能...

關於面試問到死鎖的問題

什麼是死鎖 就是在多執行緒的執行中,當乙個執行緒需要獲取到鎖時才能繼續往下執行,如果沒有立即獲取到鎖它是會進入等待狀態的。那麼假設如果執行緒1獲持有著a鎖之後它還想獲取b鎖,但是b鎖被執行緒2所持有,所以執行緒1要進入等待狀態。但是剛好執行緒2想獲取a鎖,被執行緒1所持有,那麼執行緒2也進入等待狀態...

程序管理 程序的死鎖

環路等待條件又叫迴圈等待條件 迴圈等待條件,請求和保持條件,互斥條件,不可剝奪條件。只要保證任何時刻產生死鎖的四個條件至少有乙個不成立,就可以起到預防死鎖的目的。即允許程序同時訪問某些資源,這樣就沒有資源的互斥使用問題了。但是,有的資源是不允許被同時訪問的,像印表機等等,這是資源本身的屬性。可以實行...