InertiaRSS Track and read blogs, news, and tech you care about
Read Original Open in InertiaRSS

Recommended Feeds

小众软件
小众软件
博客园 - 叶小钗
有赞技术团队
有赞技术团队
大猫的无限游戏
大猫的无限游戏
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
T
Tailwind CSS Blog
Jina AI
Jina AI
量子位
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
V
Visual Studio Blog
月光博客
月光博客

博客园 - lzhdim

10个不该免费的GitHub神级项目 JavaScript语言全面概述:从历史到现代实践 一张图看懂常见咖啡 C盘空间多出来4GB:谷歌服软 Chrome本地AI大模型可禁用、删除了 96GB显存运行230B大模型!七彩虹灵创K16笔记本评测:160W性能释放 AMD锐龙AI Max+ 395加持全能移动AI工作站 AI PC最强存储搭档!雷克沙NM1090 PRO 8TB固态评测:14400MB/s满速不缩水 C#已经不是当年的C#了——我用它写了个微秒级数据库引擎 EasyTool:轻量全能的 .NET 工具类库 SQL 入门 17:MySQL 数据类型:从字符串到 JSON 的全面解析 原相3955XM+TL3228主控!小米电竞鼠标2全面评测:原生双8K究竟有多强 PCIe 4.0火力全开:闪迪奥丁马仕GX 7100 NVMe SSD上手 超乎预料的千元级电竞主板!七彩虹iGame B850M Ultra-OC V14主板评测 SQL 入门 15:SQL 事务:从 ACID 到四种常见的并发问题 名人 - 我的闪存 C#性能优化技巧 SQL 入门 14:SQL 触发器与事件:自动化数据处理 38个实用的JavaScript 技巧 一款基于 .Net WinForm 开发的节点编辑器,纯 GDI 实现,体积仅 100+Kb Masuit.Tools:这个 C# 工具库 SQL 入门 13:SQL 存储过程与函数:封装逻辑与参数处理 49个 JavaScript 代码快捷技巧,让你在 2026 年成为代码高手 20260503 - 个人小作品更新 - lzhdim SQL 入门 12:SQL 视图:创建、修改与可更新视图 C#如何实现Windows系统桌面截图功能... 6999元AMD新旗舰首测!锐龙9 9950X3D2性能解禁:这颗U根本不是给游戏玩家造的 SQL 入门 11:日期时间格式化、IF、CASE的使用 基于C#的校时类库的实现及使用 - 开源项目研究文章 39g无打孔+原生双8K!雷柏VT0 Air MAX / VT3s Air MAX鼠标评测:轻到飞起还快到极致 WinForm 中控件自适应窗体变化 20260413 - 个人小作品更新 SQL 入门 10:SQL 内置函数:数值、字符串与时间处理 最强麒麟芯+自研风冷 华为Mate80 Pro MAX 风驰版全面评测:涡轮散热封神 性能稳到骨子里 WiFi8核心技术特性与性能分析 2026年最新AMD/Intel桌面CPU排名:多线程、单线程、游戏性能谁才是第一 用DG对C盘进行扩容 - 初学者系列 - 学习者系列 还有比它更强的OEM固态吗!长江存储PC550 1TB评测:不用散热片也能满速的PCIe 5.0 SSD - lzhdim SharpCompress:跨平台的 C# 压缩与解压库 - lzhdim SQL 入门 9:SQL 高级子查询:ANY、EXISTS 与多位置应用 - lzhdim 盘点接私活20个最实用的 .NET 开源项目 - lzhdim C#中加载图片的资源释放 - lzhdim 20260404 - 个人小作品更新 - lzhdim 开启iphone的墙纸玻璃效果 - lzhdim 可能是综合性能最强的PCIe 5.0 SSD!铠侠EXCERIA PRO G2 2TB评测:AIDA64线性写入全程不掉速 SQL 入门 8:SQL 复杂查询:子查询与ALL关键词 - lzhdim 性能最强的锐龙AI 9 H 465轻薄本!华硕无畏Pro16锐龙版2026评测 4盘RAID 0读写超1100MB/s!希捷酷狼Pro 32TB硬盘评测:百万文件快速完成备份 - lzhdim 项目(Project)、项目集(Program)、项目组合(Portfolio) SQL 入门 7:SQL 聚合与分组:函数、GROUP BY 与 ROLLUP
SQL Beginner 16: SQL Transaction Isolation Levels and Deadlock Resolution (Easy to Understand)
lzhdim · 2026-05-28 · via 博客园 - lzhdim

Transaction isolation level is the core of database concurrency control, defining how transactions are isolated from each other and the extent to which one transaction can see changes made by other transactions. Based on the  sql_store  database, this article explains in detail the four standard transaction isolation levels supported by MySQL and their impacts through code examples, illustrates the role of isolation levels with scenarios, and analyzes deadlocks and their avoidance methods to help readers gain a deep understanding of transaction concurrency management.

Learning content

1. Overview of transaction isolation levels

MySQL supports four standard transaction isolation levels, from low to high:

  • Read Uncommitted:
    The lowest isolation level, where transactions can read uncommitted changes from other transactions, leading to dirty read issues. Offers the highest performance but may result in dirty reads, non-repeatable reads, and phantom reads.
  • Read Committed
    Transactions can only read committed changes, avoiding dirty reads, but non-repeatable reads and phantom reads may still occur. The default level for many databases (such as Oracle, PostgreSQL).
  • Repeatable Read
    MySQL InnoDB's default isolation level, ensures consistent results when reading the same data multiple times within a transaction through Multi-Version Concurrency Control (MVCC) and gap locking, avoiding dirty reads and non-repeatable reads, but phantom reads may still occur (InnoDB mitigates this partially through locking mechanisms).
  • Serializable
    The highest isolation level, forces transaction serial execution, completely avoids dirty reads, non-repeatable reads, and phantom reads, but has the lowest performance.

The higher the isolation level, the lower the concurrency performance, but the higher the data consistency. In ATM transfer scenarios, an appropriate isolation level can ensure that account balance updates are not interfered with by other transactions.

2. Viewing and Setting Isolation Level

Use  SHOW VARIABLES LIKE 'transaction_isolation'  to view the current isolation level, which defaults to  REPEATABLE-READ. The isolation level can be set using the following statement:

  • SET TRANSACTION ISOLATION LEVEL <level>: Affects the next transaction.
  • SET SESSION TRANSACTION ISOLATION LEVEL <level>: Affects the current session.
  • SET GLOBAL TRANSACTION ISOLATION LEVEL <level>: Affects global new sessions.

3. Concurrency Issues and Isolation Levels

Different isolation levels have varying capabilities in controlling dirty reads, non-repeatable reads, and phantom reads:

  • Dirty read : Reading uncommitted data, which may lead to incorrect decisions.
  • Non-repeatable read : Inconsistent query results within the same transaction due to changes committed by other transactions.
  • Phantom read : Changes in the number of rows within the query range in the same transaction due to insertions or deletions by other transactions.

4. Deadlock

Deadlock occurs in concurrent transactions when two or more transactions wait for each other to release resources, preventing them from proceeding. MySQL detects deadlocks and terminates one transaction, reporting an error. To reduce deadlocks, maintain a consistent resource access order or shorten transaction lengths.

Example code and explanation

1. View isolation level

SHOW VARIABLES LIKE 'transaction_isolation';

Displays the current isolation level, with the default value of  REPEATABLE-READ, which is the standard configuration for MySQL InnoDB.

2. Set the isolation level

SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;

sets the isolation level of the current session to  SERIALIZABLE to ensure that subsequent transactions are executed serially and to avoid all concurrency issues.

3. Read Uncommitted (READ UNCOMMITTED)

Console 1 :

SET  TRANSACTION  ISOLATION  LEVEL  READ  UNCOMMITTED;
SELECT   points   FROM customers WHERE customer_id = 1;

Console 2

START TRANSACTION;
UPDATE customers SET points = 20 WHERE customer_id 1;
-- Not committed

in console 1 set READ UNCOMMITTED after, query customer 1's points (originally 2303). Console 2 updates the points to 20 but not committed, console 1 queries again will see 20, reflecting dirty read. If console 2 rolls back, console 1's query result is invalid, may mislead business decisions.

4. Read committed (READ COMMITTED)

Console 1:

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT points FROM customers WHERE customer_id = 1;

Console 2

START TRANSACTION;
UPDATE customers SET points = 20 WHERE customer_id = 1;
COMMIT;

After setting READ COMMITTED in Console 1, query the points. If Console 2 does not commit the changes, Console 1 still sees the original value 2303, avoiding dirty reads. After Console 2 commits, Console 1 queries to get 20. However, if multiple queries are made within the same transaction, the results may be inconsistent due to other transactions committing, leading to non-repeatable reads.

Console 1 (Non-repeatable read example) :

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
START TRANSACTION;
SELECT points FROM customers WHERE customer_id = 1;
-- assume returns 20
SELECT points FROM customers WHERE customer_id = 1;
-- return 30
COMMIT;

Console 2

START TRANSACTION;
UPDATE customers SET points = 30 WHERE customer_id = 1;
COMMIT;

Console 1 first query gets 20, Console 2 commits and updates to 30, Console 1 second query gets 30, demonstrating non-repeatable read, data consistency may be affected.

Repeatable Read

Console 1

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION;
SELECT points FROM customers WHERE customer_id = 1;
-- returns 30
SELECT points FROM customers WHERE customer_id = 1;
-- still returns 30
COMMIT;

Console 2

START   TRANSACTION;
UPDATE   customers   SET   points =   40   WHERE   customer_id =   1;
COMMIT;

in REPEATABLE READ  below, the console 1 creates a consistent view when the transaction starts, based on the snapshot at the time of transaction start (score is 30). Console 2 commits the change to 40, but the second query of console 1 still returns 30, avoiding non-repeatable read. MVCC ensures data consistency within the transaction.

Example of phantom read:

Console 1:

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION;
SELECT * FROM customers WHERE state = 'CO';
-- return 1 record
COMMIT;

Console 2

START TRANSACTION;
UPDATE customers SET state = 'CO' WHERE customer_id = 1;
COMMIT;

Console 1 Query state = 'CO' 's customers, initially only 1 record. Console 2 Change the status of customer 1 to 'CO' and commit. After console 1 commits the transaction and queries again, it still retrieves 1 record, not reflecting the latest database status, resulting in phantom reads. InnoDB's gap locks can partially mitigate phantom reads.

6. Serialization (SERIALIZABLE)

Console 1:

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SELECT * FROM customers WHERE state = 'CO';
COMMIT;

Console 2:

START TRANSACTION;
UPDATE customers SET state =  'CO' WHERE customer_id =  2;
COMMIT;

Under  SERIALIZABLE  isolation level, Console 1 queries customers with  state = 'CO'  (initially 2). Console 2 attempts to change the status of customer 2 to 'CO' but does not commit, blocking Console 1's transaction until Console 2 commits, preventing phantom reads. After commit, Console 1 queries correctly reflect the latest data.

7. Deadlock

Console 1

START TRANSACTION;
UPDATE customers SET state = 'VA' WHERE customer_id = 1;
UPDATE orders SET status = 1 WHERE order_id = 1;
COMMIT;

Console 2:

START TRANSACTION;
UPDATE orders SET status = 1 WHERE order_id = 1;
UPDATE customers SET state = 'VA' WHERE customer_id = 1;
COMMIT;

Console 1 Locked customers , attempting to lock  orders; Console 2 locks  orders, attempting to lock  customers, forming a circular dependency, leading to deadlock. MySQL terminates a transaction upon detecting deadlock and reports an error. To avoid deadlock, the order of resource access must be unified or the transaction length shortened.

summarizes

transaction isolation levels determine the visibility and consistency of data for concurrent transactions. READ UNCOMMITTED   has high performance but carries significant risks, SERIALIZABLE   is secure but has low performance, REPEATABLE READ   is the default compromise solution for MySQL InnoDB. The ATM transfer scenario illustrates the importance of isolation levels for data consistency. This article is based on sql_store database, explaining four isolation levels and deadlock issues through code examples. Subsequent content will explore index design or query optimization, stay tuned.