Snapshot isolation
Overview and setup
A snapshot provides a consistent view of committed data at a defined point, plus the transaction’s own changes. Snapshot isolation in the classical sense is not necessarily serializability: write skew may occur. SQLite WAL gives stable reader snapshots and serializes writes, rejecting an attempt to upgrade a stale snapshot into a writer.
Use a fresh disposable file database initialized with versions/v55/isolation-fixture.sql. Enable WAL with PRAGMA journal_mode=WAL; before opening two independent private-cache connections A and B. Use driver autocommit, no implicit transaction, zero busy timeout, and fully fetch each query result. Follow steps in order; the included Python validator automates this. Never use production data.
First two-session example
Run each numbered statement in the indicated connection; do not paste the whole schedule into one session.
1. Session A
BEGIN;2. Session A
SELECT cents FROM iso_account WHERE id=1;Observed rows: [[100]]
3. Session B
UPDATE iso_account SET cents=120 WHERE id=1;4. Session A
UPDATE iso_account SET cents=cents+10 WHERE id=1;Observed error: SQLITE_BUSY_SNAPSHOT
5. Session A
ROLLBACK;6. Session A
SELECT cents FROM iso_account WHERE id=1;Observed rows: [[120]]
How it works
The first schedule observes SQLITE_BUSY_SNAPSHOT after B commits. A must end its stale transaction before trying again. In the second, both staff initially see two on call; A commits going off call, B cannot upgrade its stale snapshot. Final count is one. This is a blocked write-skew attempt, not a reproduced write skew. PostgreSQL Repeatable Read uses snapshot isolation and can allow the two distinct writes described in the previous lesson.
Second two-session example
Run each numbered statement in the indicated connection; do not paste the whole schedule into one session.
1. Session A
BEGIN;2. Session B
BEGIN;3. Session A
SELECT SUM(on_call) FROM iso_shift;Observed rows: [[2]]
4. Session B
SELECT SUM(on_call) FROM iso_shift;Observed rows: [[2]]
5. Session A
UPDATE iso_shift SET on_call=0 WHERE id=1;6. Session A
COMMIT;7. Session B
UPDATE iso_shift SET on_call=0 WHERE id=2;Observed error: SQLITE_BUSY_SNAPSHOT
8. Session B
ROLLBACK;9. Session B
SELECT SUM(on_call) FROM iso_shift;Observed rows: [[1]]
Practice and mistakes
Practice: after the error, rollback B and read the count again. Decide whether B may leave. Hint 1: do not reuse the old count. Hint 2: at least one staff member must remain on call. Mistake: treating all snapshot-based isolation mechanisms as identical.