Ben Flynn
Ben Flynn

Reputation: 18922

Appengine datastore concurrent commits

I am trying to understand what happens during race conditions on the Google AppEngine datastore. I have been using Objectify, so I'll use that notation here.

@Cached
public class Car {
  @Id
  public Long id;
  @Unindexed
  public String owner = "C";
}

Now consider I have two runnables that try to modify the owner of a particular car simultaneously.

Car myCar = ofy.get(Car.class, 10);
myCar.owner = "A";  // Other runnable uses "B"
ofy.put(myCar);
System.out.println(ofy.get(Car.class, 10).owner);

I presume in this case its undetermined whether Car(10) will be "owned" by "A" or "B" but no errors will be reported. It would not be clear to the "loser" until the writes were persisted into the datastore. What would the result of the printlns be?

If I had used transactions, would one of the runnables (the "loser") have raised an exception?

Upvotes: 0

Views: 377

Answers (1)

Peter Knego
Peter Knego

Reputation: 80330

  1. Both entities use the same ID (10) so one put() will overwrite the other. It's only the question which does it first.

  2. The second transaction to put() would get the exception.

Edit:

You might get different exceptions during transaction, not all of them mean that change was not (or will not be) commited. You should actually inspect the transaction and rollback if appropriate. See first example: http://code.google.com/appengine/docs/java/datastore/transactions.html

Upvotes: 1

Related Questions