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:op.care.info.DlgDiag.java

private void fillCMBs() {
    EntityManager em = OPDE.createEM();
    Query queryArzt = em.createQuery("SELECT a FROM GP a WHERE a.status >= 0 ORDER BY a.name, a.vorname");
    java.util.List<GP> listAerzte = queryArzt.getResultList();
    listAerzte.add(0, null);/*w ww  .  j  a va 2 s.  c o  m*/

    Query queryKH = em.createQuery("SELECT k FROM Hospital k WHERE k.state >= 0 ORDER BY k.name");
    java.util.List<Hospital> listKH = queryKH.getResultList();
    listKH.add(0, null);
    em.close();

    cmbArzt.setModel(new DefaultComboBoxModel(listAerzte.toArray()));
    cmbArzt.setRenderer(GPTools.getRenderer());
    cmbArzt.setSelectedIndex(0);

    cmbKH.setModel(new DefaultComboBoxModel(listKH.toArray()));
    cmbKH.setRenderer(HospitalTools.getKHRenderer());
    cmbKH.setSelectedIndex(0);

    cmbSicherheit.setModel(new DefaultComboBoxModel(new String[] { SYSTools.xx("misc.msg.diag.security.na"),
            SYSTools.xx("misc.msg.diag.security.confirmed"), SYSTools.xx("misc.msg.diag.security.suspected"),
            SYSTools.xx("misc.msg.diag.security.rulingout"),
            SYSTools.xx("misc.msg.diag.security.conditionafter") }));
    cmbSicherheit.setSelectedIndex(1);

    cmbKoerper.setModel(new DefaultComboBoxModel(
            new String[] { SYSTools.xx("misc.msg.diag.side.na"), SYSTools.xx("misc.msg.diag.side.left"),
                    SYSTools.xx("misc.msg.diag.side.right"), SYSTools.xx("misc.msg.diag.side.both") }));
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.EmployeeRepositoryImplementation.java

@Override
public List<UserSkill> getSkills(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<UserSkill> list = new LinkedList<>();
    try {/*from  w w  w.j  a va  2  s .c  o m*/
        em.getTransaction().begin();

        TypedQuery<UserSkill> query = em.createNamedQuery("UserSkill.findByEmployee", UserSkill.class);
        query.setParameter("employee", employee);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(EmployeeRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

From source file:BO.UserHandler.java

private User getUserByEmail(String email) {
    User tempUser;/* w w  w.  j a va 2 s  . c o  m*/

    EntityManager em;
    EntityManagerFactory emf;

    emf = Persistence.createEntityManagerFactory(PERSISTENCE_NAME);
    em = emf.createEntityManager();

    try {
        tempUser = (User) em.createQuery("SELECT u FROM User u WHERE u.email LIKE :email")
                .setParameter("email", email).getSingleResult();
        return tempUser;
    } catch (NoResultException e) {
        throw (e);
    } catch (Exception e) {
        throw (e);
    } finally {
        if (em != null) {
            em.close();
        }
        if (emf != null) {
            emf.close();
        }
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.EmployeeRepositoryImplementation.java

@Override
public boolean clearSkills(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {//w w  w  .  j a v  a 2  s.  c o m
        em.getTransaction().begin();

        Query query = em.createNamedQuery("UserSkill.clearSkillsForEmployee");
        query.setParameter("employee", employee);

        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(EmployeeRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancyRepositoryImplementation.java

@Override
public List<VacancySkill> getSkills(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<VacancySkill> list = new LinkedList<>();
    try {//from  w w w.j  a v  a 2  s  . co  m
        em.getTransaction().begin();

        TypedQuery<VacancySkill> query = em.createNamedQuery("VacancySkill.findByVacancy", VacancySkill.class);
        query.setParameter("vacancy", vacancy);
        list = query.getResultList();

        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(VacancyRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return list;
}

From source file:org.noorganization.instalist.server.api.ListResource.java

/**
 * Deletes the list./*www  . j av a  2 s .  c  o m*/
 * @param _groupId The id of the group containing the list.
 * @param _listUUID The uuid of the list to update.
 */
@DELETE
@TokenSecured
@Path("{listuuid}")
@Produces({ "application/json" })
public Response deleteList(@PathParam("groupid") int _groupId, @PathParam("listuuid") String _listUUID)
        throws Exception {
    UUID listUUID;
    try {
        listUUID = UUID.fromString(_listUUID);
    } catch (IllegalArgumentException _e) {
        return ResponseFactory.generateBadRequest(CommonEntity.INVALID_UUID);
    }

    EntityManager manager = DatabaseHelper.getInstance().getManager();
    IListController listController = ControllerFactory.getListController(manager);
    try {
        listController.delete(_groupId, listUUID);
    } catch (NotFoundException _e) {
        return ResponseFactory
                .generateNotFound(new Error().withMessage("A list with this " + "uuid was not found."));
    } catch (GoneException _e) {
        return ResponseFactory
                .generateGone(new Error().withMessage("A list with this " + "uuid was already deleted."));
    } finally {
        manager.close();
    }

    return ResponseFactory.generateOK(null);
}

From source file:ec.edu.chyc.manejopersonal.controller.PersonaJpaController.java

public List<Persona> listTodasPersonas() throws Exception {
    EntityManager em = null;
    try {/*from  ww w  .  jav  a2 s  .co  m*/
        em = getEntityManager();
        Query q = em.createQuery(
                "select distinct p from Persona p left join fetch p.personaFirmasCollection where p.id<>1 order by p.activo desc, p.apellidos asc ");
        List<Persona> list = q.getResultList();
        return list;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

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

private void shouldKill() {
    // Throttle: only peek once every 1 second.
    if (lastPeek != null && Calendar.getInstance().getTimeInMillis() - lastPeek.getTimeInMillis() < 1000L) {
        return;/*from www.  j  a va 2  s. co m*/
    }

    EntityManager em = Helpers.getNewEm();
    try {
        this.ji = em.find(JobInstance.class, this.ji.getId());
        jqmlogger.trace("Analysis: should JI " + ji.getId() + " get killed? Status is " + ji.getState());
        if (ji.getState().equals(State.KILLED)) {
            jqmlogger.info("Job will be killed at the request of a user");
            Thread.currentThread().interrupt();
            throw new JqmKillException("This job" + "(ID: " + ji.getId() + ")" + " has been killed by a user");
        }
    } finally {
        em.close();
        lastPeek = Calendar.getInstance();
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancyRepositoryImplementation.java

@Override
public boolean clearSkills(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {//w  w  w . j  a  v  a  2 s  .  com
        em.getTransaction().begin();

        Query query = em.createNamedQuery("VacancySkill.clearSkillsForVacancy");
        query.setParameter("vacancy", vacancy);
        query.executeUpdate();

        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(VacancyRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:org.noorganization.instalist.server.api.TaggedProductResource.java

/**
 * Deletes the tagged product.//from w w  w . jav a  2s .co m
 * @param _groupId The id of the group still containing the tagged product.
 * @param _taggedProductUUID The uuid of the tagged product itself.
 */
@DELETE
@TokenSecured
@Path("{tpuuid}")
@Produces({ "application/json" })
public Response deleteTaggedProduct(@PathParam("groupid") int _groupId,
        @PathParam("tpuuid") String _taggedProductUUID) throws Exception {
    UUID toDelete;
    try {
        toDelete = UUID.fromString(_taggedProductUUID);
    } catch (IllegalArgumentException _e) {
        return ResponseFactory.generateBadRequest(CommonEntity.INVALID_UUID);
    }

    EntityManager manager = DatabaseHelper.getInstance().getManager();
    ITaggedProductController taggedProductController = ControllerFactory.getTaggedProductController(manager);
    try {
        taggedProductController.delete(_groupId, toDelete);
    } catch (NotFoundException _e) {
        return ResponseFactory
                .generateNotFound(new Error().withMessage("The tagged product " + "was not found."));
    } catch (GoneException _e) {
        return ResponseFactory
                .generateGone(new Error().withMessage("The tagged product has " + "been deleted."));
    } finally {
        manager.close();
    }

    return ResponseFactory.generateOK(null);
}