[appengine-java] Implementation question regarding efficient check and lock on datastore

2012-01-12 Thread Raphael André Bauer
Hi,


I got a rather simple question and I am sure there is a simple solution.

Let's say I got a RPC endpoint that checks if an entity exists and
then creates another entity. This RPC endpoint can be called
simultaneously on different instances. Say we got a user object, and
sometimes we generate an AccountStatus for the User. The generation is
triggered by an RPC call that checks if AccountStatus for an User
exists - if not it generates a new entity.

The problem is: While one RPC checks if an entity exists - the other
RPC call is already after the check (with no result) and generates the
AccountStatus entity. This results in two entities being created and
not one.

I guess what I want to have is some kind of check and lock. My
question is how to implement an efficient lock on a distributed
environment like GAE. Transactions? Or better using memcache?


Is there a best practise how to implement that?


Thanks :)

Best,


Raphael

-- 
You received this message because you are subscribed to the Google Groups 
Google App Engine for Java group.
To post to this group, send email to google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.



Re: [appengine-java] Implementation question regarding efficient check and lock on datastore

2012-01-12 Thread Shawn Brown
 I guess what I want to have is some kind of check and lock. My
 question is how to implement an efficient lock on a distributed
 environment like GAE. Transactions? Or better using memcache?


 Is there a best practise how to implement that?

I'm not saying this is best practice but just throwing it out there...

1 start transaction
2a run check on entity on whether AccountStatus needs generation
2b (if AccountStatus needs generation  AccountStatusLock entity is
null  )  store entity Key or unique identifier in separate
AccountStatusLock entity
2c commit transaction [which stores AccountStatusLock]
3a generate AccountStatus
3b commit entity with generated AccountStatus
4 delete AccountStatusLock

Shawn

-- 
You received this message because you are subscribed to the Google Groups 
Google App Engine for Java group.
To post to this group, send email to google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.