Isolation Levels and Locking
When many users read and write at once, isolation levels decide what each transaction may see — and how long others must wait.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Nov 26, 2026
Related tutorials
Learning objectives#
- Explain shared and exclusive locks
- Compare the isolation levels
- Find blocking sessions
- Turn on read committed snapshot
Explanation#
Writers take exclusive locks on the rows they change until the transaction ends. Under the default READ COMMITTED level, readers ask for shared locks and must wait for those writers — that waiting is blocking.
Levels from least to most strict: READ UNCOMMITTED (NOLOCK; may read uncommitted, later rolled-back data and even skip or double-read rows), READ COMMITTED, REPEATABLE READ, SERIALIZABLE (no phantom rows), and SNAPSHOT (reads a consistent version from when the transaction started).
READ_COMMITTED_SNAPSHOT (RCSI), a database option, makes READ COMMITTED read the last committed version of rows from tempdb instead of waiting. Readers no longer block writers or the other way round — the usual fix instead of sprinkling NOLOCK. Azure SQL Database turns it on by default.
Find blockers with sys.dm_exec_requests (blocking_session_id) or SSMS Activity Monitor. Keep transactions short: never wait for user input inside one.
Step-by-step screens#
Drawings of SQL Server 2022 Setup and SQL Server Management Studio (SSMS 20) with the ShopDB sample database. The red numbers match the numbered actions under each picture; names on your computer will differ.
Loading pictures…
Code example: See blocking, then remove it with RCSI#
-- Session 1
BEGIN TRAN;
UPDATE Products SET Price = Price * 1.1 WHERE Category = 'Laptops';
-- (not committed yet)
-- Session 2 waits here under READ COMMITTED
SELECT Name, Price FROM Products WHERE Category = 'Laptops';
-- Session 3: who is blocking whom?
SELECT session_id, blocking_session_id, wait_type, wait_time, command
FROM sys.dm_exec_requests
WHERE blocking_session_id <> 0;
-- Fix: readers use row versions instead of waiting (run when no one is connected)
ALTER DATABASE ShopDB SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
session_id blocking_session_id wait_type wait_time command ---------- ------------------- --------- --------- ------- 62 58 LCK_M_S 8412 SELECT
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Session 1 holds exclusive locks on the laptop rows.
- Session 2’s SELECT needs shared locks and waits (LCK_M_S).
- The DMV shows session 62 blocked by 58 for 8.4 seconds.
- With RCSI on, the SELECT reads the previous committed prices instead of waiting.
Real-world use case#
A shop’s product pages froze whenever the nightly price update ran. Turning on RCSI let pages read the old prices until the update committed — no code changes.
Common mistakes#
It can return rows that are later rolled back, or count a row twice during page splits. Use RCSI instead.
Locks are held until commit. Keep transactions short and never open one while waiting for a user.
Best practices#
- Keep transactions short
- Prefer RCSI over NOLOCK
- Index the WHERE columns of updates (fewer locks)
- Monitor blocking regularly
Remember this#
Readers wait for writers under READ COMMITTED.
RCSI = readers read versions, no waiting.
NOLOCK = dirty reads.
Practice exercise#
Your task: Check whether READ_COMMITTED_SNAPSHOT is on for ShopDB.
Show solution
SELECT name, is_read_committed_snapshot_on FROM sys.databases WHERE name = 'ShopDB';
Mini quiz#
Which isolation level allows dirty reads?
Want more? Practise all SQL Server MCQs · SQL Server interview questions
My notes#
Saved in this browser.