INSERT和SELECT SP之间的死锁

时间:2014-03-19 13:27:57

标签: sql sql-server stored-procedures database-deadlocks database-locking

我遇到了一个应用程序出现问题,我认为这种情况永远不会发生。我有两个存储过程。其中一个只读取表的一部分,其中一个向表中添加一行,或者如果存在主键违规则更新现有行。

我已经读过有关存储过程可以获得共享读锁的情况,并且如果两个存储过程都升级为独占锁,则会出现死锁。我无法看到这里发生的事情,因为GetList存储过程只读取并且从不写入。这些存储过程是否还有其他可能导致死锁的原因?

以下有关死锁的报告:

<?xml version="1.0" encoding="UTF-8"?>
<deadlock-list>
   <deadlock victim="process3e37f62c8">
      <process-list>
         <process id="process3e37f62c8" taskpriority="0" logused="0" waitresource="KEY: 7:72057594152681472 (de2613e7782e)" waittime="2535" ownerId="7732055781" transactionname="SELECT" lasttranstarted="2014-03-17T17:48:31.437" XDES="0x1298bc1c0" lockMode="S" schedulerid="11" kpid="21372" status="suspended" spid="121" sbid="0" ecid="0" priority="0" trancount="0" lastbatchstarted="2014-03-17T17:48:31.437" lastbatchcompleted="2014-03-17T17:48:31.437" clientapp=".Net SqlClient Data Provider" hostname="STO2AP07" hostpid="4908" loginname="(hidden)" isolationlevel="read committed (2)" xactid="7732055781" currentdb="7" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
            <executionStack>
               <frame procname="(hidden).dbo.GetList" line="18" stmtstart="1060" stmtend="1942" sqlhandle="0x03000700072cfd1b7b8be800cfa200000100000000000000">
                   SELECT 
                        TOP (@NumRows)
                        [T1].[Id] , 
                        [T1].[A] , 
                        [T1].[B] , 
                        [T1].[C] , 
                        [T1].[D] , 
                        [T1].[E] , 
                        [T1].[F] , 
                        [T1].[G] , 
                        [T1].[H] , 
                        [T1].[I] , 
                        [T1].[J] 
                    FROM [Item] AS [T1] where [J] > @LastUpdatedDateTime ORDER BY [T1].[LastUpdatedField] ASC
                </frame>
            </executionStack>
            <inputbuf>Proc [Database Id = 7 Object Id = 469576711]</inputbuf>
         </process>
         <process id="process5a514c8" taskpriority="0" logused="244" waitresource="KEY: 7:72057594152615936 (16f70bd264f5)" waittime="2535" ownerId="7732055725" transactionname="user_transaction" lasttranstarted="2014-03-17T17:48:31.437" XDES="0x6602dfa00" lockMode="X" schedulerid="13" kpid="21196" status="suspended" spid="267" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2014-03-17T17:48:31.437" lastbatchcompleted="2014-03-17T17:48:31.437" clientapp=".Net SqlClient Data Provider" hostname="STO2AP07" hostpid="4908" loginname="(hidden)" isolationlevel="read committed (2)" xactid="7732055725" currentdb="7" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
            <executionStack>
               <frame procname="(hidden).dbo.AddToList" line="52" stmtstart="2484" stmtend="3294" sqlhandle="0x030007005cbf2019598be800cfa200000100000000000000">
                    UPDATE [Item] 
                    SET 
                        [A] = @A , 
                        [B] = @B , 
                        [C] = @C , 
                        [D] = @D , 
                        [E] = @E , 
                        [F] = @F , 
                        [G] = @G , 
                        [H] = @H , 
                        [I] = @I ,
                        [J] = @J 
                    WHERE [Id] = @Id
                </frame>
            </executionStack>
            <inputbuf>Proc [Database Id = 7 Object Id = 421576540]</inputbuf>
         </process>
      </process-list>
      <resource-list>
         <keylock hobtid="72057594152681472" dbid="7" objectname="(hidden).dbo.Item" indexname="PK_Item_1" id="lock47c381b80" mode="X" associatedObjectId="72057594152681472">
            <owner-list>
               <owner id="process5a514c8" mode="X" />
            </owner-list>
            <waiter-list>
               <waiter id="process3e37f62c8" mode="S" requestType="wait" />
            </waiter-list>
         </keylock>
         <keylock hobtid="72057594152615936" dbid="7" objectname="(hidden).dbo.Item" indexname="IX_LastUpdatedField" id="lock402a95800" mode="S" associatedObjectId="72057594152615936">
            <owner-list>
               <owner id="process3e37f62c8" mode="S" />
            </owner-list>
            <waiter-list>
               <waiter id="process5a514c8" mode="X" requestType="wait" />
            </waiter-list>
         </keylock>
      </resource-list>
   </deadlock>
</deadlock-list>

以下是名为GetList的存储过程:

ALTER PROCEDURE [dbo].[GetList]
    @LastUpdatedDateTime datetime,
    @NumRows int
WITH RECOMPILE
AS
BEGIN
    SET NOCOUNT ON;

    SELECT TOP (@NumRows)
           [T1].[Id]
         , [T1].[A]
         , [T1].[B]
         , [T1].[C]
         , [T1].[D]
         , [T1].[E]
         , [T1].[F]
         , [T1].[G]
         , [T1].[H]
         , [T1].[I]
         , [T1].[J]
      FROM [Item] AS [T1] where J >= @LastUpdatedDateTime
  ORDER BY [T1].[J] ASC
END

以下是名为AddToList的存储过程:

ALTER PROCEDURE [dbo].[AddToList]
    @Id int,
    @A varchar(30),
    @B decimal(18,2),
    @V varchar(30),
    @D varchar(512),
    @E datetime,
    @F datetime,
    @G bit,
    @H int,
    @I int
    @J datetime,
AS
IF NOT EXISTS(SELECT Id FROM Item WHERE [Id] = @Id)
    BEGIN
        SET NOCOUNT ON;
        INSERT INTO [Item] 
                  ( [Id] 
                  , [A]
                  , [B]
                  , [C]
                  , [D]
                  , [E]
                  , [F]
                  , [G]
                  , [H]
                  , [I]
                  , [J]
                  )
             VALUES
                  ( @Id
                  , @A
                  , @B
                  , @C
                  , @D
                  , @E
                  , @F
                  , @G
                  , @H
                  , @I
                  , @J
                  )
    END
ELSE
    BEGIN
        UPDATE [Item] SET
                   [A] = @A
                  , [B] = @B
                  , [C] = @C
                  , [D] = @D
                  , [E] = @E
                  , [F] = @F
                  , [G] = @G
                  , [H] = @H
                  , [I] = @I
                  , [J] = @J
        WHERE [Id] = @Id
    END

1 个答案:

答案 0 :(得分:2)

选择这样做:

  1. 从LastUpdatedDateTime索引(S-lock)读取
  2. 查找相应的CI行(S-lock)
  3. 更新执行此操作:

    1. 写入CI(X锁定)
    2. 写入LastUpdatedDateTime索引(X-lock)
    3. 他们以相反的顺序访问两个不兼容的资源。死锁。

      读者是否在高于READ COMMITTED的隔离级别下运行? READ COMMITTED应在读取锁定的行后立即释放锁。

      如果您可以使读取事务使用快照隔离模型,则死锁将以简单,可靠和永久的方式消失。

      如果无法使用READ COMMITTED隔离级别。

      如果那是不可能的,你要么必须搞乱锁定提示(难以维护),要么实现死锁重试逻辑。