What are the transaction isolation levels in SQL?
MediumConceptConcurrency Control & Locking
Answer
An isolation level sets how strictly a transaction is protected from the effects of concurrent transactions. The SQL standard defines four levels by the read anomalies they allow.
- Read Uncommitted: dirty reads, non-repeatable reads and phantoms are all possible.
- Read Committed: no dirty reads. It is the default in PostgreSQL, Oracle and SQL Server.
- Repeatable Read: no dirty or non-repeatable reads; the standard still permits phantoms. It is the default in MySQL InnoDB, whose snapshots and next-key locks also prevent most phantoms.
- Serializable: no anomalies; the outcome equals some serial order, at the cost of more waiting or more aborted transactions.
Example: A row has balance = 100. While T1 is open, T2 changes it to 50 and commits:
T1: SELECT balance -> 100
T2: UPDATE ... SET balance = 50; COMMIT
T1: SELECT balance -> 50 under Read Committed
-> 100 under Repeatable Read