← Back to SQL, page 12

sql-0227 · Medium mid

While connection A has an uncommitted UPDATE in SQLite, what does connection B read?

B reads the last committed value, not A's pending change, so SQLite does not allow dirty reads. In the default rollback-journal mode, B can read while A holds its write lock but before A starts writing to the database file at commit. After A commits, B sees the new value.

Full answer
sql
CREATE TABLE accounts (id INTEGER PRIMARY KEY, owner TEXT, balance INTEGER NOT NULL CHECK (balance >= 0));
INSERT INTO accounts VALUES (1, 'Ana', 100), (2, 'Bilal', 50);

A starts a transaction and changes Ana's balance without committing:

sql
BEGIN;
UPDATE accounts SET balance = 70 WHERE id = 1;
SELECT balance FROM accounts WHERE id = 1;
balance
70

B reads the same row:

sql
SELECT balance FROM accounts WHERE id = 1;
balance
100

B sees 100, the committed value. A then commits:

sql
COMMIT;
SELECT 'committed' AS a_status;
a_status
committed
sql
SELECT balance FROM accounts WHERE id = 1;
balance
70

Now B sees 70. No isolation level in SQLite exposes uncommitted data from another connection. (The read_uncommitted pragma applies only to connections sharing a cache in one process.)

sql
CREATE TABLE accounts (id INTEGER PRIMARY KEY, owner TEXT, balance INTEGER NOT NULL CHECK (balance >= 0));
INSERT INTO accounts VALUES (1, 'Ana', 100), (2, 'Bilal', 50);

BEGIN;
UPDATE accounts SET balance = 70 WHERE id = 1;
SELECT balance FROM accounts WHERE id = 1;

-- second session
SELECT balance FROM accounts WHERE id = 1;

COMMIT;
SELECT 'committed' AS a_status;

-- second session
SELECT balance FROM accounts WHERE id = 1;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • SQLite never shows another connection's uncommitted changes
  • Readers see the last committed state
  • After COMMIT, new reads see the change
  • Dirty reads are prevented by default

Common mistakes

  • Assuming a reader blocks until the writer commits
  • Confusing shared-cache read_uncommitted with normal behaviour

Follow-ups

  • What happens if B tries to write while A holds the lock?
  • How does WAL mode change reader behaviour?

Tips

  • Narrate the A and B timeline