skipnothing/SQL

Isolation Levels & Locks

Two clerks open the accounts table at once. One reads a balance of 100, the other commits a change to 120, and the first reads that same row again, now seeing a number that wasn't there when its transaction began.

  • ▸Run the same SELECT twice in one transaction and a neighbor's commit can hand you a different answer.
  • ▸Each step up in isolation hides at least as much as the step below; the top two steps refuse transactions.
  • ▸Two transactions can each wait for the lock the other holds; the engine breaks the tie by killing one.
BUILDS ON
01

The same read, two answers

The accounts table holds account 1 with a balance of 100. Two transactions open at the same moment. Transaction A runs SELECT balance FROM accounts WHERE id = 1 and reads 100. Before A finishes its work, transaction B runs UPDATE accounts SET balance = 120 WHERE id = 1 and commits.

Now A, still inside the same open transaction, runs the identical SELECT a second time. It reads 120. The same query, against the same row, in the same transaction, returned two different answers. Nothing was mistyped: B's committed change leaked into A's view halfway through.

This is the whole problem of running transactions side by side. A transaction would like a single steady picture of the data for as long as it runs, but the moment a neighbor commits nearby, that picture can shift underfoot. When a row you already read comes back changed on a re-read, that specific shift has a name: a non-repeatable read.

A VALUE THAT CHANGES UNDER YOU

Keep going, sign up to unlock the rest

4 more parts in this topic, plus 30+ more topics in SQL.

Sign up, it's freeSee the full SQL
Writes, Constraints & Transactions0/4#26 Transactions & ACID
#28 Indexes