atom feed9 messages in org.tigris.propel.usersRe: [propel] Record locking
FromSent OnAttachments
Gijs NelissenJul 3, 2007 8:52 am 
Hans LellelidJul 3, 2007 9:29 am 
Steve HayesJul 4, 2007 1:08 am 
Gijs NelissenJul 4, 2007 2:24 am 
Hans LellelidJul 4, 2007 8:06 am 
Hans LellelidJul 4, 2007 8:08 am 
Murray CollingwoodJul 5, 2007 12:49 am 
Steve HayesJul 5, 2007 8:08 am 
Gijs NelissenJul 6, 2007 8:17 am 
Subject:Re: [propel] Record locking
From:Hans Lellelid (ha@velum.net)
Date:Jul 4, 2007 8:06:19 am
List:org.tigris.propel.users

Hi Steve,

Steve Hayes wrote:

I'm quite new to the world of ORM, so please forgive me if this is a dumb question!

Does Propel support any kind of row/record locking? If I instantiate a Propel generated object, change a property and then save the object, what's to stop another client process doing the same in parallel?

E.G.

$my_obj = MyPeer::retrieveByPK($pk); $my_obj->setACounter($my_obj->getACounter() + 1); $my_obj->save();

In the past I'd have done something simple with SQL, such as "UPDATE things SET counter = counter + 1 where..." or for more involved changes I'd grab a write lock on the table.

Is there a standard way of handling this sort of thing in Propel?

There is not a built-in way in Propel to do optimistic/pessimistic locking. HOWEVER, it would not be difficult to add this yourself. Just create the lock column in your table and then override the save() method in the stub class to verify that the row is not locked.

Something like this (pseudo code):

public function isLocked() { return ($this->getLockColumn() !== false); }

public function save(PDO $con = null) { if ($this->isModified()) { if ($this->isLocked()) { throw new PropelException("This row is currently locked."); } try { $this->setLockColumn(true); return parent::save($con); $this->setLockColumn(false); } catch (Exception $x) { // row wasn't saved; reset lock $this->setLockColumn(false); throw $x; } } }

It would not be difficult -- and probably would be useful -- to add this to the generator for the future.

Hope that helps -

Hans