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