正确处理使用Google App Engine的交易回滚

问题描述

| 我不确定我是否了解Google文档,我想知道是否其他人可以检查我的理解。 以下代码是否保证只能执行以下两项操作: 更新银行帐户余额 存储交易记录。 以下是我的理解是正确的:
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();
        }
    }
    }
}
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)