【发布时间】:2012-02-19 22:44:45
【问题描述】:
我正在阅读有关 innodb 事务的手册,但我仍然有很多不清楚的地方。例如,我不太了解以下行为:
-- 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:For locking reads (SELECT with FOR UPDATE or LOCK IN SHARE MODE), UPDATE, and DELETE statements, locking **depends on whether the statement uses a unique index with a unique search condition**, or a range-type search condition. ...
标签: mysql transactions innodb isolation-level database-locking