Chapter 07 · Transactions, Locking, Row Versioning, Isolation, and Deadlocks

READ COMMITTED, RCSI, SNAPSHOT, REPEATABLE READ, SERIALIZABLE, and Anomalies

Compare locking and row-versioned isolation with explicit timelines, database-option prerequisites, anomalies, and update-conflict semantics.

Advanced130–170 minutesIsolation timeline labSQL Server 2025 · compatibility 170Developer/Express · database-option changesLast reviewed: August 2026

Learning outcomes

ServiceHub wants less reader/writer blocking, but “turn on snapshot” hides two different SQL Server models. READ_COMMITTED_SNAPSHOT (RCSI) changes READ COMMITTED reads to statement-level row versions. SNAPSHOT provides a transaction-level versioned view after ALLOW_SNAPSHOT_ISOLATION is enabled. Neither removes writer/writer locking or all schema blocking.

01

Compare locking READ COMMITTED, RCSI, SNAPSHOT, REPEATABLE READ, and SERIALIZABLE.

02

Distinguish statement-level from transaction-level versioning.

03

Predict nonrepeatable reads, phantoms, and snapshot update conflicts.

04

Change database options only with explicit before/after state.

05

Reject the myth that row versioning eliminates all blocking.

Database-option safety

RCSI and ALLOW_SNAPSHOT_ISOLATION are database-scoped. Record existing values, end all lab transactions, use only the disposable course database, and restore the recorded state afterward.

1. Name the guarantee before choosing the level

Locking READ COMMITTED prevents dirty reads but does not guarantee repeatable values across statements. REPEATABLE READ keeps locks on rows already read until transaction end but still allows qualifying new rows. SERIALIZABLE also protects key ranges. RCSI serves a committed version appropriate to each statement. SNAPSHOT exposes the transaction to a consistent versioned view and uses optimistic update-conflict detection.

Mode Read view Consequence
READ COMMITTED, RCSI OFF Locking committed reads Reader/writer waits possible
READ COMMITTED, RCSI ON Statement-start version Versioned reads per statement
SNAPSHOT Transaction snapshot Update conflict possible
REPEATABLE READ Locking Previously read keys protected
SERIALIZABLE Locking + ranges Phantoms prevented

2. Record the current database state

sql · configuration evidence
SELECT name,is_read_committed_snapshot_on,       snapshot_isolation_state_desc,is_accelerated_database_recovery_onFROM sys.databases WHERE name=N'ServiceHubLab';GO

Keep this output as the rollback manifest. Compatibility level and engine version are separate from these database options.

sql · enable explicit SNAPSHOT for the lab
-- Ensure other Chapter 07 transactions are closed first.ALTER DATABASE ServiceHubLab SET ALLOW_SNAPSHOT_ISOLATION ON;GOSELECT snapshot_isolation_state_desc FROM sys.databases WHERE name=N'ServiceHubLab';GO

3. SNAPSHOT is transaction-level and can reject a stale writer

sql · Session A — snapshot reader
USE ServiceHubLab;GOSET TRANSACTION ISOLATION LEVEL SNAPSHOT;BEGIN TRANSACTION;SELECT part_id,on_hand FROM lab07.Inventory WHERE part_id=10;-- Let Session B commit an update now.SELECT part_id,on_hand FROM lab07.Inventory WHERE part_id=10;-- Optional: now UPDATE the same row to observe snapshot update-conflict behavior.ROLLBACK;SET TRANSACTION ISOLATION LEVEL READ COMMITTED;GO
sql · Session B — committed writer
UPDATE lab07.Inventory SET on_hand=on_hand+1 WHERE part_id=10;SELECT * FROM lab07.Inventory WHERE part_id=10;GO

Both Session A reads should represent its transaction snapshot. If Session A then attempts to update a row changed after its snapshot began, optimistic conflict detection can abort that write. “Snapshot” therefore is not a last-writer-wins mode.

4. RCSI is statement-level READ COMMITTED versioning

sql · enable RCSI only in the disposable lab
-- Close other ServiceHubLab sessions first and retain the original setting.ALTER DATABASE ServiceHubLab SET READ_COMMITTED_SNAPSHOT ON;GOSELECT is_read_committed_snapshot_on FROM sys.databases WHERE name=N'ServiceHubLab';GO

Under RCSI, eligible READ COMMITTED SELECT statements can read versions instead of waiting on many writer locks. A later statement in the same transaction can see a newer committed value because its statement snapshot is newer. This is intentionally different from SNAPSHOT’s transaction-wide view.

5. Versioning still has write and metadata conflicts

UPDATE and DELETE still take locks; uniqueness/foreign-key checks can wait; DDL still uses schema locks; and SNAPSHOT updates can conflict. Row versioning is a concurrency model for consistent reads, not a switch that removes all contention. Measure writer latency and version-store pressure before adopting it broadly.

Wrong approach

“RCSI means no blocking.” It reduces many reader/writer lock waits, but does not remove writer/writer conflicts, schema locks, latches, or resource pressure.

6. Restore the recorded baseline

sql · rollback the database-option experiment
-- With all lab transactions closed:ALTER DATABASE ServiceHubLab SET READ_COMMITTED_SNAPSHOT OFF;ALTER DATABASE ServiceHubLab SET ALLOW_SNAPSHOT_ISOLATION OFF;GOSELECT is_read_committed_snapshot_on,snapshot_isolation_state_descFROM sys.databases WHERE name=N'ServiceHubLab';GO

If your starting values were already ON, restore those actual starting values instead of blindly using this snippet. Production change plans should record current setting, application assumptions, capacity evidence, and rollback.

Check your understanding

  1. What is the main difference between RCSI and SNAPSHOT?
  2. Does SNAPSHOT prevent every write conflict?
  3. What does SERIALIZABLE add to REPEATABLE READ?
  4. Are RCSI and ALLOW_SNAPSHOT_ISOLATION session settings?
  5. Why record the original options?
Review the answers

RCSI is normally statement-level versioning under READ COMMITTED; SNAPSHOT is transaction-level.

No; conflicting stale writes can fail and writers still use locks.

Key-range protection against phantoms.

No, they are database-scoped.

So the environment can be restored exactly rather than assuming defaults.

Authoritative references

Keep knowledge open

Help the academy stay free and grow.

If these tutorials save you time, a small donation supports new lessons, technical review, diagrams, examples, and long-term maintenance.

ETHEthereum / ERC-20 only
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0

Send only Ethereum or ERC-20 compatible assets to this address.