查询死锁和处理死锁(SqlServer)

-------------------查询死锁,极其引起的原因-------------------------------use master
go
create procedure sp_who_lock
as
begin
declare @spid int,@bl int,
 @intTransactionCountOnEntry  int,
        @intRowcount    int,
        @intCountProperties   int,
        @intCounter    int

 create table #tmp_lock_who (
 id int identity(1,1),
 spid smallint,
 bl smallint)

 IF @@ERROR<>0 RETURN @@ERROR

 insert into #tmp_lock_who(spid,bl) 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

 IF @@ERROR<>0 RETURN @@ERROR 

-- 找到临时表的记录数
 select  @intCountProperties = Count(*),@intCounter = 1
 from #tmp_lock_who

 IF @@ERROR<>0 RETURN @@ERROR 

 if @intCountProperties=0
  select ‘现在没有阻塞和死锁信息‘ as message

-- 循环开始
while @intCounter <= @intCountProperties
begin
-- 取第一条记录
  select  @spid = spid,@bl = bl
  from #tmp_lock_who where Id = @intCounter
 begin
  if @spid =0
            select ‘引起数据库死锁的是: ‘+ CAST(@bl AS VARCHAR(10)) + ‘进程号,其执行的SQL语法如下‘
 else
            select ‘进程号SPID:‘+ CAST(@spid AS VARCHAR(10))+ ‘被‘ + ‘进程号SPID:‘+ CAST(@bl AS VARCHAR(10)) +‘阻塞,其当前进程执行的SQL语法如下‘
 DBCC INPUTBUFFER (@bl )
 end 

-- 循环指针下移
 set @intCounter = @intCounter + 1
end

drop table #tmp_lock_who

return 0
end

------执行语句exec sp_who_lock

---------一次性删除所有的索引-----------use master
go

if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[p_killspid]‘) and OBJECTPROPERTY(id, N‘IsProcedure‘) = 1)
drop procedure [dbo].[p_killspid]
GO

create proc p_killspid
@dbname varchar(200)    --要关闭进程的数据库名
as
    declare @sql  nvarchar(500)
    declare @spid nvarchar(20)

    declare #tb cursor for
        select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
    open #tb
    fetch next from #tb into @spid
    while @@fetch_status=0
    begin
        exec(‘kill ‘+@spid)
        fetch next from #tb into @spid
    end
    close #tb
    deallocate #tb
go

--用法  传入数据库
exec p_killspid  ‘数据库名‘
时间: 2024-08-24 15:04:48

查询死锁和处理死锁(SqlServer)的相关文章

数据库死锁及解决死锁问题

deadlocks(死锁) 所谓死锁<DeadLock>: 是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程.由于资源占用是互斥的,当某个进程提出申请资源后,使得有关进程在无外力协助下,永远分配不到必需的资源而无法继续运行,这就产生了一种特殊现象死锁. 一种情形,此时执行程序中两个或多个线程发生永久堵塞(等待),每个线程都在等待被其他线程占用并堵塞了的资源

死锁及oracle死锁--转载

今天看群里在讨论数据库死锁的问题,也一起研究了下,查了些资料在这里总结下. 所谓死锁: 是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程. 由于资源占用是互斥的,当某个进程提出申请资源后,使得有关进程在无外力协助下,永远分配不到必需的资源而无法继续运行,这就产生了一种特殊现象死锁. 关于数据库死锁的检查方法 一.数据库死锁的现象 程序在执行的过程中,点击确定

SqlServer 查询死锁,杀死死锁进程

-- 查询死锁 select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran_locks where resource_type='OBJECT' --杀死死锁进程 kill spid

sqlserver 查询死锁和杀掉死锁

查询死锁select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran_locks where resource_type='OBJECT'杀掉死锁 kill spid

一个查询语句引发的死锁

程序错误日志大量的报死锁错误,去数据库错误日志查看确实有很多死锁(应在数据库实例启动时执行dbcc traceon(1222,-1)开启死锁跟踪): 04/29/2016 14:07:51,spid33s,δ?,waiter id=process71da6bb88 mode=IX requestType=wait 04/29/2016 14:07:51,spid33s,δ?,waiter-list 04/29/2016 14:07:51,spid33s,δ?,owner id=process5b

Sql数据库查询当前环境有无死锁

DECLARE @spid INT , @bl INT , @intTransactionCountOnEntry INT , @intRowcount INT , @intCountProperties INT , @intCounter INT CREATE TABLE #tmp_lock_who ( id INT IDENTITY(1, 1) , spid SMALLINT , bl SMALLINT ) IF @@ERROR <> 0 print @@ERROR INSERT INTO

谈谈MySQL死锁之二 死锁检测和处理源码分析

这一篇主要是通过一个实验来进行描述,过程是比较枯燥的. 实验准备 create table test_lock(id int auto_increment primary key ,stock int) engine=innodb; insert into test_lock(id,stock) value(1,50); 这里我把堆栈信息尽可能的简化,25个主要函数的名称和入参 后面为了突出主题,我对事务相关的函数加上这个开头死锁检测函数列表,一共10个函数   死锁检测函数列表A row_se

sql server 查看表的死锁和Kill 死锁进程

查询出来 select        request_session_id spid,       OBJECT_NAME(resource_associated_entity_id) tableName    from        sys.dm_tran_locks   where        resource_type='OBJECT 杀死死锁进程 kill spid 另: exec master.dbo.sp_who_lock --查看当前死锁进程 exec master.dbo.p_

c++多线程の死锁与防止死锁

如果有两把锁 lock1(mutex_gard 方式)和lock: 两者的调用顺序不同,会出现相互等待的情况,从而造成死锁: 为了避免死锁,我们可以: 1.每个线程中锁的调用顺序都相同: 2.使用std:: lock(); 具体用法;在主线程和子线程都调用的方法中 std::lock(mutex1,mutex2,....); 加锁的时候 另外:mutex自身也有lock和unlock方法