I am still a bit confused about transactions, whether it is using DatastoreService or Objectify. (Yes, I read What is the correct way to atomically increment a counter in App Engine?). I need to increment a counter atomically. How do I do that? The example in the app engine docs has a rollback in its finally block. But I don’t want a rollback, I want the system to keep trying. On the other hand, the objectify docs say that its transaction model is different from that of the low-level api. So I am writing both codes, I just need help correcting them or confirming them.
DatastoreService version
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService()
Transaction txn = datastore.beginTransaction();
try {
Key commentKey = KeyFactory.createKey(“Comment”, id);
Entity comment = datastore.get(commentKey);
int views = (Integer)comment.getProperty(“views”);
views++;//increment step
comment.setProperty(“views”, views);
datastore.put(comment);
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
Objectify version
ofy().transact(new VoidWork() {
@Override
public void vrun() {
Comment comment = ofy().load().type(Comment.class).id(commentId).now();
long views = 1+ comment.getViews();
comment.setViews(views);
ofy().save().entity(comment).now();
}
});
An important point is that I want the system to keep trying ad infinitum. And of course I want the client call to return while all of this happens asynchronously. Thanks for any help