{"id":2199,"date":"2012-06-14T13:26:06","date_gmt":"2012-06-14T13:26:06","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=2199"},"modified":"2012-06-14T14:25:28","modified_gmt":"2012-06-14T14:25:28","slug":"deadlock","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2012\/06\/14\/deadlock\/","title":{"rendered":"deadlock"},"content":{"rendered":"<p>SQL Server selects the deadlock victim based on the following criteria:<br \/>\n1. Deadlock priority &ndash; the assigned DEADLOCK_PRIORITY of a given session determines<br \/>\nthe relative importance of it completing its transactions, if that session is<br \/>\ninvolved in a deadlock. The session with the lowest priority will always be chosen as<br \/>\nthe deadlock victim. Deadlock priority is covered in more detail later in this chapter.<br \/>\n2. Rollback cost &ndash; if two or more sessions involved in a deadlock have the same<br \/>\ndeadlock priority, then SQL Server will choose as the deadlock victim the session that<br \/>\nhas lowest estimated cost to roll back.<br \/>\n3,when occurs,it selects one of the participants as a victim, cancels that spid&rsquo;s current batch, and rolls backs his transaction in order to let the other spids continue with their work.&nbsp; The deadlock victim will get a 1205 error: &nbsp;<br \/>\nTransaction (Process ID 52) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.<\/p>\n<p>There are circumstances (for example, a critical report that performs a long running<br \/>\nSELECT that must complete even if it is the ideal deadlock victim) where it may be<br \/>\npreferable to specify which process will be chosen as the deadlock victim in the event<br \/>\nof a deadlock, rather than let SQL Server decide based purely on the cost of rollback. As<br \/>\ndemonstrated in Listing 7.18, SQL Server offers the ability to set, at the session or batch<br \/>\nlevel, a deadlock priority using the SET DEADLOCK PRIORITY option.<\/p>\n<p>&#8212; Set a Low deadlock priority<br \/>\nSET DEADLOCK_PRIORITY LOW ;<br \/>\nGO<br \/>\n&#8212; Set a High deadlock priority<br \/>\nSET DEADLOCK_PRIORITY HIGH ;<br \/>\nGO<br \/>\n&#8212; Set a numeric deadlock priority<br \/>\nSET DEADLOCK_PRIORITY 2 ;-10\u523010<\/p>\n<p>SQL Server 2005 and 2008 however, have three named deadlock priorities; Low, Normal, and High, as well<br \/>\nas a numeric range from -10 to +10, for fine-tuning the deadlock priority of different<br \/>\noperations.<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL Server selects the deadlock victim based on the fol [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2199","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/2199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=2199"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/2199\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=2199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=2199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=2199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}