Objectify: delay between "put()" and object being found with "query()"
I'm using Objectify to store items in a database, and want to enforce uniqueness on the "name" field. I'm starting with a trivial implementation, before I start worrying about race conditions...
Here's how I add the object to the database:
Objectify ofy = ObjectifyService.begin();
if (ofy.query(Item.class).filter("name", name).count() == 0) {
Item newItem = new Item(name);
ofy.put(newIt开发者_如何学运维em);
}
If I attempt to insert an object several times quickly, sometimes I'll be able to create three or four before the filter finds existing objects and prevents a new one being saved.
This is running locally - so hasn't been deployed to Google App Engine yet.
Should I be worried? Am I missing something obvious? I haven't explicitly enabled caching on the Item class.
(The dev environment is "Google Plugin for Eclipse" v1.5.2, Objectify 3.0)
This HRD behavior is solved by using @Parent attribute and ancestor queries. See also http://code.google.com/p/objectify-appengine/wiki/AdvancedPatterns and http://code.google.com/appengine/docs/java/datastore/hr/overview.html
Have you tried to force transaction by commit?
http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#Transactions
精彩评论