Transaction transaction = transactionHandler.begin();
try {
execute(transaction);//Delete entities from datastore
logger.info("Initiating commit of transaction");
transactionHandler.commit();
} finally {
transactionHandler.tryRollback();
}
public class TransactionHandler{
private Logger logger = Logger.getLogger(this.getClass().getName());
private Transaction transaction;
public Transaction begin(){
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
TransactionOptions options = TransactionOptions.Builder.withXG(true);
transaction = datastore.beginTransaction(options);
return transaction;
}
public void commit(){
if (transaction.isActive()){
transaction.commit();
logger.info("Transaction comittet");
}else{
logger.warning("Transaction was not active - transaction was not comittet");
}
}
public void tryRollback(){
if (transaction.isActive()){
transaction.rollback();
logger.info("Transaction rolled back as transaction was still active");
}else{
logger.info("Transaction was not active - transaction was not rolledback");
}
}
}
Hello Louise,
Your code starts with a “try { “ but doesn’t throw any exception. You might be interested in the DatastoreTimeoutException as exemplified in the “Handling Datastore Errors” online document. A sample statement looks like: catch (ConcurrentModificationException e).
To properly roll back your transaction, you may consider following the examples provided in the “Transactions” document.
Because the Cloud Datastore API does not retry transactions, you may consider adding logic so that your app provides the same functionality, in your case to delete a number of entities, analogous to the appengine/datastore/src/test/java/com/example/appengine/TransactionsTest.java sample code from this document.