Sql Server 数据库死锁介绍和解决方法

您所在的位置:网站首页 selenium多开死锁 Sql Server 数据库死锁介绍和解决方法

Sql Server 数据库死锁介绍和解决方法

2023-09-28 23:01| 来源: 网络整理| 查看: 265

SQL Server数据库死锁,通俗的讲就是两个或多个trans,同时请求对方正在请求的某个实际应用对象,而导致双方互相等待。简单的例子如下:  

sql server死锁表现一:

   一个用户A 访问表A(锁住了表A),然后又访问表B。    另一个用户B 访问表B(锁住了表B),然后企图访问表A,    这时用户A由于用户B已经锁住表B,它必须等待用户B释放表B,才能继续,好了他老人家就只好老老实实在这等了,同样用户B要等用户A释放表A才能继续,这就造成死锁了。

  sql server死锁解决方法:    这种死锁是由于你的程序的BUG产生的,除了调整你的程序的逻辑别无他法    仔细分析你程序的逻辑,    1:尽量避免同时锁定两个资源    2: 必须同时锁定两个资源时,要保证在任何时刻都应该按照相同的顺序来锁定资源.   sql server死锁表现二:

   用户A读一条纪录,然后修改该条纪录。    这时用户B修改该条纪录,这里用户A的事务里锁的性质由共享锁企图上升到独占锁(for update),而用户B里的独占锁由于A有共享锁存在所以必须等A释    放掉共享锁,而A由于B的独占锁而无法上升的独占锁也就不可能释放共享锁,于是出现了死锁。    这种死锁比较隐蔽,但其实在稍大点的项目中经常发生。

sql server死锁解决方法:    让用户A的事务(即先读后写类型的操作),在select 时用Update lock 语法如下:    select * from table1 with(updlock) where ....

sqlserver死锁检查工具 (存储过程)

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_who_lock]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_who_lock] GO 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 @@ERROR0 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 @@ERROR0 RETURN @@ERROR -- 找到临时表的记录数 select @intCountProperties = Count(*),@intCounter = 1 from #tmp_lock_who IF @@ERROR0 RETURN @@ERROR if @intCountProperties=0 select '现在没有阻塞和死锁信息' as message -- 循环开始 while @intCounter


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3