Mysql事务隔离级别之可重复读

Mysql事务隔离级别之。可重复读(REPEATABLE-READ)
查看mysql 事务隔离级别
mysql> SELECT @@tx_isolation;
+—————–+
| @@tx_isolation |
+—————–+
| REPEATABLE-READ |
+—————–+
1 row in set (0.00 sec)

可以看到默认的事务隔离级别为 REPEATABLE-READ 可重复读
下面看看当前隔离级别下的事务隔离详情,开启两个查询终端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 | 1 |
+—-+——–+
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了。
下面给一个简单的示意图

我们可以看到,在事务隔离级别为可重复读 的情况下,A,B事务在执行期间前后看到的数据是一致的。这也就是可重复读的隔离特性。·遵循事务在执行期间看到的数据前后必须是一致的`
原文地址

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理