Transaction isolation affects how concurrent transactions see each other's changes. It is an important part of ACID properties. In ANSI SQL, there are four basic isolation levels:
The choice of level depends on the application's requirements:
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; BEGIN; -- Subsequent SELECTs will see "frozen" values
"Does the REPEATABLE READ level guarantee protection against phantom reads in any DB?"
No. In PostgreSQL and some other DBMS, the REPEATABLE READ level prevents only dirty and non-repeatable reads but does not necessarily protect against phantom reads. In MySQL/InnoDB, REPEATABLE READ is essentially SERIALIZABLE, but not in other DBMS.
-- In one transaction, read SELECT * FROM orders WHERE amount > 100; -- In another transaction, a new value with amount > 100 is inserted and committed -- The first transaction upon a repeated SELECT will see a "phantom" row if isolation is below SERIALIZABLE
Story
A financial service locked only READ COMMITTED for performance — the user saw an amount that had already been changed by another process, resulting in balance discrepancies.
Story
In a hotel booking system, double bookings of the same room occurred — transactions did not isolate current bookings, the level was READ COMMITTED.
Story
Transitioning from MySQL to PostgreSQL: a developer was used to REPEATABLE READ protecting against phantoms, but after migration, "stuck" orders appeared that were not expected to be seen in repeated queries within the same transaction.