✏️ Explanatory Question
Level: Advanced — Directly tied to the "Isolation" in ACID; a favourite deep-dive question.
Isolation levels define how and when the changes made by one transaction become visible to other concurrent transactions. They balance data consistency against performance/concurrency.
MySQL (InnoDB) supports the four SQL-standard isolation levels, from least to most strict:
| Isolation Level | Dirty Read | Non-Repeatable Read | Phantom Read |
|---|---|---|---|
| READ UNCOMMITTED | Possible | Possible | Possible |
| READ COMMITTED | Prevented | Possible | Possible |
| REPEATABLE READ (default) | Prevented | Prevented | Prevented* |
| SERIALIZABLE | Prevented | Prevented | Prevented |
*In InnoDB, REPEATABLE READ also prevents phantom reads via next-key (gap) locking — beyond the basic SQL standard.
-- View the current isolation level
SELECT @@transaction_isolation;
-- Set the isolation level for the NEXT transaction
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
-- Set it globally (affects new sessions)
SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- Use within a transaction
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SELECT * FROM accounts WHERE acc_id = 1;
COMMIT;