Mysql with innodb and serializable transaction does not (always) lock rows

Posted by Tobias G. on Stack Overflow See other posts from Stack Overflow or by Tobias G.
Published on 2011-01-12T12:30:00Z Indexed on 2011/01/12 13:53 UTC
Read the original article Hit count: 97

Hello,

I have a transaction with a SELECT and possible INSERT. For concurrency reasons, I added FOR UPDATE to the SELECT. To prevent phantom rows, I'm using the SERIALIZABLE transaction isolation level. This all works fine when there are any rows in the table, but not if the table is empty. When the table is empty, the SELECT FOR UPDATE does not do any (exclusive) locking and a concurrent thread/process can issue the same SELECT FOR UPDATE without being locked.

CREATE TABLE t (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  display_order INT
) ENGINE = InnoDB;

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SELECT COALESCE(MAX(display_order), 0) + 1 from t FOR UPDATE;

..

This concept works as expected with SQL Server, but not with MySQL. Any ideas on what I'm doing wrong?

EDIT

Adding an index on display_order does not change the behavior.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about transactions