I am writing some code that needs to do a rollback on a secondary
object/table should the transaction fail, I believe this can be done
via entity groups, however I am not sure if this is how it would be
implemented. I have written some sample code to check if what I would
be doing is correct?
Would the following code ensure the "Account" object is never updated
if the insert of the "TransactionRecord" object fails.
public void addTransaction(String account, Double value, String
description, Date date) throws EntityNotFoundException {
DatastoreService datastore =
DatastoreServiceFactory.getDatastoreService();
int retries = 3;
while (true) {
Transaction txn = datastore.beginTransaction();
try {
// Update the bank balance
Key key = KeyFactory.createKey("Account",
account);
Entity e = datastore.get(key);
Double balance = (Double)
e.getProperty("balance");
balance += value;
e.setProperty("balance", value);
datastore.put(e);
// Record transaction details
Entity d = new Entity("TransactionRecord", key);
d.setProperty("account_key", key);
d.setProperty("date", date);
d.setProperty("value", value);
d.setProperty("description", description);
txn.commit();
break;
} catch (ConcurrentModificationException e) {
if (retries == 0)
throw e;
--retries;
} finally {
if (txn.isActive())
txn.rollback();
}
}
}
Thanks for any feedback!
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.