可以看到,用id<=1加的锁,只锁住了id<=1的范围,可以成功添加id为2的记录,添加id为0的记录时就会等待锁的释放。
附说明:
MySQL manual里对可重复读里的锁的详细解释:
http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html#isolevel_repeatable-read
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. For a unique index with a unique search condition, InnoDB
locks
only the index record found, not the gap before it. For other search conditions, InnoDB
locks
the index range scanned, using gap locks or next-key (gap plus index-record)locks to block insertions by other sessions into the gaps covered by the range.
一致性读和提交读,先看实验,
实验4-4: