Example usage for javax.persistence EntityManager isOpen

List of usage examples for javax.persistence EntityManager isOpen

Introduction

In this page you can find the example usage for javax.persistence EntityManager isOpen.

Prototype

public boolean isOpen();

Source Link

Document

Determine whether the entity manager is open.

Usage

From source file:org.apache.airavata.registry.core.experiment.catalog.ExpCatResourceUtils.java

/**
 * @param gatewayResource/*  w w  w . j a  v a  2 s. c o m*/
 * @param userResource
 * @return
 */
public static boolean removeGatewayWorker(GatewayResource gatewayResource, UserResource userResource) {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractExpCatResource.GATEWAY_WORKER);
        generator.setParameter(AbstractExpCatResource.GatewayWorkerConstants.GATEWAY_ID,
                gatewayResource.getGatewayName());
        generator.setParameter(AbstractExpCatResource.UserConstants.USERNAME, userResource.getUserName());
        Query q = generator.deleteQuery(em);
        q.executeUpdate();
        em.getTransaction().commit();
        em.close();
        return true;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }

}

From source file:org.apache.airavata.registry.core.experiment.catalog.ExpCatResourceUtils.java

public static List<String> getAllUsersInGateway(String gatewayId) throws RegistryException {
    EntityManager em = null;
    try {/*from  w w  w . java2  s .c  o m*/
        em = getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator = new QueryGenerator(AbstractExpCatResource.USERS);
        generator.setParameter(AbstractExpCatResource.UserConstants.GATEWAY_ID, gatewayId);
        Query q = generator.selectQuery(em);
        List<Users> users = q.getResultList();
        em.getTransaction().commit();
        em.close();
        ArrayList<String> usernameList = new ArrayList<>();
        if (usernameList != null) {
            for (int i = 0; i < usernameList.size(); i++) {
                usernameList.add(users.get(i).getUserName());
            }
        }
        return usernameList;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}

From source file:org.apache.airavata.registry.core.utils.JPAUtils.java

public static <R> R execute(Committer<EntityManager, R> committer) {
    EntityManager entityManager = JPAUtils.getEntityManager();
    try {/*from   w ww .  j  a va2 s . c o m*/
        entityManager.getTransaction().begin();
        R r = committer.commit(entityManager);
        entityManager.getTransaction().commit();
        return r;
    } finally {
        if (entityManager != null && entityManager.isOpen()) {
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
            entityManager.close();
        }
    }
}

From source file:org.apache.hadoop.crypto.key.RangerKMSDB.java

private boolean isDbConnected() {
    EntityManager em = getEntityManager();

    return em != null && em.isOpen();
}

From source file:org.apache.oozie.service.JPAService.java

/**
 * Execute a {@link JPAExecutor}.//w  w w  . j  a v  a2  s .  c  om
 *
 * @param executor JPAExecutor to execute.
 * @return return value of the JPAExecutor.
 * @throws JPAExecutorException thrown if an jpa executor failed
 */
public <T> T execute(JPAExecutor<T> executor) throws JPAExecutorException {
    EntityManager em = getEntityManager();
    Instrumentation.Cron cron = new Instrumentation.Cron();
    try {
        LOG.trace("Executing JPAExecutor [{0}]", executor.getName());
        if (instr != null) {
            instr.incr(INSTRUMENTATION_GROUP_JPA, executor.getName(), 1);
        }
        cron.start();
        em.getTransaction().begin();
        T t = executor.execute(em);
        if (em.getTransaction().isActive()) {
            if (FaultInjection.isActive("org.apache.oozie.command.SkipCommitFaultInjection")) {
                throw new RuntimeException("Skipping Commit for Failover Testing");
            }

            em.getTransaction().commit();
        }
        return t;
    } catch (PersistenceException e) {
        throw new JPAExecutorException(ErrorCode.E0603, e);
    } finally {
        cron.stop();
        if (instr != null) {
            instr.addCron(INSTRUMENTATION_GROUP_JPA, executor.getName(), cron);
        }
        try {
            if (em.getTransaction().isActive()) {
                LOG.warn("JPAExecutor [{0}] ended with an active transaction, rolling back",
                        executor.getName());
                em.getTransaction().rollback();
            }
        } catch (Exception ex) {
            LOG.warn("Could not check/rollback transaction after JPAExecutor [{0}], {1}", executor.getName(),
                    ex.getMessage(), ex);
        }
        try {
            if (em.isOpen()) {
                em.close();
            } else {
                LOG.warn("JPAExecutor [{0}] closed the EntityManager, it should not!", executor.getName());
            }
        } catch (Exception ex) {
            LOG.warn("Could not close EntityManager after JPAExecutor [{0}], {1}", executor.getName(),
                    ex.getMessage(), ex);
        }
    }
}

From source file:org.apache.oozie.service.JPAService.java

private void processFinally(EntityManager em, Instrumentation.Cron cron, String name, boolean checkActive) {
    cron.stop();/*  ww  w.j  a  v  a  2s  . c o  m*/
    if (instr != null) {
        instr.addCron(INSTRUMENTATION_GROUP_JPA, name, cron);
    }
    if (checkActive) {
        try {
            if (em.getTransaction().isActive()) {
                LOG.warn("[{0}] ended with an active transaction, rolling back", name);
                em.getTransaction().rollback();
            }
        } catch (Exception ex) {
            LOG.warn("Could not check/rollback transaction after [{0}], {1}", name, ex.getMessage(), ex);
        }
    }
    try {
        if (em.isOpen()) {
            em.close();
        } else {
            LOG.warn("[{0}] closed the EntityManager, it should not!", name);
        }
    } catch (Exception ex) {
        LOG.warn("Could not close EntityManager after [{0}], {1}", name, ex.getMessage(), ex);
    }
}

From source file:org.apache.ranger.audit.destination.DBAuditDestination.java

private boolean isDbConnected() {
    EntityManager em = getEntityManager();
    return em != null && em.isOpen();
}

From source file:org.drools.semantics.lang.dl.DL_9_CompilationTest.java

private void checkSQLRefresh(Object obj, ClassLoader urlk, String punit) {
    ClassLoader oldKL = Thread.currentThread().getContextClassLoader();
    EntityManagerFactory emf = null;/*from   w  w  w .  ja v  a 2s  . co m*/
    EntityManager em = null;
    try {
        Thread.currentThread().setContextClassLoader(urlk);

        HashMap props = new HashMap();
        props.put("hibernate.hbm2ddl.auto", "create-drop");
        emf = Persistence.createEntityManagerFactory(punit, props);

        em = emf.createEntityManager();

        checkJPARefresh(obj, ((UIdAble) obj).getDyEntryId(), em);

    } finally {
        Thread.currentThread().setContextClassLoader(oldKL);
        if (em != null && em.isOpen()) {
            em.clear();
            em.close();
        }
        if (emf != null && emf.isOpen()) {
            emf.close();
        }
    }

}

From source file:org.eclipse.jubula.client.core.persistence.Persistor.java

/**
 * @param s// w  ww .  ja v  a  2  s. co m
 *            session to close
 * @param dropLocks
 *            should I drop LockManager locks?
 * @throws PMException
 *             in case of any db error
 */
private void closeSession(EntityManager s, boolean dropLocks) throws PMException {

    Validate.notNull(s);
    try {
        if (s.isOpen()) {
            try {
                EntityTransaction tx = s.getTransaction();
                if (tx.isActive()) {
                    rollbackTransaction(s, tx);
                }
            } finally {
                s.close();
            }
        }
    } catch (PersistenceException e) {
        log.error(Messages.CloseSessionFailed, e);
    } finally {
        if (dropLocks) {
            removeLocks(s);
        }
        m_sessions.remove(s);
    }
}

From source file:org.eclipse.jubula.client.core.persistence.Persistor.java

/**
 * //from   ww  w . ja v a 2  s .  co m
 */
public void dispose() {
    if (m_sf != null) {
        try {
            for (EntityManager sess : m_sessions) {
                if (sess.isOpen()) {
                    sess.close();
                }
            }
            m_sessions.clear();
            m_sf.close();
            DatabaseStateDispatcher.notifyListener(new DatabaseStateEvent(DatabaseState.DB_LOGOUT_SUCCEEDED));
        } catch (Throwable e) {
            log.error(Messages.DisposeOfPersistorFailed, e);
        }
    }

    instance = null;
}