怎么理解mysql中read commited与repeatable read
这篇文章主要介绍“怎么理解mysql中read commited与repeatable read”,在日常操作中,相信很多人在怎么理解mysql中read commited与repeatable read问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么理解mysql中read commited与repeatable read”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
两个事物,A与B
1. 先看read committed级别的情况:
--A事物
mysql> set session transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)
mysql> set autocommit = 0;
Query OK, 0 rows affected (0.01 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
+------+------+
| id | comm |
+------+------+
| 1 | aaa |
+------+------+
1 row in set (0.00 sec)
--B事物
mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update test set comm = 'bbb' where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
--A事物此时查询表test,此时B事物未提交,因此读到的仍然是旧的数据
mysql> select * from test;
+------+------+
| id | comm |
+------+------+
| 1 | aaa |
+------+------+
1 row in set (0.00 sec)
--B事物提交
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
--A事物再次查询,数据已发生改变。
mysql> select * from test;
+------+------+
| id | comm |
+------+------+
| 1 | bbb |
+------+------+
1 row in set (0.00 sec)
可见,在 read committed级别,顾名思义,A事物在事物进行过程中,读取不到B事物未提交的数据,但是可以读取到B事物已经提交的数据修改
2.再看repeatable read级别
--A事物:
mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> set session transaction isolation level repeatable read;
Query OK, 0 rows affected (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
+------+------+
| id | comm |
+------+------+
| 1 | aaa |
+------+------+
1 row in set (0.00 sec)
--B事物:
mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update test set comm = 'bbb' where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
--A事物此时查询表test,此时B事物未提交,因此读到的仍然是旧的数据
mysql> select * from test;
+------+------+
| id | comm |
+------+------+
| 1 | aaa |
+------+------+
1 row in set (0.00 sec)
--B事物提交
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
--A事物再次查询,发现数据未发生改变
mysql> select * from test;
+------+------+
| id | comm |
+------+------+
| 1 | aaa |
+------+------+
1 row in set (0.00 sec)
可见,在 repeatable read 级别,顾名思义,A事物在事物进行过程中,是不能读取到B事物未提交和已提交的数据修改。
对于repeatable read 级别,事物只能读取到事物开始时的数据,因此在事物进行过程中读取到的数据时一致的,而对于read commited级别,在事物进行过程中,读取到的数据可能是不一致的。
到此,关于“怎么理解mysql中read commited与repeatable read”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!