7

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 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. 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. If the lock is not released within 1 minute the error is "timeout", not deadlock.

Quassnoi
  • 413,100
  • 91
  • 616
  • 614
NickSoft
  • 3,215
  • 5
  • 27
  • 48

3 Answers3

8

SELECT FOR UPDATE obtains an intent exclusive lock on the table prior to obtaining the exclusive lock on the record.

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.

Actually, this very scenario is described in the MySQL manual on locking.

To work around this, you need to get rid of all indexes except the one you are searching on, that is lock_name.

Just drop the primary key on id.

Quassnoi
  • 413,100
  • 91
  • 616
  • 614
  • So if I use UPDATE instead of INSERT, primary key won't hurt. Also the problem is not on one table. There are other tables that I can't remove the primary key. Are there any other workarounds? There should be a way to savely insert a row when you make X lock on a row – NickSoft Mar 28 '11 at 08:39
  • I guess the hard way is to have table locks with only one unique index and use it to lock before X lock and insert in more complex tables. But that has to be done before every insert that can deadlock and I hope there is other way. So is there another way to insert safely on tables with more than one (unique?) index. – NickSoft Mar 28 '11 at 08:44
0

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...

Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52
  • There is no way to check if the pid selected from the table is still running before do the update. What if 'lockname' is already in table, but it has crashed (not running)? The new process will never take the lock. I need to select&lcok->check if it's running->if not running update with new pid – NickSoft Mar 28 '11 at 08:30
0

Just figured it out thanks to Quassnoi's answer...

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
}
NickSoft
  • 3,215
  • 5
  • 27
  • 48