两个事物,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级别,在事物进行过程中,读取到的数据可能是不一致的。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10972173/viewspace-2153205/,如需转载,请注明出处,否则将追究法律责任。