当对数据库并发访问的时候,当两个会话同时等待访问由其他会话锁定的资源时,便会发生死锁。
当sql发现死锁的访问后,将选择其中一个会话作为死锁牺牲品来解决死锁.
这个是msSqlServer的基本功能,我想大多数人都有所了解,
我想问的是,msSqlServer自身是如何判断2个访问发生了死锁的啊,
我做了一个简单的存储过程,然后用并发去执行,超不过10次就提示,发生了死锁,我很不理解啊.
存储过程代码ALTER PROCEDURE [dbo].[test]
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
insert dbo.temp(name) select top 1 id from TransferInformation where RecordsState<>5
declare @id int
select @id=name from temp where id= @@IDENTITY
update TransferInformation set RecordsState=5 where Id=@id
COMMIT TRANSACTION
END
大家都是调用者一个存储过程,先调用的会对temp表上锁,然后其他的访问等待,等上一个事务结束后,下一个在执行,并没死锁的可能啊.......
当sql发现死锁的访问后,将选择其中一个会话作为死锁牺牲品来解决死锁.
这个是msSqlServer的基本功能,我想大多数人都有所了解,
我想问的是,msSqlServer自身是如何判断2个访问发生了死锁的啊,
我做了一个简单的存储过程,然后用并发去执行,超不过10次就提示,发生了死锁,我很不理解啊.
存储过程代码ALTER PROCEDURE [dbo].[test]
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
insert dbo.temp(name) select top 1 id from TransferInformation where RecordsState<>5
declare @id int
select @id=name from temp where id= @@IDENTITY
update TransferInformation set RecordsState=5 where Id=@id
COMMIT TRANSACTION
END
大家都是调用者一个存储过程,先调用的会对temp表上锁,然后其他的访问等待,等上一个事务结束后,下一个在执行,并没死锁的可能啊.......
http://blog.csdn.net/roy_88/article/details/2686724
set RecordsState=5
where RecordsState<>5就這麼回事
UPDATE TransferInformation
set RecordsState=5
where RecordsState<>5;
SET ROWCOUNT 0;
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
insert dbo.temp(name)
select top 1 id from TransferInformation(nolock) where RecordsState<>5
declare @id int
select @id=name from temp(nolock) where id= @@IDENTITY
update TransferInformation set RecordsState=5 where Id=@id
COMMIT TRANSACTION
END
第6行的 TransferInformation 修改为 TransferInformation (nolock)
第8行的 temp 修改为 temp (nolock)