无法在xml_deadlock_report上看到死锁

时间:2014-10-22 12:00:11

标签: sql sql-server tsql sql-server-2012

我几个小时后就遇到了死锁错误(将近20个)。

MySp: 1205: Transaction (Process ID 55) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction. Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0

我正在尝试使用(Extented Event system_health正在运行)获取更多信息,

SELECT XEvent.query('(event/data/value/deadlock)[1]') AS DeadlockGraph 
FROM ( SELECT XEvent.query('.') AS XEvent 
       FROM ( SELECT CAST(target_data AS XML) AS TargetData 
              FROM sys.dm_xe_session_targets st 
                   JOIN sys.dm_xe_sessions s 
                   ON s.address = st.event_session_address 
              WHERE s.name = 'system_health' 
                    AND st.target_name = 'ring_buffer' 
              ) AS Data 
              CROSS APPLY 
                 TargetData.nodes 
                    ('RingBufferTarget/event[@name="xml_deadlock_report"]')
              AS XEventData ( XEvent ) 
      ) AS src;

更新:这是造成此问题的另一个SP,

            ALTER PROCEDURE [dbo].[Insertorupdatedevicecatalog]
            (
                @OS                 NVARCHAR(50) 
                ,@UniqueID          VARCHAR(500)
                ,@Longitude         FLOAT 
                ,@Latitude          FLOAT
                ,@Culture           VARCHAR(10)
                ,@Other             NVARCHAR(200)
                ,@IPAddress         VARCHAR(50)
                ,@NativeDeviceID    VARCHAR(50)
                ,@MyAppApplicationID INT = 1
                ,@LatestApplicationVersion VARCHAR(50)
            )
            AS 
            BEGIN 
                SET NOCOUNT ON;
                DECLARE @TranCount INT;
                SET @TranCount = @@TRANCOUNT;

                DECLARE @OldUniqueID VARCHAR(500) = '-1';
                SELECT @OldUniqueID = [UniqueID] FROM DeviceCatalog WHERE (@NativeDeviceID != '' AND [NativeDeviceID] = @NativeDeviceID);

                DECLARE @Geo GEOGRAPHY = geography::STGeomFromText('POINT(' + CONVERT(VARCHAR(100), @Longitude) + ' ' + CONVERT(VARCHAR(100), @Latitude) + ')', 4326);

                DECLARE @OldGeo     GEOGRAPHY
                        ,@OldCity       NVARCHAR(100)
                        ,@OldCountry    NVARCHAR(100)
                        ,@OldAddress    NVARCHAR(100);

                SELECT  @OldGeo = [LastUpdatedLocationFromJob]
                        ,@OldCity = [City]
                        ,@OldCountry = [Country]
                        ,@OldAddress = [Address]
                FROM    DeviceCatalog
                WHERE   [UniqueID] = @UniqueID;

                BEGIN TRY
                    IF @TranCount = 0
                        BEGIN TRANSACTION
                    ELSE
                        SAVE TRANSACTION Insertorupdatedevicecatalog;

                    MERGE INTO DeviceCatalog DC
                    USING (SELECT @UniqueID AS UniqueID, @NativeDeviceID AS NativeDeviceID) T ON (DC.UniqueID = T.UniqueID OR DC.[NativeDeviceID] = T.NativeDeviceID)
                    WHEN MATCHED THEN
                        UPDATE   
                            SET [OS] = @OS
                                ,[Location] = @Geo
                                ,[Culture] = @Culture
                                ,[Other] = @Other
                                ,[IPAddress] = @IPAddress
                                ,[NativeDeviceID] = CASE WHEN DC.UniqueID = T.UniqueID THEN (CASE WHEN @NativeDeviceID IS NULL OR @NativeDeviceID = '' THEN DC.[NativeDeviceID] ELSE @NativeDeviceID END) ELSE DC.[NativeDeviceID] END
                                ,[UniqueID] = CASE WHEN  DC.[NativeDeviceID] = T.NativeDeviceID AND DC.[NativeDeviceID] <> '' THEN @UniqueID ELSE DC.UniqueID END
                                ,[Lastmodifieddate] = CASE WHEN (@OldGeo IS NULL OR @OldAddress IS NULL OR @OldCity IS NULL OR @OldCountry IS NULL OR ISNULL(@Geo.STDistance(@OldGeo) / 1000,0) > 50) THEN GETDATE() ELSE [Lastmodifieddate] END
                                ,[LastUpdatedByDeviceDate] = GETDATE()
                                ,[MyAppApplicationID] = @MyAppApplicationID
                                ,[LatestApplicationVersion] = @LatestApplicationVersion
                    WHEN NOT MATCHED THEN
                        INSERT  
                                ([OS]
                                ,[UniqueID]
                                ,[Location] 
                                ,[Culture] 
                                ,[Other]
                                ,[IPAddress]
                                ,[NativeDeviceID]
                                ,[MyAppApplicationID]
                                ,[LatestApplicationVersion])
                        VALUES  (@OS
                                ,@UniqueID
                                ,@Geo
                                ,@Culture
                                ,@Other
                                ,@IPAddress
                                ,@NativeDeviceID
                                ,@MyAppApplicationID
                                ,@LatestApplicationVersion);

LBEXIT:
                    IF @TranCount = 0
                        COMMIT;

                END TRY
                BEGIN CATCH
                    DECLARE @Error INT, @Message VARCHAR(4000), @XState INT;
                    SELECT  @Error = ERROR_NUMBER() ,@Message = ERROR_MESSAGE() ,@XState = XACT_STATE();

                    IF @XState = -1
                        ROLLBACK;
                    IF @XState = 1 AND @TranCount = 0
                        rollback
                    IF @XState = 1 AND @TranCount > 0
                        ROLLBACK TRANSACTION Insertorupdatedevicecatalog;

                    RAISERROR ('Insertorupdatedevicecatalog: %d: %s', 16, 1, @error, @message) ;
                END CATCH

            END

这是它的xdl死锁报告(从探查器得到它),

<deadlock-list>
 <deadlock victim="process2a28b0188">
  <process-list>
   <process id="process2a28b0188" taskpriority="0" logused="228" waitresource="PAGE: 5:1:1183 " waittime="8632" ownerId="2111061378" transactionname="user_transaction" lasttranstarted="2014-10-22T12:07:28.620" XDES="0x2206d9c40" lockMode="U" schedulerid="4" kpid="12064" status="suspended" spid="70" sbid="0" ecid="2" priority="0" trancount="0" lastbatchstarted="2014-10-22T12:07:28.620" lastbatchcompleted="2014-10-22T12:07:28.620" lastattention="1900-01-01T00:00:00.620" clientapp=".Net SqlClient Data Provider" hostname="NORQ01" hostpid="3640" isolationlevel="read uncommitted (1)" xactid="2111061378" currentdb="5" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
    <executionStack>
     <frame procname="myapp.dbo.Insertorupdatedevicecatalog" line="44" stmtstart="2692" stmtend="6364" sqlhandle="0x03000500d291a62559cc2801e3a2000001000000000000000000000000000000000000000000000000000000">
MERGE INTO DeviceCatalog DC
                        USING (SELECT @UniqueID AS UniqueID, @NativeDeviceID AS NativeDeviceID) T ON (DC.UniqueID = T.UniqueID OR DC.[NativeDeviceID] = T.NativeDeviceID)
                        WHEN MATCHED THEN
                            UPDATE   
                                SET [OS] = @OS
                                    ,[Location] = @Geo
                                    ,[Culture] = @Culture
                                    ,[Other] = @Other
                                    ,[IPAddress] = @IPAddress
                                    ,[NativeDeviceID] = CASE WHEN DC.UniqueID = T.UniqueID THEN (CASE WHEN @NativeDeviceID IS NULL OR @NativeDeviceID = &apos;&apos; THEN DC.[NativeDeviceID] ELSE @NativeDeviceID END) ELSE DC.[NativeDeviceID] END
                                    ,[UniqueID] = CASE WHEN  DC.[NativeDeviceID] = T.NativeDeviceID AND DC.[NativeDeviceID] &lt;&gt; &apos;&apos; THEN @UniqueID ELSE DC.UniqueID END
                                    ,[Lastmodifieddate] = CASE WHEN (@OldGeo IS NULL OR @OldAddress IS NULL OR @OldCity IS NULL OR @OldCountry IS NULL OR ISNULL(@Geo.STDistance(@OldGeo) / 1000,0) &gt; 50) THEN GETDATE() ELSE [Lastmodifieddate] END
                                    ,[LastUpdatedByDeviceDate] = GETDATE()
                                    ,[myappApplicationID]     </frame>
    </executionStack>
    <inputbuf>
Proc [Database Id = 5 Object Id = 631673298]    </inputbuf>
   </process>
   <process id="process2b46be558" taskpriority="0" logused="228" waitresource="PAGE: 5:1:1183 " waittime="4650" ownerId="2111061011" transactionname="user_transaction" lasttranstarted="2014-10-22T12:07:25.357" XDES="0x1c8e65bb0" lockMode="U" schedulerid="1" kpid="9408" status="suspended" spid="51" sbid="0" ecid="3" priority="0" trancount="0" lastbatchstarted="2014-10-22T12:07:25.357" lastbatchcompleted="2014-10-22T12:07:25.357" lastattention="1900-01-01T00:00:00.357" clientapp=".Net SqlClient Data Provider" hostname="NORQ01" hostpid="3640" isolationlevel="read uncommitted (1)" xactid="2111061011" currentdb="5" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
    <executionStack>
     <frame procname="myapp.dbo.Insertorupdatedevicecatalog" line="44" stmtstart="2692" stmtend="6364" sqlhandle="0x03000500d291a62559cc2801e3a2000001000000000000000000000000000000000000000000000000000000">
MERGE INTO DeviceCatalog DC
                        USING (SELECT @UniqueID AS UniqueID, @NativeDeviceID AS NativeDeviceID) T ON (DC.UniqueID = T.UniqueID OR DC.[NativeDeviceID] = T.NativeDeviceID)
                        WHEN MATCHED THEN
                            UPDATE   
                                SET [OS] = @OS
                                    ,[Location] = @Geo
                                    ,[Culture] = @Culture
                                    ,[Other] = @Other
                                    ,[IPAddress] = @IPAddress
                                    ,[NativeDeviceID] = CASE WHEN DC.UniqueID = T.UniqueID THEN (CASE WHEN @NativeDeviceID IS NULL OR @NativeDeviceID = &apos;&apos; THEN DC.[NativeDeviceID] ELSE @NativeDeviceID END) ELSE DC.[NativeDeviceID] END
                                    ,[UniqueID] = CASE WHEN  DC.[NativeDeviceID] = T.NativeDeviceID AND DC.[NativeDeviceID] &lt;&gt; &apos;&apos; THEN @UniqueID ELSE DC.UniqueID END
                                    ,[Lastmodifieddate] = CASE WHEN (@OldGeo IS NULL OR @OldAddress IS NULL OR @OldCity IS NULL OR @OldCountry IS NULL OR ISNULL(@Geo.STDistance(@OldGeo) / 1000,0) &gt; 50) THEN GETDATE() ELSE [Lastmodifieddate] END
                                    ,[LastUpdatedByDeviceDate] = GETDATE()
                                    ,[myappApplicationID]     </frame>
    </executionStack>
    <inputbuf>
Proc [Database Id = 5 Object Id = 631673298]    </inputbuf>
   </process>
   <process id="process2bfef7c38" taskpriority="0" logused="10000" waittime="4617" schedulerid="2" kpid="1844" status="suspended" spid="51" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2014-10-22T12:07:25.357" lastbatchcompleted="2014-10-22T12:07:25.357" lastattention="1900-01-01T00:00:00.357" clientapp=".Net SqlClient Data Provider" hostname="NORQ01" hostpid="3640" loginname="myapp" isolationlevel="read uncommitted (1)" xactid="2111061011" currentdb="5" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
    <executionStack>
     <frame procname="myapp.dbo.Insertorupdatedevicecatalog" line="44" stmtstart="2692" stmtend="6364" sqlhandle="0x03000500d291a62559cc2801e3a2000001000000000000000000000000000000000000000000000000000000">
MERGE INTO DeviceCatalog DC
                        USING (SELECT @UniqueID AS UniqueID, @NativeDeviceID AS NativeDeviceID) T ON (DC.UniqueID = T.UniqueID OR DC.[NativeDeviceID] = T.NativeDeviceID)
                        WHEN MATCHED THEN
                            UPDATE   
                                SET [OS] = @OS
                                    ,[Location] = @Geo
                                    ,[Culture] = @Culture
                                    ,[Other] = @Other
                                    ,[IPAddress] = @IPAddress
                                    ,[NativeDeviceID] = CASE WHEN DC.UniqueID = T.UniqueID THEN (CASE WHEN @NativeDeviceID IS NULL OR @NativeDeviceID = &apos;&apos; THEN DC.[NativeDeviceID] ELSE @NativeDeviceID END) ELSE DC.[NativeDeviceID] END
                                    ,[UniqueID] = CASE WHEN  DC.[NativeDeviceID] = T.NativeDeviceID AND DC.[NativeDeviceID] &lt;&gt; &apos;&apos; THEN @UniqueID ELSE DC.UniqueID END
                                    ,[Lastmodifieddate] = CASE WHEN (@OldGeo IS NULL OR @OldAddress IS NULL OR @OldCity IS NULL OR @OldCountry IS NULL OR ISNULL(@Geo.STDistance(@OldGeo) / 1000,0) &gt; 50) THEN GETDATE() ELSE [Lastmodifieddate] END
                                    ,[LastUpdatedByDeviceDate] = GETDATE()
                                    ,[myappApplicationID]     </frame>
    </executionStack>
    <inputbuf>
Proc [Database Id = 5 Object Id = 631673298]    </inputbuf>
   </process>
  </process-list>
  <resource-list>
   <pagelock fileid="1" pageid="1183" dbid="5" subresource="FULL" objectname="myapp.dbo.devicecatalog" id="lock2edbca800" mode="U" associatedObjectId="72057594051952640">
    <owner-list>
     <owner id="process2bfef7c38" mode="U"/>
    </owner-list>
    <waiter-list>
     <waiter id="process2a28b0188" mode="U" requestType="wait"/>
    </waiter-list>
   </pagelock>
   <pagelock fileid="1" pageid="1183" dbid="5" subresource="FULL" objectname="myapp.dbo.devicecatalog" id="lock2edbca800" mode="U" associatedObjectId="72057594051952640">
    <owner-list>
     <owner id="process2a28b0188" mode="U" requestType="wait"/>
    </owner-list>
    <waiter-list>
     <waiter id="process2b46be558" mode="U" requestType="wait"/>
    </waiter-list>
   </pagelock>
   <exchangeEvent id="Pipe222b92900" WaitType="e_waitPipeGetRow" nodeId="9">
    <owner-list>
     <owner id="process2b46be558"/>
    </owner-list>
    <waiter-list>
     <waiter id="process2bfef7c38"/>
    </waiter-list>
   </exchangeEvent>
  </resource-list>
 </deadlock>
</deadlock-list>

0 个答案:

没有答案