Ejemplo n.º 1
0
  // WARNING: (andrus) if we ever decide to make this method protected or public, we
  // need to change the signature to avoid API dependency on commons-collections
  Object runInTransaction(Transformer operation) {

    // user or container-managed or nested transaction
    if (Transaction.getThreadTransaction() != null) {
      return operation.transform(null);
    }

    // Cayenne-managed transaction

    Transaction transaction = createTransaction();
    Transaction.bindThreadTransaction(transaction);

    try {
      // implicit begin..
      Object result = operation.transform(null);
      transaction.commit();
      return result;
    } catch (Exception ex) {
      transaction.setRollbackOnly();

      // must rethrow
      if (ex instanceof CayenneRuntimeException) {
        throw (CayenneRuntimeException) ex;
      } else {
        throw new CayenneRuntimeException(ex);
      }
    } finally {
      Transaction.bindThreadTransaction(null);
      if (transaction.getStatus() == Transaction.STATUS_MARKED_ROLLEDBACK) {
        try {
          transaction.rollback();
        } catch (Exception rollbackEx) {
          // although we don't expect an exception here, print the stack, as
          // there have been some Cayenne bugs already (CAY-557) that were
          // masked by this 'catch' clause.
          jdbcEventLogger.logQueryError(rollbackEx);
        }
      }
    }
  }