Transaction to find an entity - locks all entities of that type?
Reading the docs for transactions:
http://code.google.com/appengine/docs/java/datastore/transactions.html
An example provided shows one way to make an instance of an object:
try {
tx.begin();
Key k = KeyFactory.createKey("SalesAccount", id);
try {
account = pm.getObjectById(Employee.class, k);
} catch (JDOObjectNotFoundException e) {
account = new SalesAccount();
account.setId(id);
}
...
When the above transaction gets executed, it will probably block all other write attempts on Account objects? I'm wondering because I'd like to have a user signup which checks for a username or ema开发者_JS百科il already in use:
tx.begin();
"select from User where mUsername == str1 LIMIT 1";
if (count > 0) {
throw new Exception("username already in use!");
}
"select from User where mEmail == str1 LIMIT 1";
if (count > 0) {
throw new Exception("email already in use!");
}
pm.makePersistent(user(username, email)); // ok.
tx.commit();
but the above would be even more time consuming I think, making an even worse bottleneck? Am I understanding what will happen correctly?
Thanks
No, transactions only operate on Entity Groups, that is, the set of entities with the same root entity. The grouping has nothing at all to do with entity Kind; an entity's parent can be of any type.
By default, all of your entities are root entities, which means that each is an entity group of 1 entity. Unless you explicitly set a parent entity when you create a new entity, this is the behavior you'll get.
精彩评论