Read Uncommited(未提交读) | Read Commited (提交读) | Repeatable Read (可重复读) | Serializable (可串行化) | |
脏读 | Possible | Not Possible | Not Possible | Not Possible |
不可重复读 | Possible | Possible | Not Possible | Not Possible |
幻行 | Possible | Possible | Possible | Not Possible |
mysql> show create table t_bitfly\G; CREATE TABLE `t_bitfly` ( `id` bigint(20) NOT NULL default '0', `value` varchar(32) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk
mysql> select @@global.tx_isolation, @@tx_isolation; +-----------------------+-----------------+ | @@global.tx_isolation | @@tx_isolation | +-----------------------+-----------------+ | REPEATABLE-READ | REPEATABLE-READ | +-----------------------+-----------------+
t Session A Session B | | START TRANSACTION; START TRANSACTION; | | SELECT * FROM t_bitfly; | empty set | INSERT INTO t_bitfly | VALUES (1, 'a'); | | SELECT * FROM t_bitfly; | empty set | COMMIT; | | SELECT * FROM t_bitfly; | empty set | | INSERT INTO t_bitfly VALUES (1, 'a'); | ERROR 1062 (23000): | Duplicate entry '1' for key 1 v (shit, 刚刚明明告诉我没有这条记录的)
t Session A Session B | | START TRANSACTION; START TRANSACTION; | | SELECT * FROM t_bitfly; | +------+-------+ | | id | value | | +------+-------+ | | 1 | a | | +------+-------+ | INSERT INTO t_bitfly | VALUES (2, 'b'); | | SELECT * FROM t_bitfly; | +------+-------+ | | id | value | | +------+-------+ | | 1 | a | | +------+-------+ | COMMIT; | | SELECT * FROM t_bitfly; | +------+-------+ | | id | value | | +------+-------+ | | 1 | a | | +------+-------+ | | UPDATE t_bitfly SET value='z'; | Rows matched: 2 Changed: 2 Warnings: 0 | (怎么多出来一行) | | SELECT * FROM t_bitfly; | +------+-------+ | | id | value | | +------+-------+ | | 1 | z | | | 2 | z | | +------+-------+ | v
mysql> select * from child;
+----+-----+
| id | val |
+----+-----+
| 10 | a |
| 20 | b |
| 30 | a |
| 40 | b |
| 50 | c |
+----+-----+
SELECT * FROM child WHERE id > 35 FOR UPDATE;
t Session A Session B | | START TRANSACTION; START TRANSACTION; |
| |
| SELECT * FROM child | WHERE id > 35 | FOR UPDATE;
|
| +----+-----+
| | id | val |
| +----+-----+
| | 40 | b |
| | 50 | c |
| +----+-----+
| 2 row in set (0.00 sec) | INSERT INTO child | VALUES (31, 'b'); | (waiting for lock ... then timeout)
| ERROR 1205 (HY000):
| Lock wait timeout exceeded;
|
|
| INSERT INTO child | VALUES (36, 'b'); | (waiting for lock ... then timeout)
| ERROR 1205 (HY000):
| Lock wait timeout exceeded;
|
|
| INSERT INTO child values (26,"c");
| Query OK, 1 row affected (0.00 sec)
| | | select * from child;
| +----+-----+
| | id | val |
| +----+-----+
| | 10 | a |
| | 20 | b |
| | 30 | a |
| | 40 | b |
| | 50 | c |
| +----+-----+ | COMMIT; |
| COMMIT;
| | select * from child;
| +----+-----+
| | id | val |
| +----+-----+
| | 10 | a |
| | 20 | b |
| | 26 | c |
| | 30 | a |
| | 40 | b |
| | 50 | c |
| +----+-----+ v
t Session A Session B | | START TRANSACTION; START TRANSACTION; | | SELECT * FROM t_bitfly; | +----+-------+ | | id | value | | +----+-------+ | | 1 | a | | +----+-------+ | INSERT INTO t_bitfly | VALUES (2, 'b'); | COMMIT; | | SELECT * FROM t_bitfly; | +----+-------+ | | id | value | | +----+-------+ | | 1 | a | | +----+-------+ | | SELECT * FROM t_bitfly LOCK IN SHARE MODE; | +----+-------+ | | id | value | | +----+-------+ | | 1 | a | | | 2 | b | | +----+-------+ | | SELECT * FROM t_bitfly FOR UPDATE; | +----+-------+ | | id | value | | +----+-------+ | | 1 | a | | | 2 | b | | +----+-------+ | | SELECT * FROM t_bitfly; | +----+-------+ | | id | value | | +----+-------+ | | 1 | a | | +----+-------+ v