MySQL5.7 Repeatable read事务隔离级别的研究。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL5.7 Repeatable read事务隔离级别的研究。相关的知识,希望对你有一定的参考价值。
事务隔离级别为Repeatable read的情况下
事务2不执行commit,读取不到事务1新添加的记录。
事务2执行了commit,可以读取到事务1新添加的记录。
session1执行事务1:只插入和提交
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set (0.00 sec)
mysql>
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from lisa;
+------------+------+
| name | age |
+------------+------+
| lisa | 99 |
| sunlongwei | 27 |
| shabala | 999 |
| balabala | 9999 |
| balabala | 9999 |
| hehe | 9999 |
| hehe | 9999 |
| bb | 1 |
| bb | 2 |
| bb | 3 |
+------------+------+
10 rows in set (0.00 sec)
mysql> insert into lisa values(‘aa‘,‘9999‘);
Query OK, 1 row affected (0.00 sec)
mysql> insert into lisa values(‘cc‘,‘9999‘);
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from lisa;
+------------+------+
| name | age |
+------------+------+
| lisa | 99 |
| sunlongwei | 27 |
| shabala | 999 |
| balabala | 9999 |
| balabala | 9999 |
| hehe | 9999 |
| hehe | 9999 |
| bb | 1 |
| bb | 2 |
| bb | 3 |
| aa | 9999 |
| cc | 9999 |
+------------+------+
12 rows in set (0.00 sec)
session2执行事务2:只查询和提交
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from lisa;
+------------+------+
| name | age |
+------------+------+
| lisa | 99 |
| sunlongwei | 27 |
| shabala | 999 |
| balabala | 9999 |
| balabala | 9999 |
| hehe | 9999 |
| hehe | 9999 |
| bb | 1 |
| bb | 2 |
| bb | 3 |
+------------+------+
10 rows in set (0.00 sec)
//事务2如果不提交则无法查询到事务1中插入的记录
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from lisa;
+------------+------+
| name | age |
+------------+------+
| lisa | 99 |
| sunlongwei | 27 |
| shabala | 999 |
| balabala | 9999 |
| balabala | 9999 |
| hehe | 9999 |
| hehe | 9999 |
| bb | 1 |
| bb | 2 |
| bb | 3 |
| aa | 9999 |
| cc | 9999 |
+------------+------+
//事务2提交后可以查询到事务1中插入的记录
以上是关于MySQL5.7 Repeatable read事务隔离级别的研究。的主要内容,如果未能解决你的问题,请参考以下文章
read repeatable&read committed 区别
带有 PESSIMISTIC_WRITE 锁的 REPEATABLE_READ 隔离级别
mysql read commited 和 REPEATABLE read 原因
Hibernate 和 PostgreSQL:REPEATABLE_READ 和使用@Version 注解来避免写入倾斜和其他现象