简体   繁体   中英

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. Java pseudo-code below:

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.
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?


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):

-Ddatastore.default_high_rep_job_policy_unapplied_job_pct=20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM