[Chicago] appengine session locking
Cosmin Stejerean
cstejerean at gmail.com
Mon May 12 21:09:22 CEST 2008
On Mon, May 12, 2008 at 1:50 PM, Massimo Di Pierro
<mdipierro at cs.depaul.edu> wrote:
> This is the problem.
> In order to lock a session I need first to check that it has not been
> locked already.
> I can only mark it as locked after I have performed the check.
> If if perform the check and then the update in separate statements I enter
> in a race condition.
> As far as I know there is no API to perform a conditional update.
>
> Am I wrong?
>
Are you familiar with datastore transactions?
http://code.google.com/appengine/docs/datastore/transactions.html
here's an example that should work
def lock_session(key):
session = db.get(key)
if session.locked:
raise ValueError("Session with key %s is already locked" % key)
else:
session.locked = True
session.put()
db.run_in_transaction(lock_session, session_key_name)
If there are concurrent updates to the same object while your
transaction is running Google will restart your transaction a fixed
number of times. If the function raises an exception Google will abort
the transaction.
--
Cosmin Stejerean
http://blog.offbytwo.com
More information about the Chicago
mailing list