コード例 #1
0
  // used to wrap every access to the dataGrid in a transaction
  private <T> T doWithinBackingTransactionIfNeeded(Callable<T> command) {
    boolean inTopLevel = false;
    boolean commandFinished = false;

    try {
      if (!dataGrid.inTransaction()) {
        dataGrid.beginTransaction();
        inTopLevel = true;
      }

      T result = command.call();
      commandFinished = true;

      return result;
    } catch (Exception e) {
      throw new PersistenceException(e);
    } finally {
      if (inTopLevel) {
        try {
          if (commandFinished) {
            dataGrid.commitTransaction();
          } else {
            dataGrid.rollbackTransaction();
          }
        } catch (Exception e) {
          throw new PersistenceException(e);
        }
      }
    }
  }