{"id":3428,"date":"2015-12-16T09:57:01","date_gmt":"2015-12-16T14:57:01","guid":{"rendered":"https:\/\/academy.sqlbak.com\/?p=3428"},"modified":"2023-10-17T04:55:46","modified_gmt":"2023-10-17T08:55:46","slug":"transaction-process-id-was-deadlocked-on-lock-resources-with-another-process-and-has-been-chosen-as-the-deadlock-victim-msg-1205","status":"publish","type":"post","link":"https:\/\/academy.sqlbak.com\/transaction-process-id-was-deadlocked-on-lock-resources-with-another-process-and-has-been-chosen-as-the-deadlock-victim-msg-1205\/","title":{"rendered":"Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim (Msg 1205)"},"content":{"rendered":"
“Transaction was deadlocked” error occurs when two or more sessions are waiting to get a lock on a resource which has already locked by another session in the same blocking chain. \u00a0As a result, none of the sessions can be completed and SQL Server has to intervene to solve this problem. It gets rid of the deadlock by automatically choosing one of the sessions as a\u00a0victim and kills it allowing the other session to continue. In such case, the client receives the following error message:<\/p>\n
Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.<\/span><\/pre>\nand the killed session is rolled back. As a rule, the victim is the session that requires the least amount of overhead to rollback.<\/p>\n
Why SQL Server Deadlocks Happen?<\/h2>\n
To understand how “Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction<\/em><\/span>” error happens let’s consider a very simple example.<\/p>\nLet’s create two tables “t1” and “t2” containing only one integer column:<\/p>\n
CREATE TABLE<\/span> t1 (id int<\/span>)\r\nCREATE TABLE<\/span> t2 (id int<\/span>)<\/pre>\nand fill them with some data:<\/p>\n
INSERT INTO<\/span> t1 (id) \r\nSELECT<\/span> 1 UNION<\/span> ALL\r\nSELECT<\/span> 2 UNION<\/span> ALL\r\nSELECT<\/span> 3\r\nGO<\/span>\r\nINSERT INTO<\/span> t2 (id)\r\nSELECT<\/span> 1 UNION<\/span> ALL\r\nSELECT<\/span> 2 UNION<\/span> ALL\r\nSELECT<\/span> 3<\/pre>\nNow, suppose we started a transaction that deletes rows with id=2 from t1:<\/p>\n
BEGIN TRAN\r\nDELETE FROM<\/span> t1 WHERE<\/span> id = 2<\/pre>\nThen, assume that the other transaction is going to delete the same rows from both tables:<\/p>\n
BEGIN TRAN\r\nDELETE FROM<\/span> t2 WHERE<\/span> id = 2\r\nDELETE FROM<\/span> t1 WHERE<\/span> id = 2<\/pre>\nIt needs to be waiting for the first transaction to complete and release table t1.<\/p>\n
But, assume that the first transaction now deletes the same row from the second table:<\/p>\n
DELETE FROM<\/span> t2 WHERE<\/span> id = 2<\/pre>\nAfter executing this statement you should receive the following error message:<\/p>\n
Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.<\/span><\/pre>\nIt is caused by a situation when the first transaction is waiting for the second one (to release t2) while the second transaction is also waiting the first (to release t1) one in the same time.<\/p>\n
How to Analyze Deadlock Graphs<\/h2>\n
A deadlock graph is a block of information showing what resources and sessions are involved in a deadlock. It helps to understand why the deadlock happened.<\/p>\n
Before SQL Servers 2008, in order to capture this information you had to set up a server-side tracing or enable trace flags\u00a0and then wait while the deadlock occurs. Starting from SQL Server 2008 everything is much easier. You can retrieve a deadlock graphs retrospectively from the extended events\u00a0“system_health” session. To do this, go to “Management” > “Extended Events” > “Sessions” > “system_health” > “package0.event_file” and click “View Target Data…”<\/p>\n
<\/p>\n
<\/p>\n
<\/p>\n
Thousands of events will be shown in the opened window. There you can find deadlock reports which marked as “xml_deadlock_report”. Let’s choose one we’ve just simulated<\/p>\n
<\/p>\n
and look at its deadlock graph (in form of XML) details consisting of resources and processes sections.<\/p>\n
Resources<\/strong> section<\/strong>\u00a0displays the lists with all the resources which were involved in the deadlock: <\/a><\/p>\nIt shows what processes were fighting over and what types of locks they were causing. It has two or more entries. Each entry has a description of the resource followed by the lists of the processes that held a lock or requested a lock on that resource. Locks in that section mainly will relate to a key, RID, a page or a table.<\/p>\n
After the resources section let’s turn to the processes section to find out what those processes\u00a0were doing.<\/p>\n