MySQL InnoDB dead lock on SELECT with exclusive lock (FOR UPDATE)

后端 未结 3 1105
野的像风
野的像风 2020-12-19 09:48

I do this to ensure only once instance of this process is running (pseudo code php/mysql innodb):

START TRANSACTION
$rpid = SELECT `value` FROM locks WHERE n         


        
3条回答
  •  -上瘾入骨i
    2020-12-19 10:27

    Without seeing the actual PHP code, it's hard to be sure - but is it possible that you are not actually using the same database connection between running the SELECT and the INSERT?

    I typically prefer not to use transactions if I can avoid it; your problem could be solved by creating a single database query along the lines of

    insert into locks
    select ('lockname', $pid)
    from locks
    where name not in
    (select name from locks)
    

    By accessing the rows affected, you can see if the process is already running...

提交回复
热议问题