Mysql事务隔离级别之读提交详解

发布时间:2026-01-12 08:53:16 作者:用户投稿 浏览量(
摘要:查看mysql 事务隔离级别mysql> show variables like ‘%isolation%‘;+---------------+----------------+| Variable_name | Value |+---------------+--...

查看mysql 事务隔离级别

Mysql事务隔离级别之读提交详解(图1)
mysql> show variables like '%isolation%';
+---------------+----------------+
| Variable_name | Value     |
+---------------+----------------+
| tx_isolation | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.00 sec)

可以看到当前的事务隔离级别为 READ-COMMITTED 读提交

下面看看当前隔离级别下的事务隔离详情,开启两个查询终端A、B。

下面有一个order表,初始数据如下

mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   1 |
+----+--------+
1 row in set (0.00 sec)

第一步,在A,B中都开启事务

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

第二步查询两个终端中的number值

A

 mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   1 |
+----+--------+
1 row in set (0.00 sec)

B

 mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   1 |
+----+--------+
1 row in set (0.00 sec)

第三步将B中的number修改为2,但不提交事务

mysql> update `order` set number=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

第四步查询A中的值

mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   1 |
+----+--------+
1 row in set (0.00 sec)

发现A中的值并没有修改。

第五步,提交事务B,再次查询A中的值

B

mysql> commit;
Query OK, 0 rows affected (0.01 sec)

A

mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   2 |
+----+--------+
1 row in set (0.00 sec)

发现A中的值已经更改

第六步,提交A中的事务,再次查询A,B的值。

A

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   2 |
+----+--------+
1 row in set (0.00 sec)

B

mysql> select * from `order`;
+----+--------+
| id | number |
+----+--------+
| 13 |   2 |
+----+--------+
1 row in set (0.00 sec)

发现A,B中的值都更改为2了。

下面给一个简单的示意图


我们可以看到,在事务隔离级别为读已提交 的情况下,当B中事务提交了之后,即使A未提交也可以读到B事务提交的结果。这样解决了脏读的问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本站内容部分来源网络搜集发布,如有侵权请联系客服删除。

亲,别再下拉了

百闻不如一见,立即拨打电话沟通吧!

在线客服
17370845950