← Back to MySQL Course | Chapter 17: Transactions | Lesson 4 of 4

Transaction Isolation Levels

Understanding Isolation Levels

Isolation levels determine how visible one transaction's in-progress changes are to other transactions running at the same time, controlling the tradeoff between data consistency and performance under concurrent access.

Example: Understanding Isolation Levels

sql
SELECT @@transaction_isolation;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

READ UNCOMMITTED (Dirty Reads)

READ UNCOMMITTED is the loosest isolation level, allowing a transaction to see another transaction's changes before they've even been committed — a so-called dirty read that can expose data that later gets rolled back.

Example: READ UNCOMMITTED (Dirty Reads)

sql
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
START TRANSACTION;
SELECT * FROM accounts;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

READ COMMITTED (Non-Repeatable Reads)

READ COMMITTED guarantees you'll never see uncommitted data from another transaction, but running the same query twice within one transaction can still return different results if another transaction commits in between.

Example: READ COMMITTED (Non-Repeatable Reads)

sql
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
START TRANSACTION;
SELECT * FROM accounts;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

REPEATABLE READ (Default in MySQL)

REPEATABLE READ is MySQL's default isolation level, and it guarantees that if you read the same row twice inside one transaction, you'll get identical values both times, even if another transaction changes that row in the meantime.

Example: REPEATABLE READ (Default in MySQL)

sql
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION;
SELECT balance FROM accounts WHERE id = 1;
SELECT balance FROM accounts WHERE id = 1;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

SERIALIZABLE (Highest Isolation)

SERIALIZABLE is the strictest level available, effectively forcing concurrent transactions to behave as if they ran one after another rather than simultaneously — it eliminates concurrency anomalies entirely but can noticeably slow down a busy system.

Example: SERIALIZABLE (Highest Isolation)

sql
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SELECT * FROM accounts;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.