mysql> insert into person (city)values('china'); Query OK, 1 row affected (0.01 sec)
mysql> select * from person; +----+-------+------+------+ | id | city | age | name | +----+-------+------+------+ | 1 | china | NULL | NULL | +----+-------+------+------+ 1 row in set (0.00 sec)
mysql> select * from person; +----+-------+------+------+ | id | city | age | name | +----+-------+------+------+ | 1 | china | NULL | NULL | +----+-------+------+------+ 1 row in set (0.00 sec)
查询A中隔离级别
1 2 3 4 5 6 7
mysql> select @@session.tx_isolation; +------------------------+ | @@session.tx_isolation | +------------------------+ | REPEATABLE-READ | +------------------------+ 1 row in set (0.00 sec)
mysql> select * from person; +----+-------+------+------+ | id | city | age | name | +----+-------+------+------+ | 2 | china | NULL | NULL | +----+-------+------+------+ 1 row in set (0.01 sec)
B事务中两次查询到数据不一致。不可重复读就是事务中数据不能进行重复读取,不然会导致数据不一致。
可重复读
同上面操作我们修改B的隔离级别为Repeatable read
1
set session transaction isolation level repeatable read
此时A和B的隔离级别是一致的,重复上面的操作
B 开启事务查询数据
1 2 3 4 5 6 7
mysql> select * from person; +----+-------+------+------+ | id | city | age | name | +----+-------+------+------+ | 2 | china | NULL | NULL | +----+-------+------+------+ 1 row in set (0.00 sec)
mysql> select * from person; +----+-------+------+------+ | id | city | age | name | +----+-------+------+------+ | 2 | china | NULL | NULL | +----+-------+------+------+ 1 row in set (0.00 sec)
mysql> select * from person; +----+-------+------+------+ | id | city | age | name | +----+-------+------+------+ | 2 | china | NULL | NULL | | 3 | china | NULL | NULL | +----+-------+------+------+ 2 rows in set (0.01 sec)
B
1 2 3 4 5 6 7 8
mysql> select * from person; +----+-------+------+------+ | id | city | age | name | +----+-------+------+------+ | 2 | china | NULL | NULL | | 3 | china | NULL | NULL | +----+-------+------+------+ 2 rows in set (0.00 sec)
A
1 2
mysql> insert into person (id,city)values(1,'xm'); Query OK, 1 row affected (0.00 sec)
B
1 2 3 4 5 6 7 8
mysql> select * from person; +----+-------+------+------+ | id | city | age | name | +----+-------+------+------+ | 2 | china | NULL | NULL | | 3 | china | NULL | NULL | +----+-------+------+------+ 2 rows in set (0.00 sec)
mysql> select * from person; +----+-------+------+------+ | id | city | age | name | +----+-------+------+------+ | 2 | china | NULL | NULL | | 3 | china | NULL | NULL | +----+-------+------+------+ 2 rows in set (0.00 sec)
A
1 2 3 4 5 6 7 8 9
mysql> select * from person; +----+-------+------+------+ | id | city | age | name | +----+-------+------+------+ | 1 | xm | NULL | NULL | | 2 | china | NULL | NULL | | 3 | china | NULL | NULL | +----+-------+------+------+ 3 rows in set (0.00 sec)
B
1 2
mysql> insert into person(id,city)values(1,"xm2"); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'