I do this to ensure that only one instance of this process is running (pseudocode 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 the following fields:
id - primary, autoinc name - varchar(64) unique key description - text value - text
I believe that the time from START TRANSACTIN to COMMIT / ROLLBACK is really a millisecond - there is not enough time even for the wait time. How can I get a dead end with this code? I do not use other tables in this transaction. It seems like a dead end is impossible. If 2 processes are started at the same time, the first that gets the lock on this line will continue, and the other will wait for the lock that needs to be released. If the lock is not released within 1 minute, the error is a timeout and not a deadlock.
mysql deadlock transactions innodb
Nicksoft
source share