One slow query is annoying. A pile of sessions all waiting on one forgotten transaction can stop an entire application. This guide shows how to see who is blocking whom in SQL Server, find the session at the root of the chain and fix the problem without making things worse.
What blocking actually is
Blocking happens when one session holds a lock that another session needs. The second session simply waits. This is normal for a few milliseconds. It becomes a problem when a transaction stays open for seconds or minutes and a queue builds up behind it.
Do not confuse blocking with a deadlock. In a deadlock two sessions wait for each other and SQL Server kills one of them automatically. Blocking never resolves on its own until the blocker finishes.
Step 1: See who is waiting
This query lists every request that is currently blocked, how long it has waited and what it was trying to run:
SELECT r.session_id,
r.blocking_session_id,
r.wait_type,
r.wait_time / 1000.0 AS wait_seconds,
DB_NAME(r.database_id) AS database_name,
s.login_name,
s.host_name,
t.text AS running_sql
FROM sys.dm_exec_requests AS r
JOIN sys.dm_exec_sessions AS s ON s.session_id = r.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
WHERE r.blocking_session_id <> 0
ORDER BY r.wait_time DESC;
The column blocking_session_id tells you which session is in the way. A wait_type starting with LCK_M_ confirms a lock wait.
Step 2: Find the head blocker
Blocking often forms a chain: A blocks B, B blocks C. Fixing C or B achieves nothing. You want the session at the top, which blocks others but is not blocked itself:
SELECT DISTINCT blocking_session_id AS head_blocker
FROM sys.dm_exec_requests
WHERE blocking_session_id <> 0
AND blocking_session_id NOT IN (SELECT session_id
FROM sys.dm_exec_requests
WHERE blocking_session_id <> 0);
Step 3: See what the blocker is doing
The head blocker is frequently sleeping: it finished its statement but never committed, so it does not appear in sys.dm_exec_requests. Look at its last statement and its open transaction instead (replace 55 with your session id):
SELECT c.session_id, t.text AS last_sql
FROM sys.dm_exec_connections AS c
CROSS APPLY sys.dm_exec_sql_text(c.most_recent_sql_handle) AS t
WHERE c.session_id = 55;
SELECT s.session_id, s.login_name, s.host_name, s.program_name,
s.open_transaction_count, s.last_request_end_time
FROM sys.dm_exec_sessions AS s
WHERE s.session_id = 55;
A session with open_transaction_count > 0 and an old last_request_end_time is the classic culprit: an application or a person in Management Studio ran BEGIN TRAN and walked away.
Step 4: Resolve it
- Ask first. If it is a person or a known job, let them commit or roll back.
- Kill only when you must.
KILL 55;forces a rollback of everything that session did. A large transaction can take as long to roll back as it took to run, and the locks stay held during the rollback. - Check progress of a rollback with
KILL 55 WITH STATUSONLY;
Never kill system sessions (session id below 50) and never kill a session you do not understand on a production server without telling its owner.
How to prevent it
- Keep transactions short. Do the slow work (calling an API, waiting for user input) outside the transaction.
- Index the filter columns. A scan locks far more rows than a seek.
- Consider row versioning. With read committed snapshot isolation, readers stop blocking on writers:
ALTER DATABASE YourDatabase
SET READ_COMMITTED_SNAPSHOT ON
WITH ROLLBACK IMMEDIATE;
Test this in a non-production copy first: it changes how reads behave and increases tempdb usage.
Quick checklist
- List blocked requests (step 1).
- Identify the head blocker (step 2).
- Read its last statement and open transaction count (step 3).
- Contact the owner, commit or roll back, kill as the last resort (step 4).
- Fix the root cause: shorter transactions, better indexes, row versioning.
More tutorials like this
Msg 208 Invalid Object Name in SQL Server: Causes and Fix
Fix SQL Server Msg 208 Invalid object name: check the spelling, the database and the schema, with real SSMS examples and scripts to find…
SQL Server Query Store Tutorial: Find Slow Queries, Compare Plans and Force a Plan
Turn on Query Store, find the most expensive queries with the sys.query_store views and the SSMS reports, compare the plans of one query and…
SQL Server Full Text Search Tutorial: CONTAINS, FORMSOF and CONTAINSTABLE
Search words inside text columns fast: create a full text catalog and index, use CONTAINS, word forms, phrases and rank the best matches with…
