Created
January 28, 2022 19:51
-
-
Save darrenmaginnis/5ae856bec2737e90bba3415eba6e27aa to your computer and use it in GitHub Desktop.
Deadlock retry logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @Retry INT; | |
SET @Retry = 3; | |
SET DEADLOCK_PRIORITY LOW; | |
WHILE (@Retry > 0) | |
BEGIN | |
BEGIN TRY | |
--Code that can deadlock | |
SET @Retry = -1; | |
END TRY | |
BEGIN CATCH | |
IF (ERROR_NUMBER() = 1205) -- If deadlock victim error, then reduce retry count for next retry. | |
BEGIN | |
SET @Retry = @Retry - 1; | |
IF (@Retry = 0) | |
THROW; --Max retry reached rethrow deadlock error | |
END | |
ELSE -- If some other error occurred, then exit retry WHILE loop. | |
BEGIN | |
SET @Retry = -1; | |
THROW; | |
END | |
END CATCH | |
END | |
SET DEADLOCK_PRIORITY NORMAL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment