Example usage for javax.persistence EntityManager close

List of usage examples for javax.persistence EntityManager close

Introduction

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

Prototype

public void close();

Source Link

Document

Close an application-managed entity manager.

Usage

From source file:nz.co.testamation.hibernate.HibernateEntityTemplate.java

public void doWithNewEntity(EntityWork work) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    work.execute(entityManager);//from  w w  w  .ja  v a  2  s . c  om
    entityManager.close();
}

From source file:com.enioka.jqm.tools.Main.java

private static void ws(String option) {
    if ("enable".equals(option)) {
        EntityManager em = Helpers.getNewEm();
        Helpers.setSingleParam("disableWsApi", "false", em);
        Helpers.setSingleParam("enableWsApiSsl", "false", em);
        Helpers.setSingleParam("enableWsApiAuth", "true", em);
        Helpers.setSingleParam("disableWsApiSimple", "false", em);
        Helpers.setSingleParam("disableWsApiClient", "false", em);
        Helpers.setSingleParam("disableWsApiAdmin", "false", em);
        Helpers.setSingleParam("enableInternalPki", "true", em);

        em.getTransaction().begin();/*from  www.  j a  v a  2 s.c om*/
        em.createQuery(
                "UPDATE Node n set n.loapApiSimple = true, n.loadApiClient = true, n.loadApiAdmin = true, n.dns=:n")
                .setParameter("n", "0.0.0.0").executeUpdate();
        em.getTransaction().commit();
        em.close();
    } else if ("disable".equals(option)) {
        EntityManager em = Helpers.getNewEm();
        em.getTransaction().begin();
        em.createQuery("UPDATE Node n set n.loadApiClient = false, n.loadApiAdmin = false").executeUpdate();
        em.getTransaction().commit();
        em.close();
    }
    if ("ssl".equals(option)) {
        EntityManager em = Helpers.getNewEm();
        Helpers.setSingleParam("enableWsApiSsl", "true", em);
        Helpers.setSingleParam("enableWsApiAuth", "true", em);
        em.close();
    }
    if ("nossl".equals(option)) {
        EntityManager em = Helpers.getNewEm();
        Helpers.setSingleParam("enableWsApiSsl", "false", em);
        em.close();
    }
    if ("internalpki".equals(option)) {
        EntityManager em = Helpers.getNewEm();
        Helpers.setSingleParam("enableInternalPki", "true", em);
        em.close();
    }
    if ("externalpki".equals(option)) {
        EntityManager em = Helpers.getNewEm();
        Helpers.setSingleParam("enableInternalPki", "false", em);
        em.close();
    }
}

From source file:controllers.base.SareTransactionalAction.java

public static <T> T execute(SareTxRunnable<T> action, Context ctx) throws Throwable {
    Validate.notNull(action);//from w ww .  ja v a2  s  . c o  m

    T result = null;
    EntityManager em = null;
    try {
        // create entity manager, add it to args, and begin transaction before the call.
        Logger.info(LoggedAction.getLogEntry(ctx, "creating entity manager"));
        em = createEntityManager();
        em.getTransaction().begin();

        // call the actual action.
        result = action.run(em);

        // commit active transaction after the call.
        if (em.isOpen() && em.getTransaction().isActive()) {
            em.getTransaction().commit();
        }
    } catch (Throwable e) {
        // rollback on error.
        if (em.isOpen() && em.getTransaction().isActive()) {
            Logger.info(LoggedAction.getLogEntry(ctx, "rolling back transaction"));
            em.getTransaction().rollback();
        }

        // rethrow.
        throw e;
    } finally {
        // close entity manager.
        if (em != null && em.isOpen()) {
            em.close();
        }
    }

    return result;
}

From source file:com.enioka.jqm.tools.ResourceParser.java

private static JndiResourceDescriptor fromDatabase(String alias) throws NamingException {
    JndiObjectResource resource = null;//from www  .  ja v a2  s . c  o m
    EntityManager em = null;
    try {
        // Using the horrible CriteriaBuilder API instead of a string query. This avoids classloading issues - Hibernate binds
        // the entities at run time with the thread current classloader...
        em = Helpers.getNewEm();

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<JndiObjectResource> q = cb.createQuery(JndiObjectResource.class);
        Root<JndiObjectResource> c = q.from(JndiObjectResource.class);
        ParameterExpression<String> p = cb.parameter(String.class);
        q.select(c).where(cb.equal(c.get("name"), p));

        TypedQuery<JndiObjectResource> query = em.createQuery(q);
        query.setParameter(p, alias);
        resource = query.getSingleResult();
    } catch (Exception e) {
        NamingException ex = new NamingException("Could not find a JNDI object resource of name " + alias);
        ex.setRootCause(e);
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }

    // Create the ResourceDescriptor from the JPA object
    JndiResourceDescriptor d = new JndiResourceDescriptor(resource.getType(), resource.getDescription(), null,
            resource.getAuth(), resource.getFactory(), resource.getSingleton());
    for (JndiObjectResourceParameter prm : resource.getParameters()) {
        d.add(new StringRefAddr(prm.getKey(), prm.getValue()));
    }

    return d;
}

From source file:vn.edu.vnuk.tasks_jpa.dao.TaskDao.java

public void update(Task task) throws SQLException {

    EntityManager manager = getEntityManager();
    manager.getTransaction().begin();/* w  ww .ja  v  a 2  s.  com*/
    manager.merge(task);
    manager.getTransaction().commit();
    manager.close();

}

From source file:vn.edu.vnuk.tasks_jpa.dao.TaskDao.java

public void create(Task task) throws SQLException {

    EntityManager manager = getEntityManager();
    manager.getTransaction().begin();/*from  w  w  w .  j  a  v a 2  s . co  m*/
    manager.persist(task);
    manager.getTransaction().commit();
    manager.close();

}

From source file:com.headissue.pigeon.service.AdminSurveyServiceTest.java

@Test
public void testChangeSurveyTitle() throws Exception {
    SurveyValue sv = getSurveyValue("changeSurveyTitle.json");
    EntityManager _manager = mergeSurvey(sv);
    _manager.close();
    assertEquals("Blabla Umfrage", survey.getName());
}

From source file:com.headissue.pigeon.service.AdminSurveyServiceTest.java

@Test
public void testAddQuestion() throws Exception {
    assertEquals(2, survey.getQuestions().size());
    SurveyValue sv = getSurveyValue("addQuestion.json");
    EntityManager _manager = mergeSurvey(sv);
    _manager.close();
    assertEquals(3, survey.getQuestions().size());
}

From source file:com.headissue.pigeon.service.AdminSurveyServiceTest.java

@Test
public void testLessQuestion() throws Exception {
    assertEquals(2, survey.getQuestions().size());
    SurveyValue sv = getSurveyValue("lessQuestion.json");
    EntityManager _manager = mergeSurvey(sv);
    _manager.close();
    assertEquals(1, survey.getQuestions().size());
}

From source file:cz.fi.muni.pa165.daoImpl.TroopDAOImpl.java

@Override
public void updateTroop(Troop troop) throws IllegalArgumentException {
    if (troop == null || troop.getName() == null || troop.getMission() == null
            || troop.getAmountOfMoney() == null) {
        throw new IllegalArgumentException("Update troop called with wrong param");
    }/*from  w  w  w .  j  a v a2s  .  c o m*/
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.merge(troop);
    em.getTransaction().commit();
    em.close();
}