Example usage for javax.transaction RollbackException RollbackException

List of usage examples for javax.transaction RollbackException RollbackException

Introduction

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

Prototype

public RollbackException(String msg) 

Source Link

Usage

From source file:org.alfresco.util.transaction.SpringAwareUserTransaction.java

/**
 * @throws IllegalStateException if a transaction was not started
 *///from   w w  w.ja v  a 2s. co  m
public synchronized void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
        SecurityException, IllegalStateException, SystemException {
    // perform checks
    TransactionInfo txnInfo = getTransactionInfo();

    int status = getStatus();
    // check the status
    if (status == Status.STATUS_NO_TRANSACTION) {
        throw new IllegalStateException("The transaction has not yet begun");
    } else if (status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK) {
        throw new RollbackException("The transaction has already been rolled back");
    } else if (status == Status.STATUS_MARKED_ROLLBACK) {
        throw new RollbackException("The transaction has already been marked for rollback");
    } else if (status == Status.STATUS_COMMITTING || status == Status.STATUS_COMMITTED) {
        throw new IllegalStateException("The transaction has already been committed");
    } else if (status != Status.STATUS_ACTIVE || txnInfo == null) {
        throw new IllegalStateException("No user transaction is active");
    }

    if (!finalized) {
        try {
            // the status seems correct - we can try a commit
            commitTransactionAfterReturning(txnInfo);
        } catch (Throwable e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Transaction didn't commit", e);
            }
            // commit failed
            internalStatus = Status.STATUS_ROLLEDBACK;
            RollbackException re = new RollbackException("Transaction didn't commit: " + e.getMessage());
            // Stick the originating reason for failure into the exception.
            re.initCause(e);
            throw re;
        } finally {
            // make sure that we clean up the stack
            cleanupTransactionInfo(txnInfo);
            finalized = true;
            // clean up leaked transaction logging
            isBeginMatched = true;
            beginCallStack = null;
        }
    }

    // regardless of whether the transaction was finally committed or not, the status
    // as far as UserTransaction is concerned should be 'committed'

    // keep track that this UserTransaction was explicitly committed
    internalStatus = Status.STATUS_COMMITTED;

    // done
    if (logger.isDebugEnabled()) {
        logger.debug("Committed user transaction: " + this);
    }
}