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.compass.core.transaction.AbstractJTATransaction.java

public void begin(InternalCompassSession session, TransactionManager transactionManager)
        throws CompassException {

    try {//from   www.  ja va 2  s. co  m
        this.session = session;
        controllingNewTransaction = true;
        newTransaction = ut.getStatus() == Status.STATUS_NO_TRANSACTION;
        if (newTransaction) {
            if (log.isDebugEnabled()) {
                log.debug("Beginning new JTA transaction, and a new compass transaction on thread ["
                        + Thread.currentThread().getName() + "]");
            }
            session.getSearchEngine().begin();

            int timeout = session.getSettings()
                    .getSettingAsInt(CompassEnvironment.Transaction.TRANSACTION_TIMEOUT, -1);
            if (timeout != -1) {
                ut.setTransactionTimeout(timeout);
            }
            ut.begin();
        } else {
            // joining an exisiting transaction
            session.getSearchEngine().begin();
            if (log.isDebugEnabled()) {
                log.debug("Joining an existing JTA transaction, starting a new compass transaction on thread ["
                        + Thread.currentThread().getName() + "] with status [" + ut.getStatus() + "]");
            }
        }
        javax.transaction.Transaction tx = transactionManager.getTransaction();
        doBindToTransaction(tx, session, newTransaction);
    } catch (Exception e) {
        throw new TransactionException("Begin failed with exception", e);
    }
    setBegun(true);
}

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

public static void enlistResource(XAResource resource) throws GenericTransactionException {
    if (resource == null) {
        return;/*  ww w  . j a  v a2 s.  co  m*/
    }

    try {
        TransactionManager tm = TransactionFactory.getTransactionManager();
        if (tm != null && tm.getStatus() == STATUS_ACTIVE) {
            Transaction tx = tm.getTransaction();
            if (tx != null) {
                tx.enlistResource(resource);
            }
        }
    } catch (RollbackException e) {
        // This is Java 1.4 only, but useful for certain debuggins: Throwable t =
        // e.getCause() == null ? e : e.getCause();
        throw new GenericTransactionException(
                "Roll Back error, could not enlist resource in transaction even though transactions are available, current transaction rolled back",
                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 enlist resource in transaction even though transactions are available",
                e);
    }
}

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

public static void registerSynchronization(Synchronization sync) throws GenericTransactionException {
    if (sync == null) {
        return;/*from  w ww . java2  s  . c o  m*/
    }

    try {
        TransactionManager tm = TransactionFactory.getTransactionManager();
        if (tm != null && tm.getStatus() == STATUS_ACTIVE) {
            Transaction tx = tm.getTransaction();
            if (tx != null) {
                tx.registerSynchronization(sync);
            }
        }
    } catch (RollbackException e) {
        // This is Java 1.4 only, but useful for certain debuggins: Throwable t =
        // e.getCause() == null ? e : e.getCause();
        throw new GenericTransactionException(
                "Roll Back error, could not register synchronization in transaction even though transactions are available, current transaction rolled back",
                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 register synchronization in transaction even though transactions are available",
                e);
    }
}

From source file:org.jboss.jbossts.fileio.xalib.txdirs.dir.XADir.java

/**
 * This method must be used after a <code>TransactionManager</code>
 * has begun and within the boundaries of a transaction (<code>begin,
 * commit/rollback</code>)./*w w  w. j  a  va 2  s . c  o  m*/
 * <p>
 * The method also creates a new {@link XAFileResourceManager} and
 * enlists it to the transaction obtained by the <code>txnMngr</code>.
 *
 * @param txnMngr the <code>TransactionManager</code> used to commit
 *                or rollback the file operations
 * @exception javax.transaction.RollbackException
 *            if an error occurs while enlisting the <code>XAResource</code>
 * @exception javax.transaction.SystemException
 *            if there is a problem enlisting the XAResource created
 *            within this method or when the <code>getTransaction</code> in the
 *            <code>TransactionManager</code> fails or when the TransactionManager
 *            is not in ready (<code>Status.ACTIVE</code>) mode
 */
public synchronized void startTransactionOn(TransactionManager txnMngr)
        throws SystemException, RollbackException {
    curTxId = "txDir-" + freMngr.getWorkDir().replace('/', '_').replace('\\', '_').replace(':', '_') + "_"
            + Thread.currentThread().getId() + "!" + System.nanoTime();
    XAFileResourceManager xafre = new XAFileResourceManager(freMngr, curTxId);
    Transaction tx = txnMngr.getTransaction();
    tx.enlistResource(xafre);
}

From source file:org.nuxeo.ecm.core.storage.sql.UnifiedCachingRowMapper.java

protected boolean hasTransaction() {
    TransactionManagerLookup transactionManagerLookup = cache.getTransactionManagerLookup();
    if (transactionManagerLookup == null) {
        return false;
    }/*from  w w w.  j ava  2s.  c  o m*/
    TransactionManager transactionManager = transactionManagerLookup.getTransactionManager();
    if (transactionManager == null) {
        return false;
    }
    Transaction transaction;
    try {
        transaction = transactionManager.getTransaction();
    } catch (SystemException e) {
        throw new RuntimeException(e);
    }
    return transaction != null;
}

From source file:org.nuxeo.ecm.core.work.WorkManagerImpl.java

/**
 * Schedule after commit. Returns {@code false} if impossible (no
 * transaction or transaction manager).//from  ww w  .  java2  s .c  o m
 *
 * @since 5.8
 */
protected boolean scheduleAfterCommit(Work work, Scheduling scheduling) {
    TransactionManager transactionManager;
    try {
        transactionManager = TransactionHelper.lookupTransactionManager();
    } catch (NamingException e) {
        transactionManager = null;
    }
    if (transactionManager == null) {
        if (log.isDebugEnabled()) {
            log.debug("Not scheduling work after commit because of missing transaction manager: " + work);
        }
        return false;
    }
    try {
        Transaction transaction = transactionManager.getTransaction();
        if (transaction == null) {
            if (log.isDebugEnabled()) {
                log.debug("Not scheduling work after commit because of missing transaction: " + work);
            }
            return false;
        }
        int status = transaction.getStatus();
        if (status == Status.STATUS_ACTIVE) {
            if (log.isDebugEnabled()) {
                log.debug("Scheduling work after commit: " + work);
            }
            transaction.registerSynchronization(new WorkScheduling(work, scheduling));
            return true;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Not scheduling work after commit because transaction is in status " + status + ": "
                        + work);
            }
            return false;
        }
    } catch (SystemException | RollbackException e) {
        log.error("Cannot schedule after commit", e);
        return false;
    }
}

From source file:org.nuxeo.ecm.platform.importer.base.TxHelper.java

protected Transaction createTxFromTM() {
    InitialContext context = null;
    try {//from w  w  w.ja v  a 2 s  . c o  m
        context = new InitialContext();
    } catch (Exception e) {
        disabled = true;
        return null;
    }

    TransactionManager tm = null;
    try {
        tm = (TransactionManager) context.lookup(TM_NAME);
    } catch (NamingException ne) {
        try {
            tm = (TransactionManager) context.lookup(TM_NAME_ALT);
        } catch (NamingException ne2) {
        }
    }
    if (tm == null) {
        disabled = true;
        return null;
    }

    try {
        return tm.getTransaction();
    } catch (SystemException e) {
        disabled = true;
        return null;
    }
}

From source file:org.nuxeo.elasticsearch.listener.ElasticSearchInlineListener.java

protected boolean registerSynchronization(Synchronization sync) {
    try {/*from  w w  w. j av  a 2  s .c  o m*/
        TransactionManager tm = TransactionHelper.lookupTransactionManager();
        if (tm != null) {
            if (tm.getTransaction() != null) {
                tm.getTransaction().registerSynchronization(sync);
                return true;
            }
            if (!Framework.isTestModeSet()) {
                log.error("Unable to register synchronization : no active transaction");
            }
            return false;
        } else {
            log.error("Unable to register synchronization : no TransactionManager");
            return false;
        }
    } catch (NamingException | IllegalStateException | SystemException | RollbackException e) {
        log.error("Unable to register synchronization", e);
        return false;
    }
}

From source file:org.nuxeo.runtime.transaction.TransactionHelper.java

/**
 * Checks if the current User Transaction has already timed out, i.e., whether a commit would immediately abort with
 * a timeout exception./*  w  w  w  .  j  ava2 s  . c  om*/
 *
 * @return {@code true} if there is a current transaction that has timed out, {@code false} otherwise
 * @since 7.1
 */
public static boolean isTransactionTimedOut() {
    TransactionManager tm = NuxeoContainer.getTransactionManager();
    if (tm == null) {
        return false;
    }
    try {
        Transaction tx = tm.getTransaction();
        if (tx == null || tx.getStatus() != Status.STATUS_ACTIVE) {
            return false;
        }
        if (tx instanceof org.apache.geronimo.transaction.manager.TransactionImpl) {
            // Geronimo Transaction Manager
            Long timeout = (Long) GERONIMO_TRANSACTION_TIMEOUT_FIELD.get(tx);
            return System.currentTimeMillis() > timeout.longValue();
        } else {
            // unknown transaction manager
            return false;
        }
    } catch (SystemException | ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.nuxeo.runtime.transaction.TransactionHelper.java

/**
 * Suspend the current transaction if active and start a new transaction
 *
 * @return the suspended transaction or null
 * @throws TransactionRuntimeException/*from   w ww  . j a  v  a 2 s .co m*/
 * @since 5.6
 */
public static Transaction requireNewTransaction() {
    TransactionManager tm = NuxeoContainer.getTransactionManager();
    if (tm == null) {
        return null;
    }
    try {
        Transaction tx = tm.getTransaction();
        if (tx != null) {
            tx = tm.suspend();
        }
        tm.begin();
        return tx;
    } catch (NotSupportedException | SystemException e) {
        throw new TransactionRuntimeException("Cannot suspend tx", e);
    }
}