Example usage for javax.transaction RollbackException getMessage

List of usage examples for javax.transaction RollbackException getMessage

Introduction

In this page you can find the example usage for javax.transaction RollbackException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.alfresco.repo.transaction.TransactionUtil.java

/**
 * Execute the transaction work in a user transaction of a specified type
 * /*w ww  .ja  v  a2  s.c o  m*/
 * @param transactionService the transaction service
 * @param transactionWork the transaction work
 * @param nonPropagatingUserTransaction indicates whether the transaction
 *        should be non propigating or not
 * @param readOnly true if the transaction should be read-only
 * 
 * @throws java.lang.RuntimeException if the transaction was rolled back
 */
private static <R> R executeInTransaction(TransactionService transactionService,
        TransactionWork<R> transactionWork, boolean nonPropagatingUserTransaction, boolean readOnly) {
    ParameterCheck.mandatory("transactionWork", transactionWork);

    R result = null;

    // Get the right type of user transaction
    UserTransaction txn = null;
    if (nonPropagatingUserTransaction == true) {
        txn = transactionService.getNonPropagatingUserTransaction();
    } else {
        txn = transactionService.getUserTransaction(readOnly);
    }

    try {
        // Begin the transaction, do the work and then commit the
        // transaction
        txn.begin();
        result = transactionWork.doWork();
        // rollback or commit
        if (txn.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
            // something caused the transaction to be marked for rollback
            txn.rollback();
        } else {
            // transaction should still commit
            txn.commit();
        }
    } catch (RollbackException exception) {
        // commit failed
        throw new AlfrescoRuntimeException("Unexpected rollback exception: \n" + exception.getMessage(),
                exception);
    } catch (Throwable exception) {
        try {
            // Roll back the exception
            txn.rollback();
        } catch (Throwable rollbackException) {
            // just dump the exception - we are already in a failure state
            logger.error("Error rolling back transaction", rollbackException);
        }

        // Re-throw the exception
        if (exception instanceof RuntimeException) {
            throw (RuntimeException) exception;
        } else {
            throw new RuntimeException("Error during execution of transaction.", exception);
        }
    }

    return result;
}