I didn't look at your snippet in detail, but you need transactions,
and that means the entities
need to be in the same entity group, that is have same ancestor.

On May 31, 2:53 am, pavb <pavieillardba...@gmail.com> wrote:
> Hi,
>
> Yes all the datastore insert, update, delete done in the transaction
> are canceled by the rollback operation.
> The put of your updated Account is validated only if the transaction
> commit is done succesfully.
>
> PA
>
> On 30 mai, 07:57, Jacob <jacob.rho...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I am writing some code that needs to do a rollback on a secondary
> > object/table should the transaction fail, I believe this can be done
> > via entity groups, however I am not sure if this is how it would be
> > implemented. I have written some sample code to check if what I would
> > be doing is correct?
>
> > Would the following code ensure the "Account" object is never updated
> > if the insert of the "TransactionRecord" object fails.
>
> >         public void addTransaction(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("TransactionRecord", 
> > 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();
> >                         }
> >                 }
>
> >         }
>
> > Thanks for any feedback!

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to