事务在另一个进程的锁资源上死锁,并被选为死锁

时间:2016-10-09 17:00:51

标签: sql sql-server transactions sql-server-2014

我有以下程序来更新我的预订和其他相关表格。

但我们经常会收到错误

Transaction (Process ID 51) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

CREATE PROCEDURE [dbo].[UpdateBookingDetails] (
     @ParmsList
    )
AS
BEGIN
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
    SET NOCOUNT ON

    BEGIN TRY
        BEGIN TRANSACTION UPDATEBOOKINGTRAN


            UPDATE Booking          

            SET @RecordsAffected = @@ROWCOUNT   

            INSERT INTO Payment 

            DELETE  FROM AdditionalBookingCategories

            INSERT AdditionalBookingCategories 

            DELETE FROM BookingAssistants

            INSERT INTO BookingAssistants 

            DELETE FROM BookingResource

            INSERT BookingResource 


        COMMIT TRANSACTION UPDATEBOOKINGTRAN
    END TRY

    BEGIN CATCH
        IF (@@TRANCOUNT > 0)
        BEGIN
            ROLLBACK TRANSACTION UPDATEBOOKINGTRAN

            PRINT 'Error detected, all changes reversed'
        END

        -- ERROR HANDLING - Grab Error code and throw  
        DECLARE @ErrorMessage NVARCHAR(4000)
            ,@ErrorProc NVARCHAR(126)
            ,@ErrorLineNo INT;

        SELECT @ErrorLineNo = ERROR_LINE()
            ,@ErrorMessage = ERROR_MESSAGE()
            ,@ErrorProc = ERROR_PROCEDURE();

        RAISERROR (
                'Error %s occurred in %s. Line  %d'
                ,16
                ,1
                ,@ErrorMessage
                ,@ErrorProc
                ,@ErrorLineNo
                )
        WITH SETERROR;
    END CATCH
END

我无法找出我的手术有什么问题。它看起来不错,但死锁是如何出现的?我的程序中是否缺少处理死锁的任何内容?

它很少发生,但每天至少发生5次。我无法复制,因为它需要同时更新。

0 个答案:

没有答案