Example usage for javax.transaction TransactionManager begin

List of usage examples for javax.transaction TransactionManager begin

Introduction

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

Prototype

public void begin() throws NotSupportedException, SystemException;

Source Link

Document

Create a new transaction and associate it with the current thread.

Usage

From source file:org.topazproject.otm.impl.AbstractSession.java

private void ensureTxActive(boolean start, int txTimeout) throws OtmException {
    if (jtaTxn != null)
        return;/*from w ww . ja  va 2  s  .  c  o m*/

    try {
        TransactionManager tm = getSessionFactory().getTransactionManager();

        jtaTxn = tm.getTransaction();
        if (jtaTxn == null) {
            if (!start)
                throw new OtmException("No active transaction");

            tm.setTransactionTimeout(txTimeout > 0 ? txTimeout : 0);
            tm.begin();
            jtaTxn = tm.getTransaction();
        }

        jtaTxn.registerSynchronization(new Synchronization() {
            public void beforeCompletion() {
                if (getFlushMode().implies(FlushMode.commit) && !isRollback())
                    flush();
            }

            public void afterCompletion(int status) {
                endTransaction();
            }

            private boolean isRollback() throws OtmException {
                try {
                    return (jtaTxn.getStatus() == Status.STATUS_MARKED_ROLLBACK);
                } catch (Exception e) {
                    throw new OtmException("Error getting rollback-only status", e);
                }
            }
        });
    } catch (OtmException oe) {
        throw oe;
    } catch (Exception e) {
        throw new OtmException("Error setting up transaction", e);
    }
}

From source file:org.wso2.carbon.dataservices.core.description.xa.DSSXATransactionManager.java

public void begin() throws DataServiceFault {
    TransactionManager txManager = getTransactionManager();
    if (txManager == null) {
        return;/*from  ww  w. ja  va 2  s.  c o m*/
    }
    try {
        if (log.isDebugEnabled()) {
            log.debug("DXXATransactionManager.begin()");
        }
        txManager.begin();
        this.beginTx.set(true);
    } catch (Exception e) {
        throw new DataServiceFault(e, "Error from transaction manager in " + "begin(): " + e.getMessage());
    }
}

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  .  ja  va2  s  .  co 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;
}

From source file:org.wso2.carbon.rssmanager.core.RSSTransactionManager.java

public void begin() throws RSSManagerException {
    TransactionManager txManager = getTransactionManager();
    try {//from  ww  w.jav  a 2  s .  c  om
        if (log.isDebugEnabled()) {
            log.debug("RSSXATransactionManager.begin()");
        }
        if (this.hasNoActiveTransaction()) {
            if (log.isDebugEnabled()) {
                log.debug("transactionManager.begin()");
            }
            txManager.begin();
            this.beginTx.set(true);
        }
    } catch (Exception e) {
        throw new RSSManagerException("Error from transaction manager", e);
    }
}

From source file:org.wso2.carbon.rssmanager.data.mgt.retriever.util.StorageUsageTransactionManager.java

public void begin() throws UsageManagerException {
    TransactionManager txManager = getTransactionManager();
    try {//w  ww .  j  a v a 2  s . c o m
        if (log.isDebugEnabled()) {
            log.debug("RSSXATransactionManager.begin()");
        }
        if (this.hasNoActiveTransaction()) {
            if (log.isDebugEnabled()) {
                log.debug("transactionManager.begin()");
            }
            txManager.begin();
            this.beginTx.set(true);
        }
    } catch (Exception e) {
        throw new UsageManagerException("Error from transaction manager", e);
    }
}