Example usage for javax.transaction UserTransaction getStatus

List of usage examples for javax.transaction UserTransaction getStatus

Introduction

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

Prototype

int getStatus() throws SystemException;

Source Link

Document

Obtain the status of the transaction associated with the current thread.

Usage

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

/** Gets the status of the transaction in the current thread IF
 * transactions are available, otherwise returns STATUS_NO_TRANSACTION */
public static int getStatus() throws GenericTransactionException {
    UserTransaction ut = TransactionFactoryLoader.getInstance().getUserTransaction();
    if (ut != null) {
        try {//from  w  w w . ja  v a2  s. c om
            return ut.getStatus();
        } catch (SystemException e) {
            throw new GenericTransactionException("System error, could not get status", e);
        }
    } else {
        return STATUS_NO_TRANSACTION;
    }
}

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 {//w w w. ja  v 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.apache.ofbiz.entity.transaction.TransactionUtil.java

/** Rolls back transaction in the current thread IF transactions are available */
public static void rollback(Throwable causeThrowable) throws GenericTransactionException {
    UserTransaction ut = TransactionFactoryLoader.getInstance().getUserTransaction();

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

            if (status != STATUS_NO_TRANSACTION) {
                //if (Debug.infoOn()) Thread.dumpStack();
                if (causeThrowable == null && Debug.infoOn()) {
                    Exception newE = new Exception("Stack Trace");
                    Debug.logError(newE, "[TransactionUtil.rollback]", module);
                }

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

                ut.rollback();
                Debug.logInfo("Transaction rolled back", module);
            } else {
                Debug.logWarning("Transaction not rolled back, status is STATUS_NO_TRANSACTION", module);
            }
        } catch (IllegalStateException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Could not rollback transaction, IllegalStateException exception: " + t.toString(), t);
        } catch (SystemException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "System error, could not rollback transaction: " + t.toString(), t);
        }
    } else {
        Debug.logInfo("No UserTransaction, transaction not rolled back", module);
    }
}

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

/** Makes a rollback the only possible outcome of the transaction in the current thread IF transactions are available */
public static void setRollbackOnly(String causeMessage, Throwable causeThrowable)
        throws GenericTransactionException {
    UserTransaction ut = TransactionFactoryLoader.getInstance().getUserTransaction();
    if (ut != null) {
        try {//w  ww. j av  a  2s .co  m
            int status = ut.getStatus();
            Debug.logVerbose("Current code : " + getTransactionStateString(status), module);

            if (status != STATUS_NO_TRANSACTION) {
                if (status != STATUS_MARKED_ROLLBACK) {
                    if (Debug.warningOn()) {
                        Debug.logWarning(new Exception(causeMessage),
                                "Calling transaction setRollbackOnly; this stack trace shows where this is happening:",
                                module);
                    }
                    ut.setRollbackOnly();
                    setSetRollbackOnlyCause(causeMessage, causeThrowable);
                } else {
                    Debug.logInfo("Transaction rollback only not set, rollback only is already set.", module);
                }
            } else {
                Debug.logWarning("Transaction rollback only not set, status is STATUS_NO_TRANSACTION", module);
            }
        } catch (IllegalStateException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Could not set rollback only on transaction, IllegalStateException exception: "
                            + t.toString(),
                    t);
        } catch (SystemException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "System error, could not set rollback only on transaction: " + t.toString(), t);
        }
    } else {
        Debug.logInfo("No UserTransaction, transaction rollback only not set", module);
    }
}

From source file:org.eclipse.ecr.core.event.tx.EventBundleTransactionHandler.java

protected UserTransaction createUT(Integer transactionTimeout, boolean retry) {
    try {//from   w w  w. j  a  va  2s  .co  m
        new InitialContext();
    } catch (Exception e) {
        disabled = true;
        return null;
    }

    UserTransaction ut;
    try {
        ut = TransactionHelper.lookupUserTransaction();
    } catch (NamingException e) {
        disabled = true;
        return null;
    }

    try {
        int txStatus = ut.getStatus();
        if (txStatus != Status.STATUS_NO_TRANSACTION && !retry) {
            // if previous tx in this thread aborted in TimeOut
            // Ajuna may have failed to dissociate tx from thread context
            // => force rollback to avoid reusing a dead tx
            log.warn(
                    "Transaction was not properly cleanup up from thread context, rolling back before getting a new tx");
            try {
                ut.rollback();
            } catch (Throwable t) {
                log.warn("error during tx rollback", t);
            }
            return createUT(transactionTimeout, true);
        }
    } catch (Exception se) {
        log.warn("Error while getting TX status", se);
    }

    if (transactionTimeout != null) {
        try {
            ut.setTransactionTimeout(transactionTimeout);
        } catch (SystemException e) {
            log.error("Error while setting transaction timeout to " + transactionTimeout, e);
        }
    }
    return ut;
}

From source file:org.eclipse.ecr.runtime.transaction.TransactionHelper.java

/**
 * Commits or rolls back the User Transaction depending on the transaction
 * status.//from  ww w.  j  av a 2  s.  com
 *
 * @throws SystemException
 * @throws HeuristicRollbackException
 * @throws HeuristicMixedException
 * @throws RollbackException
 * @throws IllegalStateException
 * @throws SecurityException
 */
public static void commitOrRollbackTransaction() {
    UserTransaction ut;
    try {
        ut = lookupUserTransaction();
    } catch (NamingException e) {
        log.warn("No user transaction", e);
        return;
    }
    try {
        int status = ut.getStatus();
        if (status == Status.STATUS_ACTIVE) {
            if (log.isDebugEnabled()) {
                log.debug("Commiting transaction");
            }
            ut.commit();
        } else if (status == Status.STATUS_MARKED_ROLLBACK) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot commit transaction because it is marked rollback only");
            }
            ut.rollback();
        }
    } catch (Exception e) {
        String msg = "Unable to commit/rollback  " + ut;
        if (e instanceof RollbackException
                && "Unable to commit: transaction marked for rollback".equals(e.getMessage())) {
            // don't log as error, this happens if there's a
            // ConcurrentModificationException at transaction end inside VCS
            log.debug(msg, e);
        } else {
            log.error(msg, e);
        }
        throw new TransactionRuntimeException(msg, e);
    }
}

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

/**
 * Begins a transaction in the current thread IF transactions are available;
 * only tries if the current transaction status is ACTIVE, if not active it
 * returns false. If and on only if it begins a transaction it will return
 * true. In other words, if a transaction is already in place it will return
 * false and do nothing./*from  ww  w .  j av  a 2 s . c  o  m*/
 */
public static boolean begin(int timeout) throws GenericTransactionException {
    UserTransaction ut = TransactionFactory.getUserTransaction();
    if (ut != null) {
        try {
            int currentStatus = ut.getStatus();
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "[TransactionUtil.begin] current status : " + getTransactionStateString(currentStatus));
            }
            if (currentStatus == Status.STATUS_ACTIVE) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "[TransactionUtil.begin] active transaction in place, so no transaction begun");
                }
                return false;
            } else if (currentStatus == Status.STATUS_MARKED_ROLLBACK) {
                Exception e = getTransactionBeginStack();
                if (e != null) {
                    logger.warn(
                            "[TransactionUtil.begin] active transaction marked for rollback in place, so no transaction begun; this stack trace shows when the exception began: ",
                            e);
                } else {
                    logger.warn(
                            "[TransactionUtil.begin] active transaction marked for rollback in place, so no transaction begun");
                }

                RollbackOnlyCause roc = getSetRollbackOnlyCause();
                // do we have a cause? if so, throw special exception
                if (roc != null && !roc.isEmpty()) {
                    throw new GenericTransactionException(
                            "The current transaction is marked for rollback, not beginning a new transaction and aborting current operation; the rollbackOnly was caused by: "
                                    + roc.getCauseMessage(),
                            roc.getCauseThrowable());
                } else {
                    return false;
                }
            }

            internalBegin(ut, timeout);

            // reset the transaction stamps, just in case...
            clearTransactionStamps();
            // initialize the start stamp
            getTransactionStartStamp();
            // set the tx begin stack placeholder
            setTransactionBeginStack();

            // initialize the debug resource
            if (debugResources) {
                DebugXaResource dxa = new DebugXaResource();
                try {
                    dxa.enlist();
                } catch (XAException e) {
                    logger.error(e.getMessage(), e);
                }
            }

            return true;
        } catch (NotSupportedException e) {
            // This is Java 1.4 only, but useful for certain debuggins: Throwable t
            // = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Not Supported error, could not begin transaction (probably a nesting problem)", e);
        } catch (SystemException e) {
            // This is Java 1.4 only, but useful for certain debuggins: Throwable t
            // = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException("System error, could not begin transaction", e);
        }
    } else {
        if (logger.isInfoEnabled())
            logger.info("[TransactionUtil.begin] no user transaction, so no transaction begun");
        return false;
    }
}

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

/**
 * Gets the status of the transaction in the current thread IF transactions
 * are available, otherwise returns STATUS_NO_TRANSACTION
 *//*from  w w  w .ja va 2 s  .  c o m*/
public static int getStatus() throws GenericTransactionException {
    UserTransaction ut = TransactionFactory.getUserTransaction();
    if (ut != null) {
        try {
            return ut.getStatus();
        } catch (SystemException e) {
            throw new GenericTransactionException("System error, could not get status", e);
        }
    } else {
        return STATUS_NO_TRANSACTION;
    }
}

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

/**
 * Commits the transaction in the current thread IF transactions are available
 *//*w  ww  . j a  v  a  2 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");
    }
}

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

/** Rolls back transaction in the current thread IF transactions are available */
public static void rollback(Throwable causeThrowable) throws GenericTransactionException {
    UserTransaction ut = TransactionFactory.getUserTransaction();

    if (ut != null) {
        try {/*  w  ww.  j a  va 2 s.  c om*/
            int status = ut.getStatus();
            logger.debug("[TransactionUtil.rollback] current status : " + getTransactionStateString(status));

            if (status != STATUS_NO_TRANSACTION) {
                // if (Debug.infoOn()) Thread.dumpStack();
                if (causeThrowable == null && logger.isInfoEnabled()) {
                    Exception newE = new Exception("Stack Trace");
                    logger.error("[TransactionUtil.rollback]");
                }

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

                ut.rollback();
                logger.info("[TransactionUtil.rollback] transaction rolled back");
            } else {
                logger.warn(
                        "[TransactionUtil.rollback] transaction not rolled back, status is STATUS_NO_TRANSACTION");
            }
        } catch (IllegalStateException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Could not rollback transaction, IllegalStateException exception: " + t.toString(), t);
        } catch (SystemException e) {
            Throwable t = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "System error, could not rollback transaction: " + t.toString(), t);
        }
    } else {
        logger.info("[TransactionUtil.rollback] No UserTransaction, transaction not rolled back");
    }
}