I am not sure if I understand the Google documentation, I wonder if anyone else can check my understanding.
Is the following code guaranteed to only do the following two things:
- bank account balance update
- save transaction record.
Below I understand correctly:
public void add(String account, Double value, String description, Date date) throws EntityNotFoundException {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
int retries = 3;
while (true) {
Transaction txn = datastore.beginTransaction();
try {
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);
Entity d = new Entity("Transaction", 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();
}
}
}
}
source
share