简体   繁体   English

带有独占锁定的MySQL InnoDB死锁(FOR UPDATE)

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

I do this to ensure only once instance of this process is running (pseudo code php/mysql innodb): 我这样做是为了确保只运行一次这个进程的实例(伪代码php / mysql innodb):

START TRANSACTION
$rpid = SELECT `value` FROM locks WHERE name = "lock_name" FOR UPDATE
$pid = posix_getpid();
if($rpid > 0){
  $isRunning = posix_kill($rpid, 0);
  if(!$isRunning){ // isRunning
    INSERT INTO locks values('lock_name', $pid) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)
  }else{
    ROLLBACK
    echo "Allready running...\n";
    exit();
  }
}else{ // if rpid == 0 -
  INSERT INTO locks values('lock_name', $pid) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)
}
COMMIT

...............

//free the pid
INSERT INTO locks values('lock_name', 0) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)

Table locks contain these fields: 表锁包含以下字段:

id - primary, autoinc
name - varchar(64) unique key
description - text
value - text

I believe the time from START TRANSACTIN to COMMIT/ROLLBACK is really milliseconds - there is no enough time to even get timeout. 我相信从START TRANSACTIN到COMMIT / ROLLBACK的时间真的是毫秒 - 没有足够的时间来获得超时。 How is it possible to get a deadlock with this code? 如何使用此代码获得死锁? I don't use other tables within this transaction. 我不在此交易中使用其他表格。 It looks that deadlock is not possible. 看起来死锁是不可能的。 If 2 processes start at the same time the first that gets the lock on that row will will proceed and the other will wait the lock to be released. 如果2个进程同时启动,则第一个获取该行锁定的进程将继续进行,另一个进程将等待锁定被释放。 If the lock is not released within 1 minute the error is "timeout", not deadlock. 如果锁定在1分钟内未释放,则错误为“超时”,而不是死锁。

SELECT FOR UPDATE obtains an intent exclusive lock on the table prior to obtaining the exclusive lock on the record. SELECT FOR UPDATE在获取记录上的独占锁之前,在表上获得意图排他锁。

Therefore, in this scenario: 因此,在这种情况下:

X1: SELECT FOR UPDATE -- holds IX, holds X on 'lock_name'
X2: SELECT FOR UPDATE -- holds IX, waits for X on 'lock_name'
X1: INSERT -- holds IX, waits for X for the gap on `id`

a deadlock occurs, since both transactions are holding an IX lock on the table and waiting for an X lock on the records. 发生死锁,因为两个事务都在表上持有IX锁并等待记录上的X锁。

Actually, this very scenario is described in the MySQL manual on locking . 实际上,这个场景在关于锁定MySQL手册中有所描述。

To work around this, you need to get rid of all indexes except the one you are searching on, that is lock_name . 要解决此问题,您需要删除除搜索的索引之外的所有索引,即lock_name

Just drop the primary key on id . 只需将主键放在id

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? 在没有看到实际的PHP代码的情况下,很难确定 - 但是在运行SELECT和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... 通过访问受影响的行,您可以查看该进程是否已在运行...

Just figured it out thanks to Quassnoi 's answer... 刚刚想通了Quassnoi的回答......

I can do: 我可以:

$myPid = posix_getpid();
$gotIt = false;
while(true){
  START TRANSACTION;
  $pid = SELECT ... FOR UPDATE; // read pid and get lock on it
  if(mysql_num_rows($result) == 0){
    ROLLBACK;// release lock to avoid deadlock
    INSERT IGNORE INTO locks VALUES('lockname', $myPid);
  }else{
    //pid existed, no insert is needed
    break;
  }
}

if($pid != $myPid){ //we did not insert that
  if($pid>0 && isRunning($pid)){
    ROLLBACK;
    echo 'another process is running';
    exit;
  }{
    // no other process is running - write $myPid in db
    UPDATE locks SET value = $myPid WHERE name = 'lockname'; // update is safe
    COMMIT;
  }
}else{
  ROLLBACK; // release lock
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM