Example usage for javax.transaction Status STATUS_ACTIVE

List of usage examples for javax.transaction Status STATUS_ACTIVE

Introduction

In this page you can find the example usage for javax.transaction Status STATUS_ACTIVE.

Prototype

int STATUS_ACTIVE

To view the source code for javax.transaction Status STATUS_ACTIVE.

Click Source Link

Document

A transaction is associated with the target object and it is in the active state.

Usage

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

/**
 * Checks if the current User Transaction is active or preparing.
 *
 * @since 8.4//ww  w  .  j  a va2 s .  c  o  m
 */
public static boolean isTransactionActiveOrPreparing() {
    int status = getTransactionStatus();
    return status == Status.STATUS_ACTIVE || status == Status.STATUS_PREPARING;
}

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 a v a 2 s.  co  m*/
 *
 * @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

/**
 * Commit the current transaction if active and resume the principal transaction
 *
 * @param tx//from   w w w  .  j av  a2s.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.nuxeo.runtime.transaction.TransactionHelper.java

/**
 * Commits or rolls back the User Transaction depending on the transaction status.
 *//*w  ww . j av a2s  . c o m*/
public static void commitOrRollbackTransaction() {
    UserTransaction ut = NuxeoContainer.getUserTransaction();
    if (ut == null) {
        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();
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Cannot commit transaction with unknown status: " + status);
            }
        }
    } catch (SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException
            | IllegalStateException | SecurityException e) {
        String msg = "Unable to commit/rollback";
        String message = e.getMessage();
        if (e instanceof RollbackException) {
            switch (message) {
            case "Unable to commit: transaction marked for rollback":
                // don't log as error, this happens if there's a ConcurrentModificationException
                // at transaction end inside VCS
            case "Unable to commit: Transaction timeout":
                // don't log either
                log.debug(msg, e);
                break;
            default:
                log.error(msg, e);
            }
        } else {
            log.error(msg, e);
        }
        throw new TransactionRuntimeException(msg + ": " + message, 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  ww w  .  j av  a2s . 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 {//from www.j  av a 2s.c om
        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.ovirt.engine.core.bll.CommandBase.java

public void endActionInTransactionScope() {
    try {// www .  j a v a2s. co m
        if (getParameters().getTaskGroupSuccess()) {
            InternalEndSuccessfully();
        } else {
            InternalEndWithFailure();
        }
    } finally {
        if (TransactionSupport.current() == null) {
            cleanUpCompensationData();
        } else {
            try {
                if (TransactionSupport.current().getStatus() == Status.STATUS_ACTIVE) {
                    cleanUpCompensationData();
                } else {
                    compensate();
                }
            } catch (SystemException e) {
                log.errorFormat("Exception while wrapping-up compensation in endAction: {0}.",
                        ExceptionUtils.getMessage(e), e);
                compensate();
            }
        }
    }
}

From source file:org.rhq.enterprise.server.purge.PurgeTemplate.java

private void rollbackIfTransactionActive() {
    try {//w  w w  . j  av a 2  s  .  co  m
        if (userTransaction.getStatus() == Status.STATUS_ACTIVE) {
            userTransaction.rollback();
        }
    } catch (Throwable ignore) {
    }
}

From source file:org.sakaiproject.kernel.test.KernelIntegrationBase.java

public static synchronized boolean beforeClass() throws ComponentActivatorException {
    if (kernelError) {
        throw new Error("Kernel Startup failed previously, test abandoned");
    }//  ww  w.  j a  va  2s.  c  om
    if (kernelManager == null) {
        System.err.println("no kernel has been started ");
        // If there are problems with startup and shutdown, these will prevent the
        // problem
        File jcrBase = new File("target/jcr");
        File dbBase = new File("target/testdb");
        System.err.println("==========================================================================");
        System.err.println("Removing all previous JCR and DB traces from " + jcrBase.getAbsolutePath() + " "
                + dbBase.getAbsolutePath());

        FileUtil.deleteAll(jcrBase);
        FileUtil.deleteAll(dbBase);
        System.err.println("==========================================================================");

        KernelManager.setTestMode();
        System.setProperty("sakai.kernel.properties",
                "inline://core.component.locations=\ncomponent.locations=classpath:;\n");

        kernelLifecycle = new KernelLifecycle();
        kernelLifecycle.start();

        kernelManager = new KernelManager();
        userEnvironmentResolverService = kernelManager.getService(UserEnvironmentResolverService.class);
        transactionManager = kernelManager.getService(TransactionManager.class);

        JCRService jcrService = kernelManager.getService(JCRService.class);
        JCRNodeFactoryService jcrNodeFactoryService = kernelManager.getService(JCRNodeFactoryService.class);

        try {
            jcrService.login();
        } catch (LoginException e) {
            LOG.error(e);
        } catch (RepositoryException e) {
            LOG.error(e);
        }

        for (String checkUser : CHECKUSERS) {
            UserEnvironment ub = userEnvironmentResolverService.resolve(checkUser);
            if (ub instanceof NullUserEnvironment || !checkUser.equals(ub.getUser().getUuid())) {
                kernelError = true;
                throw new Error("Failed to Start Kernel Correctly, didnt find the user environmnt for "
                        + checkUser + " indicating it was not added by default. This is an error with "
                        + "the inital population of the reository (or that user is no longer created)");
            }
        }
        try {
            jcrService.save();
            if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
                LOG.error("Uncommitted Transaction Found at test startup");
                transactionManager.commit();
            }
        } catch (Exception ex) {
            kernelError = true;
            throw new Error("Found uncommitted transaction at kernel start ", ex);
        }
        try {
            jcrService.logout();
        } catch (LoginException e) {
            LOG.error(e);
        } catch (RepositoryException e) {
            LOG.error(e);
        }

        KernelIntegrationBase.checkNotNull(jcrService);

        // check for active sessions, there should not be any
        KernelIntegrationBase.checkFalse(jcrService.hasActiveSession());

        // login as system since configuration is not visible
        try {
            jcrService.loginSystem();
            Node n = jcrNodeFactoryService.getNode("/configuration/repositoryconfig.xml");
            KernelIntegrationBase.checkNotNull(n);
            // check we can see the template
            n = jcrNodeFactoryService.getNode("/configuration/defaults/sitetypes/project-site.json");
            KernelIntegrationBase.checkNotNull(n);

            // logout
            jcrService.logout();
        } catch (Exception ex) {
            throw new Error(ex);
        }
        return true;
    } else {
        System.err.println("Reusing the kernel ");
        return false;
    }
}

From source file:org.sakaiproject.kernel.test.KernelIntegrationBase.java

public static void afterClass(boolean shutdown) {

    if (false) {//  w ww  . ja v  a  2 s . c o  m
        try {
            kernelLifecycle.stop();
            KernelManager.clearTestMode();
        } catch (Exception ex) {
            LOG.info("Failed to stop kernel ", ex);
        }
        kernelManager = null;
        kernelLifecycle = null;
        KernelIntegrationBase.enableKernelStartup();
    } else {
        System.err.println("Keeping kernel alive ");
    }
    if (transactionManager != null) {

        try {
            if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
                LOG.error("Uncommitted Transaction Found at test shutdown");
                transactionManager.commit();
            }
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RollbackException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (HeuristicMixedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (HeuristicRollbackException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SystemException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}