在执行单选的进程中进行S锁定

时间:2013-03-25 09:38:23

标签: sql-server profiling deadlock

我在2个进程之间出现僵局, processd38f8748 (第1个)和 process5b2988 (第2个)。

第一个进程是SELECT,它在一个单独的连接中运行。在代码中没有为它打开显式事务。在同一连接中,此SELECT之前或之后可能没有运行sql请求。此选择需要第356615页上的获取共享锁。

第二个进程是一个UPDATE并且它已经在页面356615上保存了一个IX锁(它很难识别系统中的确切位置,但我可以相信),而它需要另一个页面356614.这里的一切都还可以。

现在,奇怪的是第二个进程无法获得第356614页的锁定,因为它已被第一个进程保留。

这对我来说很奇怪有两个原因:

  • 我非常确定此选择是sql-connection
  • 中的唯一请求
  • IL是readcommited。我认为对于这个IL,一旦语句结束就会释放所有S锁。

这是死锁xml。

<deadlock-list>
 <deadlock victim="processd38f8748">
  <process-list>
   <process id="processd38f8748" taskpriority="0" logused="0" waitresource="PAGE: 7:1:356615" waittime="2369" ownerId="24096354927" transactionname="SELECT" lasttranstarted="2013-03-06T11:26:10.810" XDES="0x368701960" lockMode="S" schedulerid="4" kpid="5756" status="suspended" spid="60" sbid="0" ecid="11" priority="0" trancount="0" lastbatchstarted="2013-03-06T11:26:10.810" lastbatchcompleted="2013-03-06T11:26:10.810" clientapp=".Net SqlClient Data Provider" hostname="CWCEINAW" hostpid="2604" isolationlevel="read committed (2)" xactid="24096354927" currentdb="7" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
    <executionStack>
     <frame procname="adhoc" line="1" sqlhandle="0x02000000dffc7a241f9433fddb83b7f97a7c0369dbf6d467">
SELECT TOP 1 1
                FROM [WP_CashCenter_StockTransaction] AS [ST]
                    LEFT JOIN [WP_CashCenter_StockTransactionLine] AS [STL] ON ( [STL].[StockTransaction_id] = [ST].[id] )
                WHERE
                    [ST].[Status] IN (0, 1) AND
                    [ST].[Type] = 0 AND
                    ([ST].[StockContainerID] = 300000120244 OR [STL].[StockContainer_id] = 300000120244)     </frame>
    </executionStack>
    <inputbuf>
    </inputbuf>
   </process>
   <process id="process5b2988" taskpriority="0" logused="17268" waitresource="PAGE: 7:1:356614" waittime="2264" ownerId="24096354521" transactionname="user_transaction" lasttranstarted="2013-03-06T11:26:10.690" XDES="0x80021040" lockMode="IX" schedulerid="2" kpid="5832" status="suspended" spid="55" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2013-03-06T11:26:10.920" lastbatchcompleted="2013-03-06T11:26:10.920" lastattention="2013-02-28T11:26:25.940" clientapp=".Net SqlClient Data Provider" hostname="CWCEINAW" hostpid="2604" loginname="cwcuser" isolationlevel="read committed (2)" xactid="24096354521" currentdb="7" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
    <executionStack>
     <frame procname="adhoc" line="1" stmtstart="702" sqlhandle="0x02000000f0a38d32edae20f54b42cd640b8022448db200b7">
UPDATE [WP_CashCenter_StockTransaction] SET [type] = @type, [status] = @status, [startdate] = @startdate, [enddate] = @enddate, [totalquantity] = @totalquantity, [totalvalue] = @totalvalue, [totalweight] = @totalweight, [datecreated] = @datecreated, [dateupdated] = @dateupdated, [stockorder_id] = @stockorder_id, [site_id] = @site_id, [stockowner_id] = @stockowner_id, [stocklocation_id] = @stocklocation_id, [stockcontainerid] = @stockcontainerid, [uid] = @uid, [machinenumber] = @machinenumber, [editor_id] = @editor_id WHERE [id] = @id     </frame>
     <frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
unknown     </frame>
    </executionStack>
    <inputbuf>
(@type int,@status int,@startdate datetime,@enddate datetime,@totalquantity int,@totalvalue decimal(6,2),@totalweight decimal(5,5),@datecreated datetime,@dateupdated datetime,@stockorder_id bigint,@site_id int,@stockowner_id int,@stocklocation_id int,@stockcontainerid bigint,@uid uniqueidentifier,@machinenumber nvarchar(1),@editor_id int,@id bigint)UPDATE [WP_CashCenter_StockTransaction] SET [type] = @type, [status] = @status, [startdate] = @startdate, [enddate] = @enddate, [totalquantity] = @totalquantity, [totalvalue] = @totalvalue, [totalweight] = @totalweight, [datecreated] = @datecreated, [dateupdated] = @dateupdated, [stockorder_id] = @stockorder_id, [site_id] = @site_id, [stockowner_id] = @stockowner_id, [stocklocation_id] = @stocklocation_id, [stockcontainerid] = @stockcontainerid, [uid] = @uid, [machinenumber] = @machinenumber, [editor_id] = @editor_id WHERE [id] = @id    </inputbuf>
   </process>
  </process-list>
  <resource-list>
   <pagelock fileid="1" pageid="356615" dbid="7" objectname="Eindhoven_CWC.dbo.WP_CashCenter_StockTransaction" id="lock10f24cb00" mode="IX" associatedObjectId="72057594093830144">
    <owner-list>
     <owner id="process5b2988" mode="IX"/>
    </owner-list>
    <waiter-list>
     <waiter id="processd38f8748" mode="S" requestType="wait"/>
    </waiter-list>
   </pagelock>
   <pagelock fileid="1" pageid="356614" dbid="7" objectname="Eindhoven_CWC.dbo.WP_CashCenter_StockTransaction" id="lock17e539e00" mode="SIU" associatedObjectId="72057594093830144">
    <owner-list>
     <owner id="processd38f8748" mode="S"/>
    </owner-list>
    <waiter-list>
     <waiter id="process5b2988" mode="IX" requestType="convert"/>
    </waiter-list>
   </pagelock>
  </resource-list>
 </deadlock>
</deadlock-list>

1 个答案:

答案 0 :(得分:1)

看起来读者与作者之间的访问顺序不同。我希望更新只对[Id] = @id进行一次搜索,但事实上它有一个页面并想要另一个表示扫描。你有[Id]的索引吗?