我不知道我是否瞭解谷歌文檔,我想知道如果別人可以檢查我的理解。正確處理使用谷歌應用程序引擎的交易回滾
是保證永遠只能下面的代碼做以下兩項:
- 更新到銀行賬戶餘額
- 店的交易記錄。
下面是我的理解是正確的:
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 {
// 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("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();
}
}
}
}
使用retries計數器處理重新嘗試交易的操作是直接從Google文檔複製的(http://code.google.com/appengine/docs/java/datastore/transactions.html#Uses_for_Transactions)。 – Jacob 2011-05-30 08:36:24
@Jacob我的錯誤。 Python SDK自動重試;正如文檔所說,Java的顯然沒有。 – 2011-05-30 10:05:27