Example usage for javax.transaction TransactionManager getTransaction

List of usage examples for javax.transaction TransactionManager getTransaction

Introduction

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

Prototype

public Transaction getTransaction() throws SystemException;

Source Link

Document

Get the transaction object that represents the transaction context of the calling thread.

Usage

From source file:org.wso2.carbon.humantask.core.scheduler.SimpleScheduler.java

public <T> T execTransaction(Callable<T> transaction, int timeout) throws Exception {
    TransactionManager txm = transactionManager;
    if (txm == null) {
        throw new HumanTaskException(
                "Cannot locate the transaction manager; " + "the server might be shutting down.");
    }/*from   ww w  .  j  a v a2 s .c  o m*/

    // The value of the timeout is in seconds. If the value is zero, 
    // the transaction service restores the default value.
    if (timeout < 0) {
        throw new IllegalArgumentException("Timeout must be positive, received: " + timeout);
    }

    boolean existingTransaction;
    try {
        existingTransaction = txm.getTransaction() != null;
    } catch (Exception ex) {
        String errMsg = "Internal Error, could not get current transaction.";
        throw new HumanTaskException(errMsg, ex);
    }

    // already in transaction, execute and return directly
    if (existingTransaction) {
        return transaction.call();
    }

    // run in new transaction
    Exception ex = null;
    //        int immediateRetryCount = _immediateTransactionRetryLimit;

    transactionManager.setTransactionTimeout(timeout);
    if (log.isDebugEnabled() && timeout != 0) {
        log.debug("Custom transaction timeout: " + timeout);
    }

    try {
        try {
            if (log.isDebugEnabled()) {
                log.debug("Beginning a new transaction");
            }
            txm.begin();
        } catch (Exception e) {
            String errMsg = "Internal Error, could not begin transaction.";
            throw new HumanTaskException(errMsg, e);
        }

        try {
            ex = null;
            return transaction.call();
        } catch (Exception e) {
            ex = e;
        } finally {
            if (ex == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Committing on " + txm + "...");
                }
                try {
                    txm.commit();
                } catch (Exception e2) {
                    ex = e2;
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Rollbacking on " + txm + "...");
                }
                txm.rollback();
            }

            //                    if (ex != null && immediateRetryCount > 0) {
            //                        if (log.isDebugEnabled()) {
            //                            log.debug("Will retry the transaction in " +
            //                                    _immediateTransactionRetryInterval + " msecs on " +
            //                                    transactionManager + " for error: ", ex);
            //                        }
            //                        Thread.sleep(_immediateTransactionRetryInterval);
            //                    }
        }
    } finally {
        // 0 restores the default value
        transactionManager.setTransactionTimeout(0);
    }

    throw ex;
}