第四团队—Team 4

 

 事务与另一个进程已被死锁在 lock 资源上

Pillys, 发表于:2008-10-04 07:05:40, 分类:Programmer 浏览( ) 评论( )  收藏这篇日志

设table1(A,B,C)
A B C
a1 b1 c1
a2 b2 c2
a3 b3 c3

1)排它锁
新建两个连接
在第一个连接中执行以下语句
begin tran
update table1
set A=aa
where B=b2
waitfor delay 00:00:30 --等待30秒
commit tran
在第二个连接中执行以下语句
begin tran
select * from table1
where B=b2
commit tran

若同时执行上述两个语句,则select查询必须等待update执行完毕才能执行即要等待30秒

2)共享锁
在第一个连接中执行以下语句
begin tran
select * from table1 holdlock -holdlock人为加锁
where B=b2
waitfor delay 00:00:30 --等待30秒
commit tran

在第二个连接中执行以下语句
begin tran
select A,C from table1
where B=b2
update table1
set A=aa
where B=b2
commit tran

若同时执行上述两个语句,则第二个连接中的select查询可以执行
而update必须等待第一个连接中的共享锁结束后才能执行 即要等待30秒

3)死锁
增设table2(D,E)
D E
d1 e1
d2 e2
在第一个连接中执行以下语句
begin tran
update table1
set A=aa
where B=b2
waitfor delay 00:00:30
update table2
set D=d5
where E=e1
commit tran

在第二个连接中执行以下语句
begin tran
update table2
set D=d5
where E=e1
waitfor delay 00:00:10
update table1
set A=aa
where B=b2
commit tran

同时执行,系统会检测出死锁,并中止进程

正在读取日志的评论数据,请稍后……
正在加载日志评论签写框,请稍后……
BLOG 推荐日志
  • 暂时没有推荐日志
BLOG 最新留言
{$SideGB}
BLOG 站内搜索

成员登录通道
正在载入成员登录通道...