begin tran waitfor delay '00:00:0.1' select * from moe_test.dbo.MOE_PendRloc with(nolock) where SourceSystem='AIR' and SourceRef='7FEMRO' and OwnerTeam='SHD' waitfor delay '00:00:0.1' delete moe_test.dbo.MOE_PendRloc where SourceSystem='AIR' and SourceRef='7FEMRO' and OwnerTeam='SHD' waitfor delay '00:00:0.1' insert into moe_test.dbo.MOE_PendRloc select * from moe.dbo.MOE_PendRloc where SourceSystem='AIR' and SourceRef='7FEMRO' and OwnerTeam='SHD'
commit tran set @i=@i-1 end
我开了3个会话用以上的SQL来测试,没死锁
然后改成
select with(UpdLock)
测试就锁发生死锁了
spid dbid ObjId IndId Type Resource Mode Status
------ ------ ----------- ------ ---- -------------------------------- -------- ------
57 51 0 0 DB S GRANT
57 80 0 0 DB S GRANT
57 51 1076198884 0 PAG 1:16568 IU GRANT
57 51 1076198884 0 PAG 1:49337 IU GRANT
57 51 1076198884 0 RID 1:16568:11 U WAIT
57 51 1076198884 0 PAG 1:72482 IU GRANT
57 51 1076198884 0 PAG 1:6642 IU GRANT
57 51 1076198884 0 PAG 1:6649 IU GRANT
57 51 1076198884 0 PAG 1:6651 IU GRANT
57 51 1076198884 0 PAG 1:6650 IU GRANT
57 51 1076198884 0 PAG 1:56307 IU GRANT
57 51 1076198884 0 PAG 1:15939 IU GRANT
57 51 1076198884 0 PAG 1:15938 IU GRANT
57 51 1076198884 0 PAG 1:15937 IU GRANT
57 51 1076198884 0 PAG 1:15936 IU GRANT
57 51 1076198884 0 PAG 1:15940 IU GRANT
57 51 1076198884 0 PAG 1:15931 IU GRANT
57 51 1076198884 0 PAG 1:15930 IU GRANT
57 51 1076198884 0 PAG 1:15928 IU GRANT
57 51 1076198884 0 PAG 1:15935 IU GRANT
57 51 1076198884 0 PAG 1:15933 IU GRANT
57 51 1076198884 0 PAG 1:15932 IU GRANT
57 51 1076198884 0 TAB IX GRANT
57 51 1076198884 0 PAG 1:48823 IU GRANT
68 80 0 0 DB S GRANT
68 51 0 0 DB S GRANT
68 51 1076198884 0 PAG 1:49337 IU GRANT
68 51 1076198884 0 PAG 1:16568 IU GRANT
68 51 1076198884 0 RID 1:16568:11 U GRANT
68 51 1076198884 0 PAG 1:72482 IU GRANT
68 51 1076198884 0 PAG 1:56307 IU GRANT
68 51 1076198884 0 PAG 1:6650 IU GRANT
68 51 1076198884 0 PAG 1:6651 IU GRANT
68 51 1076198884 0 PAG 1:6649 IU GRANT
68 51 1076198884 0 PAG 1:15937 IU GRANT
68 51 1076198884 0 PAG 1:15938 IU GRANT
68 51 1076198884 0 PAG 1:6642 IU GRANT
68 51 1076198884 0 PAG 1:15939 IU GRANT
68 51 1076198884 0 PAG 1:15932 IU GRANT
68 51 1076198884 0 PAG 1:15933 IU GRANT
68 51 1076198884 0 PAG 1:15935 IU GRANT
68 51 1076198884 0 PAG 1:15928 IU GRANT
68 51 1076198884 0 PAG 1:15930 IU GRANT
68 51 1076198884 0 PAG 1:15931 IU GRANT
68 51 1076198884 0 PAG 1:15940 IU GRANT
68 51 1076198884 0 PAG 1:15936 IU GRANT
68 51 1076198884 0 PAG 1:48823 IU GRANT
68 51 1076198884 0 TAB IX GRANT
74 51 0 0 DB S GRANT
74 80 0 0 DB S GRANT
74 51 1076198884 0 PAG 1:16568 IU GRANT
74 51 1076198884 0 PAG 1:72482 IU GRANT
74 51 1076198884 0 RID 1:16568:11 U WAIT
74 51 1076198884 0 PAG 1:49337 IU GRANT
74 51 1076198884 0 PAG 1:56307 IU GRANT
74 51 1076198884 0 TAB IX GRANT
74 51 1076198884 0 PAG 1:48823 IU GRANT
可以看到有两个U锁确实是处于wait的状态。
select with(Updlock) select with(Updlock) select with(Updlock)
| | |
Delete Delete Delete
| | |
Insert Insert Insert
套上死锁理论:例如A B互抢
A B
| |
B A
可以发现是有可能发生死锁的。
以上只是本人目前水平的结论。有错的地方希望有心人指出。
还有,本人想更深入的了解Insert 语句的机制,例如包括Insert前和后发生了一些什么操作,或者所加的锁,或者其它等。
有关于这方面的资料可以共享下吗?求赐教
begin tran waitfor delay '00:00:0.1' select * from moe_test.dbo.MOE_PendRloc with(nolock) where SourceSystem='AIR' and SourceRef='7FEMRO' and OwnerTeam='SHD' waitfor delay '00:00:0.1' delete moe_test.dbo.MOE_PendRloc where SourceSystem='AIR' and SourceRef='7FEMRO' and OwnerTeam='SHD' waitfor delay '00:00:0.1' insert into moe_test.dbo.MOE_PendRloc select * from moe.dbo.MOE_PendRloc where SourceSystem='AIR' and SourceRef='7FEMRO' and OwnerTeam='SHD'
commit tran set @i=@i-1 end
我开了3个会话用以上的SQL来测试,没死锁
然后改成
select with(UpdLock)
测试就锁发生死锁了
spid dbid ObjId IndId Type Resource Mode Status
------ ------ ----------- ------ ---- -------------------------------- -------- ------
57 51 0 0 DB S GRANT
57 80 0 0 DB S GRANT
57 51 1076198884 0 PAG 1:16568 IU GRANT
57 51 1076198884 0 PAG 1:49337 IU GRANT
57 51 1076198884 0 RID 1:16568:11 U WAIT
57 51 1076198884 0 PAG 1:72482 IU GRANT
57 51 1076198884 0 PAG 1:6642 IU GRANT
57 51 1076198884 0 PAG 1:6649 IU GRANT
57 51 1076198884 0 PAG 1:6651 IU GRANT
57 51 1076198884 0 PAG 1:6650 IU GRANT
57 51 1076198884 0 PAG 1:56307 IU GRANT
57 51 1076198884 0 PAG 1:15939 IU GRANT
57 51 1076198884 0 PAG 1:15938 IU GRANT
57 51 1076198884 0 PAG 1:15937 IU GRANT
57 51 1076198884 0 PAG 1:15936 IU GRANT
57 51 1076198884 0 PAG 1:15940 IU GRANT
57 51 1076198884 0 PAG 1:15931 IU GRANT
57 51 1076198884 0 PAG 1:15930 IU GRANT
57 51 1076198884 0 PAG 1:15928 IU GRANT
57 51 1076198884 0 PAG 1:15935 IU GRANT
57 51 1076198884 0 PAG 1:15933 IU GRANT
57 51 1076198884 0 PAG 1:15932 IU GRANT
57 51 1076198884 0 TAB IX GRANT
57 51 1076198884 0 PAG 1:48823 IU GRANT
68 80 0 0 DB S GRANT
68 51 0 0 DB S GRANT
68 51 1076198884 0 PAG 1:49337 IU GRANT
68 51 1076198884 0 PAG 1:16568 IU GRANT
68 51 1076198884 0 RID 1:16568:11 U GRANT
68 51 1076198884 0 PAG 1:72482 IU GRANT
68 51 1076198884 0 PAG 1:56307 IU GRANT
68 51 1076198884 0 PAG 1:6650 IU GRANT
68 51 1076198884 0 PAG 1:6651 IU GRANT
68 51 1076198884 0 PAG 1:6649 IU GRANT
68 51 1076198884 0 PAG 1:15937 IU GRANT
68 51 1076198884 0 PAG 1:15938 IU GRANT
68 51 1076198884 0 PAG 1:6642 IU GRANT
68 51 1076198884 0 PAG 1:15939 IU GRANT
68 51 1076198884 0 PAG 1:15932 IU GRANT
68 51 1076198884 0 PAG 1:15933 IU GRANT
68 51 1076198884 0 PAG 1:15935 IU GRANT
68 51 1076198884 0 PAG 1:15928 IU GRANT
68 51 1076198884 0 PAG 1:15930 IU GRANT
68 51 1076198884 0 PAG 1:15931 IU GRANT
68 51 1076198884 0 PAG 1:15940 IU GRANT
68 51 1076198884 0 PAG 1:15936 IU GRANT
68 51 1076198884 0 PAG 1:48823 IU GRANT
68 51 1076198884 0 TAB IX GRANT
74 51 0 0 DB S GRANT
74 80 0 0 DB S GRANT
74 51 1076198884 0 PAG 1:16568 IU GRANT
74 51 1076198884 0 PAG 1:72482 IU GRANT
74 51 1076198884 0 RID 1:16568:11 U WAIT
74 51 1076198884 0 PAG 1:49337 IU GRANT
74 51 1076198884 0 PAG 1:56307 IU GRANT
74 51 1076198884 0 TAB IX GRANT
74 51 1076198884 0 PAG 1:48823 IU GRANT
可以看到有两个U锁确实是处于wait的状态。
select with(Updlock) select with(Updlock) select with(Updlock)
| | |
Delete Delete Delete
| | |
Insert Insert Insert
套上死锁理论:例如A B互抢
A B
| |
B A
可以发现是有可能发生死锁的。
以上只是本人目前水平的结论。有错的地方希望有心人指出。
还有,本人想更深入的了解Insert 语句的机制,例如包括Insert前和后发生了一些什么操作,或者所加的锁,或者其它等。
有关于这方面的资料可以共享下吗?求赐教