【问题标题】:Identify the reason of deadlock确定死锁的原因
【发布时间】:2012-09-27 10:43:54
【问题描述】:

我有 2 个以上的并发进程。在它们内部有一个执行重复验证(选择)+保存(插入)操作的迭代。这是关于死锁的 sql-server-profiler 信息:

<deadlock-list>
     <deadlock victim="process8e09048">
      <process-list>
       <process id="process8e09048" taskpriority="0" logused="1088" waitresource="PAGE: 29:1:376823" waittime="920" ownerId="1276429306" transactionname="user_transaction" lasttranstarted="2012-09-26T20:59:44.367" XDES="0x3077833c0" lockMode="S" schedulerid="1" kpid="4872" status="suspended" spid="79" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2012-09-26T20:59:44.427" lastbatchcompleted="2012-09-26T20:59:44.427" clientapp=".Net SqlClient Data Provider" hostname="PORTAL" hostpid="5348" loginname="IIS APPPOOL\ASP.NET v4.0 Classic" isolationlevel="read committed (2)" xactid="1276429306" currentdb="29" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
        <executionStack>
         <frame procname="adhoc" line="1" stmtstart="282" sqlhandle="0x02000000b79df8046665e3984d6dc129fec20a3029fce9bc">
    SELECT id FROM [WP_CashCenter_StockTransactionLine] WHERE StockTransaction_id=@StockTransaction_id and Direction=@Direction and IsVerified=@IsVerified and QualificationType=@QualificationType and id &lt;&gt; @id and Product_id is null and Material_id=@Material_id and StockContainer_id is null and StockLocation_id=@StockLocation_id     </frame>
         <frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
    unknown     </frame>
        </executionStack>
        <inputbuf>
    (@id bigint,@StockTransaction_id bigint,@Direction int,@IsVerified bit,@QualificationType int,@Material_id nvarchar(3),@StockLocation_id int)SELECT id FROM [WP_CashCenter_StockTransactionLine] WHERE StockTransaction_id=@StockTransaction_id and Direction=@Direction and IsVerified=@IsVerified and QualificationType=@QualificationType and id &lt;&gt; @id and Product_id is null and Material_id=@Material_id and StockContainer_id is null and StockLocation_id=@StockLocation_id    </inputbuf>
       </process>
       <process id="process5c13948" taskpriority="0" logused="2636" waitresource="PAGE: 29:1:376823" waittime="920" ownerId="1276429252" transactionname="user_transaction" lasttranstarted="2012-09-26T20:59:44.337" XDES="0x222e64e80" lockMode="S" schedulerid="6" kpid="2956" status="suspended" spid="70" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2012-09-26T20:59:44.427" lastbatchcompleted="2012-09-26T20:59:44.427" clientapp=".Net SqlClient Data Provider" hostname="PORTAL" hostpid="5348" loginname="IIS APPPOOL\ASP.NET v4.0 Classic" isolationlevel="read committed (2)" xactid="1276429252" currentdb="29" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
        <executionStack>
         <frame procname="adhoc" line="1" stmtstart="238" sqlhandle="0x02000000d77dd1038a9b7f6d7158436117c042e42767242d">
    SELECT id FROM [WP_CashCenter_StockTransactionLine] WHERE StockTransaction_id=@StockTransaction_id and Direction=@Direction and IsVerified=@IsVerified and QualificationType=@QualificationType and id &lt;&gt; @id and Product_id is null and Material_id=@Material_id and StockContainer_id is null and StockLocation_id is null     </frame>
         <frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
    unknown     </frame>
        </executionStack>
        <inputbuf>
    (@id bigint,@StockTransaction_id bigint,@Direction int,@IsVerified bit,@QualificationType int,@Material_id nvarchar(3))SELECT id FROM [WP_CashCenter_StockTransactionLine] WHERE StockTransaction_id=@StockTransaction_id and Direction=@Direction and IsVerified=@IsVerified and QualificationType=@QualificationType and id &lt;&gt; @id and Product_id is null and Material_id=@Material_id and StockContainer_id is null and StockLocation_id is null    </inputbuf>
       </process>
      </process-list>
      <resource-list>
       <pagelock fileid="1" pageid="376823" dbid="29" objectname="Edsson_WebPortal_v5.01.dbo.WP_CashCenter_StockTransactionLine" id="lock9302c80" mode="IX" associatedObjectId="72057594148028416">
        <owner-list>
         <owner id="process5c13948" mode="IX"/>
        </owner-list>
        <waiter-list>
         <waiter id="process8e09048" mode="S" requestType="convert"/>
        </waiter-list>
       </pagelock>
       <pagelock fileid="1" pageid="376823" dbid="29" objectname="Edsson_WebPortal_v5.01.dbo.WP_CashCenter_StockTransactionLine" id="lock9302c80" mode="IX" associatedObjectId="72057594148028416">
        <owner-list>
         <owner id="process8e09048" mode="IX"/>
        </owner-list>
        <waiter-list>
         <waiter id="process5c13948" mode="S" requestType="convert"/>
        </waiter-list>
       </pagelock>
      </resource-list>
     </deadlock>
    </deadlock-list>

语句是带有条件的选择。这些选择的 where 子句仅使用包含在非聚集索引中的列。执行计划:

【问题讨论】:

  • 您的事务有 IX 锁,这意味着该事务在此 SELECT 之前完成了更新、删除或插入。
  • Stack Overflow 是一个问答板,而不是解决问题的机器。如果你需要帮助,你需要把它变成一个问题:解释问题,解释你想要做什么,解释你试图纠正它,然后问一个问题。一路走来,您可能会找到“橡皮鸭”的答案。
  • @RemusRusanu 是的,这是一个带有验证(选择)+插入的循环。并且有 2+ 个并发进程正在执行这个循环。
  • 只需在您的选择语句中添加 with(udplock) 提示。
  • 可能的解决方案。 1. 覆盖选择语句的索引。 2. RCSI 3. SELECT 上的 ROWLOCK 提示

标签: sql sql-server profiling deadlock


【解决方案1】:

在这个死锁图中,两个进程都成功执行了 INSERT,并且在同一页面上持有 IX 锁。他们必须在同一页的两个单独的行上也持有 X 锁。

由于SELECT被NC索引覆盖,死锁涉及的页面必须属于NC索引。

您的 SELECT 语句正在尝试获取页级锁,该锁已通过 INSERT 语句获得 IX 锁。

因此,如果您的 INSERT 不属于同一个 StockTransaction_id,只需将 ROWLOCK 提示添加到 SELECT 即可解决死锁情况。

RCSI 保证读取器不会被写入器阻塞,反之亦然。但是,如果您的应用程序设计依赖于任何阻塞行为,这可能是一个问题。

您可以阅读更多关于锁粒度不同导致的死锁here

【讨论】:

    猜你喜欢
    • 2021-10-03
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多