我正在阅读有关innodb交易的手册,但仍然有很多不清楚的东西给我。举例来说,我不太明白以下行为:inno db隔离级别和锁定
-- client 1 -- client 2
mysql> create table simple (col int)
engine=innodb;
mysql> insert into simple values(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into simple values(2);
Query OK, 1 row affected (0.00 sec)
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
mysql> begin;
Query OK, 0 rows affected (0.01 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update simple set col=10 where col=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update simple set col=42 where col=2;
-- blocks
现在,最后更新命令(在客户端2)等待。我期望命令执行,因为我会假设只有行1被锁定。即使客户端2中的第二个命令是insert
,行为也是一样的。任何人都可以描述这个例子背后的锁定背景(在哪里和为什么锁)?
阅读关于**'REPEATABLE READ' **:[Transaction levels](http://dev.mysql.com/doc/refman/5.5/en/set-transaction.html):'For locking reads( SELECT FOR FOR UPDATE或LOCK IN SHARE MODE),UPDATE和DELETE语句,锁定**取决于语句是使用具有唯一搜索条件**的唯一索引,还是范围类型搜索条件。 ...' – 2012-02-19 22:49:51