If you set the transaction isolation level to SERIALIZABLE
, InnoDB
wil implicity append LOCK IN SHARE MODE
to all SELECT
statements.
This mode conflicts with the locks placed by SELECT FOR UPDATE
and the SELECT
s will lock.
Note, though, that InnoDB
may lock more rows than satisfy the WHERE
condition. This is because it locks all rows scanned, not only those matched.
Say, you have an index on col1
and this query:
SELECT *
FROM mytable
WHERE col1 = 1
AND col2 = 2
FOR UPDATE
uses this index.
This will lock all records with col1 = 1
, even those with col2 <> 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…