Example usage for javax.transaction RollbackException getCause

List of usage examples for javax.transaction RollbackException getCause

Introduction

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

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:org.apache.ofbiz.entity.transaction.TransactionUtil.java

/** Commits the transaction in the current thread IF transactions are available */
public static void commit() throws GenericTransactionException {
    UserTransaction ut = TransactionFactoryLoader.getInstance().getUserTransaction();

    if (ut != null) {
        try {//from   w w w . j av a 2  s .  c  o  m
            int status = ut.getStatus();
            Debug.logVerbose("Current status : " + getTransactionStateString(status), module);

            if (status != STATUS_NO_TRANSACTION && status != STATUS_COMMITTING && status != STATUS_COMMITTED
                    && status != STATUS_ROLLING_BACK && status != STATUS_ROLLEDBACK) {
                ut.commit();

                // clear out the stamps to keep it clean
                clearTransactionStamps();
                // clear out the stack too
                clearTransactionBeginStack();
                clearSetRollbackOnlyCause();

                Debug.logVerbose("Transaction committed", module);
            } else {
                Debug.logWarning("Not committing transaction, status is " + getStatusString(), module);
            }
        } catch (RollbackException e) {
            RollbackOnlyCause rollbackOnlyCause = getSetRollbackOnlyCause();

            if (rollbackOnlyCause != null) {
                // the transaction is now definitely over, so clear stuff as normal now that we have the info from it that we want
                clearTransactionStamps();
                clearTransactionBeginStack();
                clearSetRollbackOnlyCause();

                Debug.logError(e,
                        "Rollback Only was set when trying to commit transaction here; throwing rollbackOnly cause exception",
                        module);
                throw new GenericTransactionException(
                        "Roll back error, could not commit transaction, was rolled back instead because of: "
                                + rollbackOnlyCause.getCauseMessage(),
                        rollbackOnlyCause.getCauseThrowable());
            } else {
                Throwable t = e.getCause() == null ? e : e.getCause();
                throw new GenericTransactionException(
                        "Roll back error (with no rollbackOnly cause found), could not commit transaction, was rolled back instead: "
                                + t.toString(),
                        t);
            }
        } catch (IllegalStateException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Could not commit transaction, IllegalStateException exception: " + t.toString(), t);
        } catch (HeuristicMixedException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Could not commit transaction, HeuristicMixed exception: " + t.toString(), t);
        } catch (HeuristicRollbackException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Could not commit transaction, HeuristicRollback exception: " + t.toString(), t);
        } catch (SystemException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException("System error, could not commit transaction: " + t.toString(),
                    t);
        }
    } else {
        Debug.logInfo("UserTransaction is null, not committing", module);
    }
}

From source file:org.etk.entity.engine.plugins.transaction.TransactionUtil.java

/**
 * Commits the transaction in the current thread IF transactions are available
 *//*from ww  w.java2  s . c  o m*/
public static void commit() throws GenericTransactionException {
    UserTransaction ut = TransactionFactory.getUserTransaction();

    if (ut != null) {
        try {
            int status = ut.getStatus();
            logger.debug("[TransactionUtil.commit] current status : " + getTransactionStateString(status));

            if (status != STATUS_NO_TRANSACTION && status != STATUS_COMMITTING && status != STATUS_COMMITTED
                    && status != STATUS_ROLLING_BACK && status != STATUS_ROLLEDBACK) {
                ut.commit();

                // clear out the stamps to keep it clean
                clearTransactionStamps();
                // clear out the stack too
                clearTransactionBeginStack();
                clearSetRollbackOnlyCause();

                logger.debug("[TransactionUtil.commit] transaction committed");
            } else {
                logger.warn(
                        "[TransactionUtil.commit] Not committing transaction, status is " + getStatusString());
            }
        } catch (RollbackException e) {
            RollbackOnlyCause rollbackOnlyCause = getSetRollbackOnlyCause();

            if (rollbackOnlyCause != null) {
                // the transaction is now definitely over, so clear stuff as normal
                // now that we have the info from it that we want
                clearTransactionStamps();
                clearTransactionBeginStack();
                clearSetRollbackOnlyCause();

                logger.error(
                        "Rollback Only was set when trying to commit transaction here; throwing rollbackOnly cause exception",
                        e);
                throw new GenericTransactionException(
                        "Roll back error, could not commit transaction, was rolled back instead because of: "
                                + rollbackOnlyCause.getCauseMessage(),
                        rollbackOnlyCause.getCauseThrowable());
            } else {
                Throwable t = e.getCause() == null ? e : e.getCause();
                throw new GenericTransactionException(
                        "Roll back error (with no rollbackOnly cause found), could not commit transaction, was rolled back instead: "
                                + t.toString(),
                        t);
            }
        } catch (IllegalStateException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Could not commit transaction, IllegalStateException exception: " + t.toString(), t);
        } catch (HeuristicMixedException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Could not commit transaction, HeuristicMixed exception: " + t.toString(), t);
        } catch (HeuristicRollbackException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Could not commit transaction, HeuristicRollback exception: " + t.toString(), t);
        } catch (SystemException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException("System error, could not commit transaction: " + t.toString(),
                    t);
        }
    } else {
        logger.info("[TransactionUtil.commit] UserTransaction is null, not commiting");
    }
}