Rob
Rob Olmos
Chief Technology Officer
Blog

Simple Database Record Locking - Propel

$c = new Criteria();
$c->add(InspectionPeer::ID, $ie->getId());
$c->add(InspectionPeer::IN_PROCESS, 1);
if(InspectionPeer::doUpdate($c) === 0) {
//field was not modified therefore it was already 1 and
//is being handled by another process so skip
continue;
}
$ie->setInProcess(1);

Simple record-level locking is achieved by atomically updating a lock field to grab the lock. This is done by updating the field and checking if it was actually locked by return of doUpdate which indicates the number of rows modified If no rows were modified then it returns 0. If you were able to grab the lock, then it returns 1.

This is important because it avoid the race condition between checking if the lock is free and actually grabbing the lock, which much the same as a typical get-set race condition with regular memory values.

This form of locking is known as "pessimistic offline" due to the fact that:
it grabs the lock first instead of only checking afterwards if it has changed
it actually stores the lock in the database rather than in the DBMS

Our Latest Tweets