如何修复2页上的死锁?

时间:2018-12-31 14:02:59

标签: c# sql sql-server database entity-framework

我的情况与this类似(存在有关PREFETCH的答案,但是答案链接是垃圾。找不到关于修复PREFETCH的好文章)。

问题地点的伪代码:

foreach(var item in list)
{
var item2 = Select //use item info
Update // use item2 info
}

Graph

我有避免在页面和行上使用S锁的想法: 1)快照交易 2)允许页面锁定:= false && SELECT WITH(updlock)。

但是对于上下文有EF ORM和UoW包装器,我在实现思想上遇到一些困难。 Mybee您有更好的想法或可以更好地解释情况。 名称已更改的XML:

<deadlock-list>
 <deadlock victim="process804e6bc8">
  <process-list>
   <process id="process804e6bc8" taskpriority="0" logused="0" waitresource="PAGE: 10:1:1488426" waittime="2142" ownerId="564828035" transactionname="SELECT" lasttranstarted="2018-12-19T17:40:15.957" XDES="0xa1c62330" lockMode="S" schedulerid="7" kpid="86760" status="suspended" spid="208" sbid="2" ecid="0" priority="0" trancount="0" lastbatchstarted="2018-12-19T17:40:15.957" lastbatchcompleted="2018-12-19T17:40:15.957" clientapp=".Net SqlClient Data Provider" hostname="" hostpid="10652" loginname="name" isolationlevel="read committed (2)" xactid="564828035" currentdb="1" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
    <executionStack>
     <frame procname="adhoc" line="1" stmtstart="66" sqlhandle="0x02000000621f4424c6c29ec12837bc8a85df7a3c6a7ab140">
SELECT 
    [Project1].[Id] AS [Id], 
    [Project1].[xxx] AS [xxx], 
    [Project1].[yyyid] AS [yyyid], 
    [Project1].[vvv] AS [vvv], 
    [Project1].[fff] AS [fff], 
    [Project1].[lll] AS [lll], 
    [Project1].[ccc] AS [ccc], 
    [Project1].[mmm] AS [mmm]
    FROM ( SELECT 
        [Extent1].[Id] AS [Id], 
        [Extent1].[xxx] AS [xxx], 
        [Extent1].[yyyid] AS [yyyid], 
        [Extent1].[vvv] AS [vvv], 
        [Extent1].[fff] AS [fff], 
        [Extent1].[lll] AS [lll], 
        [Extent1].[ccc] AS [ccc], 
        [Extent1].[mmm] AS [mmm]
        FROM [dbo].[sss] AS [Extent1]
        WHERE ([Extent1].[Cl     </frame>
     <frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
unknown     </frame>
    </executionStack>
    <inputbuf>
(@p__linq__0 int,@p__linq__1 int)SELECT 
    [Project1].[Id] AS [Id], 
    [Project1].[xxx] AS [xxx], 
    [Project1].[yyyid] AS [yyyid], 
    [Project1].[vvv] AS [vvv], 
    [Project1].[fff] AS [fff], 
    [Project1].[lll] AS [lll], 
    [Project1].[ccc] AS [ccc], 
    [Project1].[mmm] AS [mmm]
    FROM ( SELECT 
        [Extent1].[Id] AS [Id], 
        [Extent1].[xxx] AS [xxx], 
        [Extent1].[yyyid] AS [yyyid], 
        [Extent1].[vvv] AS [vvv], 
        [Extent1].[fff] AS [fff], 
        [Extent1].[lll] AS [lll], 
        [Extent1].[ccc] AS [ccc], 
        [Extent1].[mmm] AS [mmm]
        FROM [dbo].[sss] AS [Exte    </inputbuf>
   </process>
   <process id="process601ddc8" taskpriority="0" logused="1976" waitresource="PAGE: 10:1:1488427" waittime="2129" ownerId="564828018" transactionname="user_transaction" lasttranstarted="2018-12-19T17:40:15.913" XDES="0xdfb74e80" lockMode="IX" schedulerid="3" kpid="93564" status="suspended" spid="191" sbid="2" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-12-19T17:40:15.977" lastbatchcompleted="2018-12-19T17:40:15.977" clientapp=".Net SqlClient Data Provider" hostname="" hostpid="10652" loginname="name" isolationlevel="read committed (2)" xactid="564828018" currentdb="1" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
    <executionStack>
     <frame procname="adhoc" line="1" stmtstart="140" stmtend="528" sqlhandle="0x02000000181bbd27d32bd4895ac7599f3d0749383576066e">
UPDATE [dbo].[sss]
SET [xxx] = @0, [yyyid] = @1, [vvv] = @2, [fff] = @3, [lll] = @4
WHERE ([Id] = @5)     </frame>
     <frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
unknown     </frame>
    </executionStack>
    <inputbuf>
(@0 int,@1 int,@2 nvarchar(32),@3 datetime2(7),@4 datetime2(7),@5 int)UPDATE [dbo].[sss]
SET [xxx] = @0, [yyyid] = @1, [vvv] = @2, [fff] = @3, [lll] = @4
WHERE ([Id] = @5)
SELECT [ccc], [mmm]
FROM [dbo].[sss]
WHERE @@ROWCOUNT &gt; 0 AND [Id] = @5    </inputbuf>
   </process>
  </process-list>
  <resource-list>
   <pagelock fileid="1" pageid="1488426" dbid="10" objectname="dbname.dbo.sss" id="lock12c525200" mode="IX" associatedObjectId="72057594551795712">
    <owner-list>
     <owner id="process601ddc8" mode="IX"/>
    </owner-list>
    <waiter-list>
     <waiter id="process804e6bc8" mode="S" requestType="wait"/>
    </waiter-list>
   </pagelock>
   <pagelock fileid="1" pageid="1488427" dbid="10" objectname="dbname.dbo.sss" id="lockc3cc5c80" mode="S" associatedObjectId="72057594551795712">
    <owner-list>
     <owner id="process804e6bc8" mode="S"/>
    </owner-list>
    <waiter-list>
     <waiter id="process601ddc8" mode="IX" requestType="wait"/>
    </waiter-list>
   </pagelock>
  </resource-list>
 </deadlock>
</deadlock-list>

0 个答案:

没有答案