Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server / SQL-Server-2012

Setting deadlock priority in SQL Server

5.00/5 (1 vote)
19 Sep 2013CPOL 32.3K  
Explains how to run a script with higher priority to avoid it getting terminated in a deadlock situation.

Introduction

Sometimes critical jobs might terminated because they faced a deadlock situation and was choosen as the deadlock victim by SQL Server. The other process which was allowed to continue may be a stored procedure call from a web page, and a failure on that would have impacted a single user's session. But the failed job might impact all users of the application. In these scenarios it is ideal to run the critical jobs with a higher deadlock priority so that they are not chosen as deadlock victims.

DEADLOCK_PRIORITY

We can set this connection property to appropriate level and SQL Server will terminate one with lowest priority. This feature is available from SQL Server 2005 onwards.

By setting the deadlock priority to high we can prevent our script being terminated as deadlock victim as shown below.

SQL
SET DEADLOCK_PRIORITY HIGH

-- Your script here

SET DEADLOCK PRIORITY NORMAL

You can give a numeric priority from -10 to 10 as well incase you want to set different priorities for a set of scripts which are expected to execute simultaneously with a chance of deadlock. 

Please refer to http://technet.microsoft.com/en-us/library/ms186736.aspx for more details.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)