Understanding MySQL Transaction Isolation Levels and Best Practices
This article explains MySQL transaction isolation concepts, the four standard isolation levels, their effects on dirty, non‑repeatable, and phantom reads, practical configuration steps, when to use repeatable read, how InnoDB implements isolation, and tips for avoiding long transactions and planning backups.
Why Transactions Matter
Transferring money is a classic example of a transaction: the operations of checking balance, deducting, and updating must be atomic, otherwise concurrent reads could cause double‑spending.
Isolation and Isolation Levels
Isolation (the "I" in ACID) prevents anomalies when multiple transactions run concurrently. The main problems are:
Dirty read
Non‑repeatable read
Phantom read
SQL defines four isolation levels, each balancing consistency and performance:
Read Uncommitted : Uncommitted changes are visible to other transactions.
Read Committed : Changes become visible only after the transaction commits.
Repeatable Read : All reads within a transaction see a snapshot taken at the start; uncommitted changes remain invisible.
Serializable : Full locking on rows; later transactions wait for earlier ones to finish.
Illustrative Example
Consider a table T(c int) ENGINE=InnoDB with a single row value 1. Two transactions run sequentially, producing different results under each isolation level:
create table T(c int) engine=InnoDB;
insert into T(c) values(1);Read Uncommitted: V1, V2, V3 = 2
Read Committed: V1 = 1, V2 & V3 = 2
Repeatable Read: V1 = V2 = 1, V3 = 2
Serializable: Transaction B is locked until A commits; from A’s view V1 = V2 = 1, V3 = 2
Implementation Details
MySQL creates a view (read‑view) for each isolation level:
Repeatable Read: view is created at transaction start and reused.
Read Committed: view is recreated for each statement.
Read Uncommitted: reads the latest row value directly, no view.
Serializable: uses explicit row locks.
Oracle defaults to Read Committed, so when migrating to MySQL you may need to set transaction_isolation='READ-COMMITTED' to keep behavior consistent.
Configuring Isolation
Set the global or session variable transaction_isolation to the desired level and verify with:
show variables like 'transaction_isolation';Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaEdge
First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
