Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phalcon row locking with models

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']);

?

like image 752
Glueckstiger Avatar asked Sep 13 '26 04:09

Glueckstiger


1 Answers

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

like image 72
LCB Avatar answered Sep 15 '26 15:09

LCB