sql server 2000阻塞和死鎖問題的查看與解決方法
數(shù)據(jù)庫發(fā)生阻塞和死鎖的現(xiàn)象:
一、數(shù)據(jù)庫阻塞的現(xiàn)象:第一個連接占有資源沒有釋放,而第二個連接需要獲取這個資源。如果第一個連接沒有提交或者回滾,第二個連接會一直等待下去,直到第一個連接釋放該資源為止。對于阻塞,數(shù)據(jù)庫無法處理,所以對數(shù)據(jù)庫操作要及時地提交或者回滾。
二、數(shù)據(jù)庫死鎖的現(xiàn)象:第一個連接占有資源沒有釋放,準(zhǔn)備獲取第二個連接所占用的資源,而第二個連接占有資源沒有釋放,準(zhǔn)備獲取第一個連接所占用的資源。這種互相占有對方需要獲取的資源的現(xiàn)象叫做死鎖。對于死鎖,數(shù)據(jù)庫處理方法:犧牲一個連接,保證另外一個連接成功執(zhí)行。
例如,一個客戶端應(yīng)用程序線程有兩個開放式連接。該線程異步啟動事務(wù)并在第一個連接上發(fā)出查詢。應(yīng)用程序隨后啟動其它事務(wù),在另一個連接上發(fā)出查詢并等待結(jié)果。當(dāng) SQL Server 返回其中一個連接的結(jié)果時,應(yīng)用程序開始處理這些結(jié)果。應(yīng)用程序就這樣處理結(jié)果,直到生成結(jié)果的查詢被另一個連接上執(zhí)行的查詢阻塞而導(dǎo)致再沒有可用的結(jié)果為止。此時第一個連接阻塞,無限期等待處理更多的結(jié)果。第二個連接沒有在鎖上阻塞,但仍試圖將結(jié)果返回給應(yīng)用程序。然而,由于應(yīng)用程序阻塞而在第一個連接上等待結(jié)果,第二個連接的結(jié)果將得不到處理。
下面是查看并處理sql server 2000阻塞和死鎖的方法:
use master --必須在master數(shù)據(jù)庫中創(chuàng)建
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_lockinfo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_lockinfo]
GO
/*--處理死鎖
查看當(dāng)前進(jìn)程,或死鎖進(jìn)程,并能自動殺掉死進(jìn)程
因?yàn)槭轻槍λ梨i的,所以如果有死鎖進(jìn)程,只能查看死鎖進(jìn)程
當(dāng)然,你可以通過參數(shù)控制,不管有沒有死鎖,都只查看死鎖進(jìn)程
*/
/*--調(diào)用示例
exec p_lockinfo
--*/
create proc p_lockinfo
@kill_lock_spid bit=0, --是否殺掉死鎖的進(jìn)程,1 殺掉, 0 僅顯示
@show_spid_if_nolock bit=1 --如果沒有死鎖的進(jìn)程,是否顯示正常進(jìn)程信息,1 顯示,0 不顯示
as
set nocount on
declare @count int,@s nvarchar(1000),@i int
select id=identity(int,1,1),標(biāo)志,
進(jìn)程ID=spid,線程ID=kpid,塊進(jìn)程ID=blocked,數(shù)據(jù)庫ID=dbid,
數(shù)據(jù)庫名=db_name(dbid),用戶ID=uid,用戶名=loginame,累計(jì)CPU時間=cpu,
登陸時間=login_time,打開事務(wù)數(shù)=open_tran, 進(jìn)程狀態(tài)=status,
工作站名=hostname,應(yīng)用程序名=program_name,工作站進(jìn)程ID=hostprocess,
域名=nt_domain,網(wǎng)卡地址=net_address
into #t from(
select 標(biāo)志='死鎖的進(jìn)程',
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 group by blocked
)b on a.spid=b.blocked where a.blocked=0
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=1
from master..sysprocesses a where blocked<>0
)a order by s1,s2
select @count=@@rowcount,@i=1
if @count=0 and @show_spid_if_nolock=1
begin
insert #t
select 標(biāo)志='正常的進(jìn)程',
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
set @count=@@rowcount
end
if @count>0
begin
create table #t1(id int identity(1,1),a nvarchar(30),b Int,EventInfo nvarchar(255))
if @kill_lock_spid=1
begin
declare @spid varchar(10),@標(biāo)志 varchar(10)
while @i<=@count
begin
select @spid=進(jìn)程ID,@標(biāo)志=標(biāo)志 from #t where id=@i
insert #t1 exec('dbcc inputbuffer('+@spid+')')
if @@rowcount=0 insert #t1(a) values(null)
if @標(biāo)志='死鎖的進(jìn)程' exec('kill '+@spid)
set @i=@i+1
end
end
else
while @i<=@count
begin
select @s='dbcc inputbuffer('+cast(進(jìn)程ID as varchar)+')' from #t where id=@i
insert #t1 exec(@s)
if @@rowcount=0 insert #t1(a) values(null)
set @i=@i+1
end
select a.*,進(jìn)程的SQL語句=b.EventInfo
from #t a join #t1 b on a.id=b.id
order by 進(jìn)程ID
end
set nocount off
go
通過的 Transact-SQL 將解決sql server 2000阻塞和死鎖問題
相關(guān)文章
SQLSERVER數(shù)據(jù)庫備份后無法還原的解決辦法
有時候?yàn)榱丝紤]數(shù)據(jù)安全我們都會備份數(shù)據(jù)庫,sqlserver的備份格式一般都是bak結(jié)尾的,但覆蓋時容易出問題,這里簡單介紹下,需要的朋友可以參考下2013-08-08SQL JOIN 連接詳細(xì)介紹及簡單使用實(shí)例
這篇文章主要介紹了SQL JOIN 連接詳細(xì)介紹及簡單使用實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01通過T-SQL語句創(chuàng)建游標(biāo)與實(shí)現(xiàn)數(shù)據(jù)庫加解密功能
這篇文章介紹了通過T-SQL語句創(chuàng)建游標(biāo)與實(shí)現(xiàn)數(shù)據(jù)庫加解密功能的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03sql中 order by 和 group by的區(qū)別
這篇文章主要介紹了sql中 order by 和 group by的區(qū)別的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11當(dāng)恢復(fù)sqlserver bak文件時,原始的用戶無法刪除的解決方法
當(dāng)你從現(xiàn)有的bak文件,恢復(fù)數(shù)據(jù)庫時,如果數(shù)據(jù)庫本身帶有一個用戶:比如用戶叫:DemoUser.2010-06-06SQL Server創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)表的相關(guān)約束實(shí)現(xiàn)方法
這篇文章主要介紹了SQL Server創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)表的相關(guān)約束實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析講述了數(shù)據(jù)庫約束的概念、功能、注意事項(xiàng)與實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11