Sub topic 5
Isolation: Concurrent Transactions Without Interference
Assume you know basic SQL, tables, primary keys, constraints, and that several users or processes can access a database at the same time. This chapter focuses on isolation, the ACID property that controls what one transaction can observe while other transactions are running.
Why isolation matters
A transaction may contain several statements. If another transaction changes the same data between those statements, the first transaction can observe a result that never existed as a complete database state.
Isolation defines the visibility rules for overlapping transactions. Strong isolation makes transactions behave more like they ran one at a time. Weaker isolation allows more concurrency but permits more anomalies.
For example, suppose an application reads an account balance and then subtracts money:
BEGIN;
SELECT balance FROM accounts WHERE id = 1;
-- Application checks that the balance is large enough.
UPDATE accounts
SET balance = balance - 20
WHERE id = 1;
COMMIT;If another transaction changes the balance after the SELECT but before the UPDATE, the check may be based on an outdated value. Isolation determines whether this can happen and how the database coordinates the two transactions.
A transaction's view of data
A database can isolate transactions with locks, snapshots, or a combination of both. A lock temporarily restricts conflicting operations. A snapshot gives a transaction a consistent view of committed data as of a particular point.
Under the common READ COMMITTED level, each statement usually gets a fresh view. The same query can therefore return different results during one transaction:
-- Session A -- Session B
BEGIN; BEGIN;
SELECT balance FROM accounts UPDATE accounts
WHERE id = 1; SET balance = 0
-- returns 70.00 WHERE id = 1;
COMMIT;
SELECT balance FROM accounts
WHERE id = 1;
-- READ COMMITTED: returns 0.00
COMMIT;This is called a non-repeatable read. A stronger level, such as REPEATABLE READ, normally keeps Session A's view stable for the entire transaction.
flowchart TD
A[Transaction A reads data] --> B{Transaction B changes it?}
B -->|No| C[A sees the same committed value]
B -->|Yes, weaker isolation| D[A may see a different value later]
B -->|Yes, snapshot isolation| E[A keeps its transaction snapshot]
D --> F[Application must tolerate or prevent the anomaly]
E --> G[Conflicting writes may be rejected]Diagram: Isolation determines which concurrent changes a transaction can observe. Alt: A flowchart showing that weaker isolation can expose later changes, while snapshot isolation keeps a stable view and may reject conflicting writes.
Choosing the boundary
Put all statements that must use the same logical facts inside one transaction. A balance check and its update belong together. So do reading an order, reserving inventory, and recording the reservation.
Isolation does not replace correct transaction design. A transaction that runs for a long time holds locks or retains a snapshot longer, increasing contention and storage work. Choose the weakest level that preserves the operation's correctness, then use explicit locks or a stronger level when the business rule requires it.
Exercise
Why might two SELECT statements in one READ COMMITTED transaction return different values?
Show answer
Each statement can use a new view of committed data, so another transaction may commit a change between the two SELECT statements.