简体   繁体   English

Google AppEngine:了解数据存储区交易

[英]Google AppEngine: understanding datastore transactions

I've recently faced this annoying problem with GAE inability to handle more than one entity group in a single transaction. 最近,我遇到了GAE无法在一次交易中处理多个实体组的烦人问题。 Java pseudo-code below: Java伪代码如下:

public void doit(EntityManager em, long id)
{
  Customer c = null;

  em.getTransaction().begin();

  if (id != 0)
    c = em.find(Customer.class, id);

  boolean create = (c == null);

  if (create)
    c = new Customer();

  c.setName("John Doe");

  if (create)
    em.persist(c);

  em.getTransaction().commit();  
}  

The purpose was to update customer data if the record exists, otherwise create it. 目的是在记录存在的情况下更新客户数据,否则创建记录。 I ended up with an exception complaining about multiple entity groups in transaction. 我最后遇到一个异常,抱怨事务中有多个实体组。 It would not allow finding/updating 2 different customers in one transaction as these entities belong to different entity groups. 由于这些实体属于不同的实体组,因此不允许在一次交易中查找/更新2个不同的客户。
So here is my (general) question: 所以这是我的(一般)问题:
Suppose I have a banking application with account entities having balance field. 假设我有一个银行应用程序,其帐户实体具有余额字段。 I want to transfer money from one account to another within a transaction of course to ensure noone updates both account balances during the transfer and in case of transfer failure i need to rollback everything. 我想在一次交易中将资金从一个帐户转移到另一个帐户,以确保在转移期间没有人同时更新两个帐户的余额,并且在转移失败的情况下,我需要回滚所有内容。 Does the scenario above even possible with GAE? GAE甚至可能出现上述情况吗?


Update: when trying XG transactions (see the answer below) with the local dev GAE server remember to add the following to VM execution command (otherwise it won't work): 更新:使用本地dev GAE服务器尝试XG事务(请参见下面的答案)时,请记住将以下内容添加到VM执行命令中(否则将不起作用):

-Ddatastore.default_high_rep_job_policy_unapplied_job_pct=20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Appengine数据存储超时异常 - Google Appengine Datastore Timeout Exception google appengine datastore.get(key)是否一致? - is google appengine datastore.get(key) consistent? 为存储在Google Appengine数据存储区中的实体创建URL - Creating URLs for entities stored on the Google Appengine datastore 了解Google App Engine数据存储 - Understanding Google App Engine datastore AppEngine ClassNotFoundException:com.google.appengine.api.datastore.DatastoreServiceFactory - AppEngine ClassNotFoundException: com.google.appengine.api.datastore.DatastoreServiceFactory Java Google Appengine分片计数器,无需事务处理 - Java Google Appengine sharded counters without transactions com.google.appengine.api.datastore.DatastoreNeedIndexException:找不到匹配的索引 - com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found com.google.appengine.api.datastore.DatastoreFailureException:意外失败 - com.google.appengine.api.datastore.DatastoreFailureException: Unexpected failure 示例应用中的com.google.appengine.api.datastore.DatastoreNeedIndexException错误 - com.google.appengine.api.datastore.DatastoreNeedIndexException error in Sample App java.lang.NoClassDefFoundError: com/google/appengine/api/datastore/AsyncDatastoreService - java.lang.NoClassDefFoundError: com/google/appengine/api/datastore/AsyncDatastoreService
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM