Lesson 1 of 2 Blocking and Deadlocks

Diagnosing and Resolving Blocking

This lesson explains how blocking chains form, how to find the head blocker and how to prevent long blocking.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the causes of long blocking chains.
  • Identify the head blocker with DMVs.
  • Describe the fixes that shorten or avoid lock waits.

How blocking occurs

Locks protect data consistency: writers take exclusive locks on rows or pages they change, held until the transaction ends, and other sessions that need incompatible locks wait. Short waits are normal. Problems arise when the head blocker holds locks for a long time, because it runs a long transaction, scans many rows due to a missing index, waits on the application (for example a user prompt between statements), or sits idle with an open transaction after an error or timeout. Other sessions queue behind it, forming a blocking chain.

Select all that apply

Why a head blocker holds locks

Exam skills 2.2.4

Identify the usual reasons a head blocker holds locks for a long time.

Log in to try this activity Interactive activities are free with an account.

Finding the blocking chain

Blocked requests show a non-zero blocking_session_id in sys.dm_exec_requests and LCK_M_* wait types. The head blocker is a session that blocks others but is not blocked itself, and it may not appear in sys.dm_exec_requests if it is idle, so sys.dm_exec_sessions (open_transaction_count) and sys.dm_exec_connections (most_recent_sql_handle) show what it last ran. sys.dm_tran_locks shows the locked resources.

SELECT s.session_id, s.status, s.open_transaction_count, s.login_name,
       r.blocking_session_id, r.wait_type, r.wait_time,
       t.text AS last_statement
FROM sys.dm_exec_sessions AS s
LEFT JOIN sys.dm_exec_requests AS r ON r.session_id = s.session_id
JOIN sys.dm_exec_connections AS c ON c.session_id = s.session_id
CROSS APPLY sys.dm_exec_sql_text(c.most_recent_sql_handle) AS t
WHERE s.session_id IN (SELECT blocking_session_id FROM sys.dm_exec_requests WHERE blocking_session_id <> 0)
   OR r.blocking_session_id <> 0;

In Azure SQL Database, the Blocks diagnostic log category and the blocked process report extended event record blocking over time for later analysis.

The following diagram shows a blocking chain with its head blocker.

A blocking chain. Session 51 holds a lock in an open transaction and is idle, so it may not appear in sys.dm_exec_requests. Session 64 waits for it with an LCK_M wait type, and sessions 70 and 72 wait for session 64. Session 51 is the head blocker: it blocks others but is not blocked itself. sys.dm_exec_sessions shows its open transaction count, and sys.dm_exec_connections shows the statement it last ran.
Resolve the head blocker and the chain clears.

Resolving blocking

Fixes target the reason locks are held long or are taken at all, as the following list explains.

  • Keep transactions short: do work that does not need a transaction, such as reading reference data or calling services, before BEGIN TRANSACTION, and never wait for users inside one.
  • Index so that UPDATE and DELETE statements seek directly to the rows they change instead of scanning and locking many rows.
  • Use row versioning (RCSI, on by default in Azure SQL Database) so readers do not take shared locks.
  • Break large modifications into batches of a few thousand rows to avoid lock escalation to the table.
  • Use SET XACT_ABORT ON so timeouts and errors do not leave transactions open.
  • As an emergency measure, end the head blocker's session with KILL, which rolls back its transaction.
Match the pairs

Fixing blocking

Exam skills 2.2.4

Match each cause of blocking with its fix.

Log in to try this activity Interactive activities are free with an account.
Worked example

An idle session blocks orders

Order inserts hang every afternoon. The DMVs show that session 87 blocks them; it is sleeping, has open_transaction_count = 1 and its last statement is an UPDATE of the Orders table run by a reporting tool that a user left open. What is the cause, and what prevents it?

Session 87 began a transaction, ran an UPDATE and never committed, so it holds exclusive locks while idle. The immediate fix is to commit or roll back (or KILL the session); the lasting fix is to change the tool or procedure so it does not leave transactions open, for example by using autocommit for ad hoc work and SET XACT_ABORT ON in procedures. RCSI would not help here because the blocked statements are inserts and updates needing locks the idle session holds, not reads.

Check yourself

Why might the head blocker be missing from sys.dm_exec_requests?

Show answer

It can be idle (sleeping) with an open transaction, so it has no current request; sys.dm_exec_sessions shows it.

Exam Tips

Remember for the exam

  • Find the head blocker: blocking_session_id, then the session that is not blocked itself.
  • Sleeping session with open_transaction_count > 0: an uncommitted transaction holding locks.
  • Short transactions, supporting indexes and batching reduce blocking.
  • RCSI removes reader-writer blocking but not writer-writer blocking.