private boolean messageStartsWith(String prefix) { final String message = getMessage(); return message != null && message.startsWith(prefix); } }
public boolean isAlreadyExists() { if (getCause() != null && getCause() instanceof DatastoreException) { DatastoreException datastoreException = (DatastoreException) getCause(); // TODO remove check on message when Google fixes the Datastore emulator return "ALREADY_EXISTS".equals(datastoreException.getReason()) || messageStartsWith("entity already exists"); } else { return false; } }
}); } catch (TransactionException e) { if (e.isAlreadyExists()) { throw new AlreadyInitializedException("Workflow instance is already triggered: " + workflowInstance); } else if (e.isConflict()) { log.debug("Transaction conflict when triggering workflow instance. Aborted: {}", workflowInstance); } else { log.debug("Transaction failure when triggering workflow instance: {}: {}", workflowInstance, e.getMessage(), e); throw new RuntimeException(e);
private StorageTransaction newTransaction() throws TransactionException { final CheckedDatastoreTransaction transaction; try { transaction = datastore.newTransaction(); } catch (DatastoreIOException e) { throw new TransactionException(e.getCause()); } return storageTransactionFactory.apply(transaction); }
}); } catch (TransactionException e) { if (e.isConflict()) { log.debug("Transaction conflict during workflow instance transition. Aborted: {}, counter={}", event, expectedCounter);
@Override public void commit() throws TransactionException { try { tx.commit(); } catch (DatastoreIOException e) { throw new TransactionException(e.getCause()); } }
public boolean isNotFound() { if (getCause() != null && getCause() instanceof DatastoreException) { DatastoreException datastoreException = (DatastoreException) getCause(); // TODO remove check on message when Google fixes the Datastore emulator return "NOT_FOUND".equals(datastoreException.getReason()) || messageStartsWith("no entity to update"); } else { return false; } }
@Override public void rollback() throws TransactionException { try { tx.rollback(); } catch (DatastoreIOException e) { throw new TransactionException(e.getCause()); } }
public <T, E extends Exception> T runInTransaction(TransactionFunction<T, E> f) throws IOException, E { final StorageTransaction tx = newTransaction(); try { final T value = f.apply(tx); tx.commit(); return value; } catch (DatastoreException e) { tx.rollback(); throw new TransactionException(e); } finally { if (tx.isActive()) { tx.rollback(); } } }