Example usage for javax.transaction TransactionManager getStatus

List of usage examples for javax.transaction TransactionManager getStatus

Introduction

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

Prototype

public 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

public static void enlistResource(XAResource resource) throws GenericTransactionException {
    if (resource == null) {
        return;//from www  .  j a  va  2  s.co  m
    }

    try {
        TransactionManager tm = TransactionFactoryLoader.getInstance().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.apache.ofbiz.entity.transaction.TransactionUtil.java

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

    try {
        TransactionManager tm = TransactionFactoryLoader.getInstance().getTransactionManager();
        if (tm != null && tm.getStatus() == STATUS_ACTIVE) {
            Transaction tx = tm.getTransaction();
            if (tx != null) {
                tx.registerSynchronization(sync);
            }
        }
    } catch (RollbackException e) {
        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) {
        throw new GenericTransactionException(
                "System error, could not register synchronization in transaction even though transactions are available",
                e);
    }
}

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

public static void enlistResource(XAResource resource) throws GenericTransactionException {
    if (resource == null) {
        return;// w  w w. j a v  a2 s .c o  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 ww w.  j  a  v  a2s.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.nuxeo.runtime.transaction.TransactionHelper.java

/**
 * Commit the current transaction if active and resume the principal transaction
 *
 * @param tx/*from  w w  w  .j  a v  a  2  s. c o  m*/
 */
public static void resumeTransaction(Transaction tx) {
    TransactionManager tm = NuxeoContainer.getTransactionManager();
    if (tm == null) {
        return;
    }
    try {
        if (tm.getStatus() == Status.STATUS_ACTIVE) {
            tm.commit();
        }
        if (tx != null) {
            tm.resume(tx);
        }
    } catch (SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException
            | InvalidTransactionException | IllegalStateException | SecurityException e) {
        throw new TransactionRuntimeException("Cannot resume tx", e);
    }
}

From source file:org.openejb.transaction.TxRequired.java

public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation,
        TransactionManager transactionManager) throws Throwable {
    Transaction transaction = transactionManager.getTransaction();
    if (transaction != null) {
        try {/*from w  w  w .  ja v a2  s  .c o m*/
            return interceptor.invoke(ejbInvocation);
        } catch (Throwable t) {
            transactionManager.setRollbackOnly();
            if (ejbInvocation.getType().isLocal()) {
                throw new TransactionRolledbackLocalException().initCause(t);
            } else {
                // can't set an initCause on a TransactionRolledbackException
                throw new TransactionRolledbackException(t.getMessage());
            }
        }
    }

    transactionManager.begin();
    try {
        InvocationResult result = interceptor.invoke(ejbInvocation);
        return result;
    } catch (RollbackException re) {
        throw re;
    } catch (Throwable t) {
        try {
            transactionManager.setRollbackOnly();
        } catch (Exception e) {
            log.warn("Unable to roll back", e);
        }
        throw t;
    } finally {
        if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
            transactionManager.commit();
        } else {
            transactionManager.rollback();
        }
    }
}

From source file:org.openejb.transaction.TxRequiresNew.java

public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation,
        TransactionManager transactionManager) throws Throwable {
    Transaction callerTransaction = transactionManager.suspend();
    try {// w w w . j av a 2  s.  co  m
        transactionManager.begin();
        try {
            InvocationResult result = interceptor.invoke(ejbInvocation);
            return result;
        } catch (RollbackException re) {
            throw re;
        } catch (Throwable t) {
            try {
                transactionManager.setRollbackOnly();
            } catch (Exception e) {
                log.warn("Unable to roll back", e);
            }
            throw t;
        } finally {
            if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
                transactionManager.commit();
            } else {
                transactionManager.rollback();
            }
        }
    } finally {
        if (callerTransaction != null) {
            transactionManager.resume(callerTransaction);
        }
    }
}

From source file:org.springframework.data.neo4j.support.DelegatingGraphDatabase.java

@Override
public boolean transactionIsRunning() {
    if (!(delegate instanceof AbstractGraphDatabase)) {
        return true; // assume always running tx (e.g. for REST or other remotes)
    }//from  w  w w  . jav  a 2s.co  m
    try {
        final TransactionManager txManager = ((AbstractGraphDatabase) delegate).getConfig().getTxModule()
                .getTxManager();
        return txManager.getStatus() != Status.STATUS_NO_TRANSACTION;
    } catch (SystemException e) {
        log.error("Error accessing TransactionManager", e);
        return false;
    }
}

From source file:org.springframework.data.neo4j.support.GraphDatabaseContext.java

/**
 * @return true if a transaction manager is available and a transaction is currently running
 *//*from  www .  ja  va  2  s  . c  om*/
public boolean transactionIsRunning() {
    if (!(graphDatabaseService instanceof AbstractGraphDatabase)) {
        return true; // assume always running tx (e.g. for REST or other remotes)
    }
    try {
        final TransactionManager txManager = ((AbstractGraphDatabase) graphDatabaseService).getConfig()
                .getTxModule().getTxManager();
        return txManager.getStatus() != Status.STATUS_NO_TRANSACTION;
    } catch (SystemException e) {
        log.error("Error accessing TransactionManager", e);
        return false;
    }
}

From source file:org.springframework.jdbc.support.lob.LobCreatorUtils.java

/**
 * Register a transaction synchronization for closing the given LobCreator,
 * preferring Spring transaction synchronization and falling back to
 * plain JTA transaction synchronization.
 * @param lobCreator the LobCreator to close after transaction completion
 * @param jtaTransactionManager the JTA TransactionManager to fall back to
 * when no Spring transaction synchronization is active (may be {@code null})
 * @throws IllegalStateException if there is neither active Spring transaction
 * synchronization nor active JTA transaction synchronization
 *///from w  ww .  j av a 2s. co  m
public static void registerTransactionSynchronization(LobCreator lobCreator,
        TransactionManager jtaTransactionManager) throws IllegalStateException {

    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        logger.debug("Registering Spring transaction synchronization for LobCreator");
        TransactionSynchronizationManager
                .registerSynchronization(new SpringLobCreatorSynchronization(lobCreator));
    } else {
        if (jtaTransactionManager != null) {
            try {
                int jtaStatus = jtaTransactionManager.getStatus();
                if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
                    logger.debug("Registering JTA transaction synchronization for LobCreator");
                    jtaTransactionManager.getTransaction()
                            .registerSynchronization(new JtaLobCreatorSynchronization(lobCreator));
                    return;
                }
            } catch (Throwable ex) {
                throw new TransactionSystemException(
                        "Could not register synchronization with JTA TransactionManager", ex);
            }
        }
        throw new IllegalStateException("Active Spring transaction synchronization or active "
                + "JTA transaction with specified [javax.transaction.TransactionManager] required");
    }
}