当对数据库并发访问的时候,当两个会话同时等待访问由其他会话锁定的资源时,便会发生死锁。
当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表上锁,然后其他的访问等待,等上一个事务结束后,下一个在执行,并没死锁的可能啊.......

解决方案 »

  1.   

    oh  my god  还没人回复..
      

  2.   

    死锁分析
    http://blog.csdn.net/roy_88/article/details/2686724
      

  3.   

    UPDATE TOP (1) TransferInformation 
    set RecordsState=5 
    where RecordsState<>5就這麼回事 
      

  4.   

    SQL2000就樣用SET ROWCOUNT 1;
    UPDATE  TransferInformation 
    set RecordsState=5 
    where RecordsState<>5;
    SET ROWCOUNT 0;
      

  5.   

    稍微改一下,如下代码,你再并发执行也不会死锁了,嘿嘿..ALTER PROCEDURE [dbo].[test]  
    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.   

    简单来讲,原先代码
    第6行的 TransferInformation 修改为 TransferInformation (nolock)
    第8行的 temp  修改为 temp (nolock)