Since forUpdate still don't work (https://github.com/phalcon/cphalcon/issues/2407), what is best way to lock SELECTed rows in db?
I have a innodb table with items to process. I start via cronjob some tasks, which look after items to process (status=open), update the row with status=processing and then do some stuff. How can i protect the time between
$oModel->findFirst('status="open"');
and
$oModel->update(['status' => 'processing']);
?
You can do this by setting an options for_update => true.
$this->db->begin();
$oModel->findFirst( [
'conditions' => 'status="open"',
'for_update' => true
] );
$oModel->status = 'processing';
$oModel->update();
$this->db->commit();
the for_update option will set exclusive lock on each row it reads. also can see document https://docs.phalconphp.com/en/latest/reference/models.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With