- Error number
- 1205
- Severity
- 13 (user-correctable error)
- State
- 51
- Product
- Microsoft SQL Server Database Engine
- Versions
- All supported SQL Server versions (2016 to 2025), Azure SQL Database, Azure SQL Managed Instance
- Category
- Concurrency & Locking
- Last reviewed
- 23 October 2026
On this page
What it means
Error 1205 means your transaction and another one were each waiting for a lock the other held, so SQL Server rolled yours back to break the cycle and asks you to run it again.
A deadlock is a circle of waits: session A holds a lock that B needs while B holds a lock that A needs. Neither can ever finish, so the deadlock monitor picks one victim, rolls back its whole transaction, releases its locks and raises 1205 in that session. The other session carries on. Process ID is the victim session id (the spid in the deadlock graph); lock resources says the cycle was on ordinary row, page or object locks.
Severity 13 means a transaction-level problem in your workload, not damage to the database. Because the victim was rolled back completely, it is safe to rerun the same transaction, which is why the message says so. If the same deadlock keeps coming back, the deadlock graph shows the two statements and the locks they collided on; that is what you change. See transactions, locks and deadlocks.
Common causes
- Two transactions update the same rows or tables in opposite order (A then B, and B then A).
- A transaction reads with shared locks and then writes, while another does the same on the other table: reader and writer block each other under the default READ COMMITTED isolation.
- Transactions stay open too long (user think time, slow calls to other systems, large batches), so the window for a collision is wide.
- Missing indexes make UPDATE, DELETE or SELECT scan and lock many more rows than they need, including rows another session is changing.
- Higher isolation levels (REPEATABLE READ, SERIALIZABLE) or lock hints such as UPDLOCK and HOLDLOCK hold more locks for longer.
Diagnose it
Read-only These queries only read metadata and dynamic management views. They do not change data, permissions or configuration, so they are safe to run first.
Read recent deadlock graphs from the system_health session
SQL Server records every deadlock in the built-in system_health Extended Events session, which is on by default. This reads its event files; no setup or trace flag is needed. Applies to SQL Server and Azure SQL Managed Instance.
SELECT TOP (10)
CAST(event_data AS xml).value('(event/@timestamp)[1]', 'datetime2(0)') AS deadlock_time_utc,
CAST(event_data AS xml).query('(event/data/value/deadlock)[1]') AS deadlock_graph
FROM sys.fn_xe_file_target_read_file(N'system_health*.xel', NULL, NULL, NULL)
WHERE object_name = N'xml_deadlock_report'
ORDER BY deadlock_time_utc DESC; One row per deadlock. Click the XML in SSMS and save it as a .xdl file to see the graph: two process ovals, the resources between them, and a cross on the victim.
Summarise each deadlock: victim, statement and resource
WITH dl AS (
SELECT CAST(event_data AS xml) AS ev
FROM sys.fn_xe_file_target_read_file(N'system_health*.xel', NULL, NULL, NULL)
WHERE object_name = N'xml_deadlock_report'
)
SELECT dl.ev.value('(event/@timestamp)[1]', 'datetime2(0)') AS deadlock_time_utc,
CASE WHEN p.value('@id', 'varchar(50)') = dl.ev.value('(event/data/value/deadlock/victim-list/victimProcess/@id)[1]', 'varchar(50)')
THEN 'victim' ELSE 'survivor' END AS outcome,
p.value('@spid', 'int') AS spid,
DB_NAME(p.value('@currentdb', 'int')) AS database_name,
p.value('@waitresource', 'varchar(200)') AS wait_resource,
p.value('@lockMode', 'varchar(10)') AS lock_mode_requested,
p.value('@isolationlevel', 'varchar(50)') AS isolation_level,
LTRIM(p.value('(inputbuf)[1]', 'nvarchar(4000)')) AS input_buffer
FROM dl
CROSS APPLY dl.ev.nodes('event/data/value/deadlock/process-list/process') AS x(p)
ORDER BY deadlock_time_utc DESC, outcome; Two (or more) rows per deadlock. Compare the input buffers: the same tables touched in a different order, or a read followed by a write, is the usual pattern. wait_resource such as KEY: 14:72057594047758336 (61a06abd401c) is the locked row.
How many deadlocks since the instance started?
SELECT instance_name AS lock_type, cntr_value AS deadlocks_since_start
FROM sys.dm_os_performance_counters
WHERE counter_name LIKE N'Number of Deadlocks/sec%'
AND cntr_value > 0; Despite the name, cntr_value is a running total. Sample it twice to get a rate; a steadily rising _Total means deadlocks are routine, not a one-off.
Which isolation settings does the database use?
SELECT name, is_read_committed_snapshot_on, snapshot_isolation_state_desc
FROM sys.databases
WHERE name = N'AppDb'; is_read_committed_snapshot_on = 0 means plain READ COMMITTED readers take shared locks and can deadlock with writers, which READ_COMMITTED_SNAPSHOT avoids.
How to fix it
Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.
Retry the transaction on 1205
Client or network sideThe victim was rolled back completely, so rerunning it is safe and usually succeeds. Retry the whole transaction, not just the failed statement, a small number of times with a short, growing pause, and log each deadlock so it can be fixed properly. The same logic can live in application code (check for error number 1205).
DECLARE @attempt INT = 1;
WHILE 1 = 1
BEGIN
BEGIN TRY
BEGIN TRANSACTION;
UPDATE dbo.Accounts SET balance = balance - 100 WHERE id = 1;
UPDATE dbo.Accounts SET balance = balance + 100 WHERE id = 2;
COMMIT TRANSACTION;
BREAK;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
IF ERROR_NUMBER() = 1205 AND @attempt < 3
BEGIN
SET @attempt += 1;
WAITFOR DELAY '00:00:00.200';
END
ELSE
THROW;
END CATCH
END;
Access objects and rows in one consistent order
Client or network sideIf every transaction takes its locks in the same order, a circle cannot form; one session simply waits for the other. For transfers between two rows, always touch the lower id first, whatever the direction of the transfer.
DECLARE @from INT = 2, @to INT = 1, @amount DECIMAL(12,2) = 100;
DECLARE @first INT = IIF(@from < @to, @from, @to),
@second INT = IIF(@from < @to, @to, @from);
BEGIN TRANSACTION;
-- Always lock the lower id first, whichever way the money moves
UPDATE dbo.Accounts SET balance = balance + IIF(id = @from, -@amount, @amount) WHERE id = @first;
UPDATE dbo.Accounts SET balance = balance + IIF(id = @from, -@amount, @amount) WHERE id = @second;
COMMIT TRANSACTION;
Keep transactions short
Client or network sideRead and validate before BEGIN TRANSACTION, keep only the writes inside it, and commit straight away. Never wait for a user, a file or a web service while a transaction is open. Break very large updates into batches that commit.
Add the index the statements need
Changes schemaWhen the deadlock graph shows a scan, a supporting index lets the statement seek to its rows and lock only those, so it stops colliding with rows other sessions are using. In the reader/writer example below, an index on Orders(product_id) narrows the SELECT. Test on a copy: an extra index adds work to every write.
CREATE INDEX IX_Orders_product_id ON dbo.Orders (product_id);
Turn on READ_COMMITTED_SNAPSHOT so readers stop taking shared locks
Changes configurationWith RCSI, READ COMMITTED readers see the last committed version of a row instead of waiting for writers, which removes reader/writer deadlocks (writer/writer deadlocks remain). It is the default in Azure SQL Database. Test the application first and plan a quiet moment to switch it on.
Caution: This changes how every READ COMMITTED query in the database behaves: code that relied on a read blocking until a writer committed may now read the previous version. Row versions add tempdb (or persistent version store) usage. The ALTER needs to be the only active connection in the database and waits until it is.
ALTER DATABASE AppDb SET READ_COMMITTED_SNAPSHOT ON;
Examples
Two sessions update two rows in opposite order
-- Setup
CREATE TABLE dbo.Accounts (id INT PRIMARY KEY, balance DECIMAL(12,2) NOT NULL);
INSERT INTO dbo.Accounts (id, balance) VALUES (1, 500), (2, 500);
-- Session 1
BEGIN TRANSACTION;
UPDATE dbo.Accounts SET balance = balance - 100 WHERE id = 1; -- locks row 1
WAITFOR DELAY '00:00:03';
UPDATE dbo.Accounts SET balance = balance + 100 WHERE id = 2; -- waits for session 2
COMMIT TRANSACTION;
-- Session 2, started straight after session 1 in another window
BEGIN TRANSACTION;
UPDATE dbo.Accounts SET balance = balance - 50 WHERE id = 2; -- locks row 2
WAITFOR DELAY '00:00:03';
UPDATE dbo.Accounts SET balance = balance + 50 WHERE id = 1; -- waits for session 1: deadlock
COMMIT TRANSACTION;
Msg 1205, Level 13, State 51, Line 4 Transaction (Process ID 98) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.
A reader and a writer deadlock across two tables
-- Setup (READ_COMMITTED_SNAPSHOT is off)
CREATE TABLE dbo.Stock (product_id INT PRIMARY KEY, qty INT NOT NULL);
CREATE TABLE dbo.Orders (id INT PRIMARY KEY, product_id INT NOT NULL, status VARCHAR(10) NOT NULL);
INSERT INTO dbo.Stock VALUES (1, 10);
INSERT INTO dbo.Orders VALUES (1, 1, 'new'), (2, 1, 'new');
-- Session 1
BEGIN TRANSACTION;
UPDATE dbo.Stock SET qty = qty - 1 WHERE product_id = 1;
WAITFOR DELAY '00:00:03';
SELECT COUNT(*) AS open_orders FROM dbo.Orders WHERE product_id = 1; -- scan meets session 2's lock
COMMIT TRANSACTION;
-- Session 2, started straight after session 1
BEGIN TRANSACTION;
UPDATE dbo.Orders SET status = 'paid' WHERE id = 1;
WAITFOR DELAY '00:00:03';
SELECT qty FROM dbo.Stock WHERE product_id = 1; -- waits for session 1: deadlock
COMMIT TRANSACTION;
Msg 1205, Level 13, State 51, Line 4 Transaction (Process ID 98) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.
Choosing the victim with SET DEADLOCK_PRIORITY
-- Same tables as the previous example. Session 1 (normal priority)
BEGIN TRANSACTION;
UPDATE dbo.Stock SET qty = qty + 1 WHERE product_id = 1;
WAITFOR DELAY '00:00:03';
UPDATE dbo.Orders SET status = 'new' WHERE id = 1;
COMMIT TRANSACTION;
-- Session 2: a background job that should lose any deadlock
SET DEADLOCK_PRIORITY LOW;
BEGIN TRANSACTION;
UPDATE dbo.Orders SET status = 'paid' WHERE id = 1;
WAITFOR DELAY '00:00:03';
UPDATE dbo.Stock SET qty = qty - 1 WHERE product_id = 1; -- session 2 is the victim
COMMIT TRANSACTION;
Msg 1205, Level 13, State 51, Line 5 Transaction (Process ID 53) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.
How to prevent it
Design transactions to be short and to take locks in one agreed order across the application. Index the columns used to find rows in UPDATE, DELETE and in reads inside write transactions, and consider READ_COMMITTED_SNAPSHOT for read-heavy OLTP databases so readers stop blocking writers. Do not use NOLOCK as a deadlock fix: it reads uncommitted and sometimes duplicated or missing rows.
Treat 1205 as an expected, retryable error in data access code, and give background jobs SET DEADLOCK_PRIORITY LOW so user requests win. Review the system_health deadlock graphs regularly: repeat deadlocks between the same two statements are a design problem that a code or index change removes. See SQL transactions and ACID explained.
Common questions
Is my data safe after a deadlock?
Yes. The victim transaction is rolled back as a whole and the other one commits normally, so nothing is left half written. Rerun the victim transaction.
How does SQL Server choose the deadlock victim?
The session with the lowest DEADLOCK_PRIORITY loses. With equal priorities, the transaction that is cheapest to roll back is chosen. In our test, SET DEADLOCK_PRIORITY LOW made the same session the victim every time.
Do I need trace flag 1222 to see deadlock graphs?
No. The built-in system_health Extended Events session already records every deadlock as xml_deadlock_report on SQL Server and Azure SQL Managed Instance; the diagnostic query above reads it.
What is the difference between error 1205 and error 1222?
In 1205 SQL Server found a circle of waits and killed one transaction. In 1222 one statement simply waited longer than its LOCK_TIMEOUT for a lock and gave up; nothing was chosen as a victim and the transaction stays open.
Learn more on CodeWithSQL
- Transactions, locks and deadlocks
- SQL transactions and ACID explained
- How SQL indexes improve performance
Official documentation
- MSSQLSERVER_1205 (Microsoft Learn)
- Deadlocks guide (Microsoft Learn)
- Use the system_health session (Microsoft Learn)
- SET DEADLOCK_PRIORITY (Transact-SQL)
Last technically reviewed 23 October 2026; examples and diagnostics run on SQL Server 2025 (17.0.1145.4). Message text checked against the engine's sys.messages catalogue.