views:

19

answers:

1

Hi,

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 email 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

+1  A: 

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.

Wooble
Ok I understand the entity locking, but in the case where I am querying all User objects to find a match by username or email address, wouldn't the system have to block any writes of new User objects to give me a correct result? Otherwise a User with the same username/email could be created at the same time that I'm doing my query?
No, queries don't block writes. The datastore also doesn't enforce uniqueness except for keys. There's no way to ensure that 2 entities aren't created with either the same username or the same email, although you could ensure that no account is created with both the same by concatenating them and using that as the key_name. Basically, you can only choose 1 unique value for an entity kind.
Wooble