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:facades.PersonFacadeDB.java

@Override
public RoleSchool addRole(String json, Integer id) throws NotFoundException {
    Person p = gson.fromJson(getPerson(id), Person.class);
    HashMap<String, String> map = new Gson().fromJson(json, new TypeToken<HashMap<String, String>>() {
    }.getType());/*  ww w .  j  a va  2  s .c  om*/

    String roleName = map.get("roleName");
    RoleSchool role;

    switch (roleName) {
    case "Teacher Assistant":
        //Create role
        RoleSchool ta = new TeacherAssistant();
        ta.setPerson(p);
        role = ta;
        break;
    case "Teacher":

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        String dateInString = map.get("date");
        Date date = new Date();

        try {
            date = formatter.parse(dateInString);
        } catch (ParseException e) {
            e.printStackTrace(System.out);
        }

        RoleSchool t = new Teacher(date, map.get("degree"));
        t.setPerson(p);
        role = t;
        break;
    case "Student":
        RoleSchool s = new Student(map.get("semester"));
        s.setPerson(p);
        role = s;
        break;
    default:
        throw new IllegalArgumentException("no such role");
    }
    //save this info
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();

    try {
        em.persist(role);
        transaction.commit();
        em.getEntityManagerFactory().getCache().evictAll();
    } catch (Exception e) {
        throw new NotFoundException("Couldnt add role");
    } finally {
        em.close();
    }

    return role;

}

From source file:in.bookmylab.jpa.JpaDAO.java

/**
 * @return/*  w  ww  . ja  v a  2  s. c  om*/
 */
public List<ResourcePricing> getResourcePrices() {
    EntityManager em = emf.createEntityManager();
    try {
        Query q = em.createNamedQuery("ResourcePricing.findAllCurrent");
        return q.getResultList();
    } finally {
        em.close();
    }
}

From source file:cz.fi.muni.pa165.dto.BookDAOTest.java

@Test
public void testUpdate() {
    EntityManager em = emf.createEntityManager();
    BookDAOImpl bdao = new BookDAOImpl();
    bdao.setManager(em);//from w w w  .  j  ava 2s.  com
    Book book = new Book();
    book = em.createQuery("SELECT b FROM Book b", Book.class).getSingleResult();
    em.getTransaction().begin();
    book.setName("Updated!");
    bdao.update(book);
    em.getTransaction().commit();

    Book book2 = em.createQuery("SELECT b FROM Book b", Book.class).getSingleResult();
    em.close();
    assertEquals(book2.getName(), "Updated!");

}

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

@Override
public BasicSkill getById(int id) {
    EntityManager em = entityManagerFactory.createEntityManager();
    BasicSkill skill = null;/*from  w  w  w .ja va2 s . co  m*/
    try {
        em.getTransaction().begin();
        skill = em.find(BasicSkill.class, id);
        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return skill;
}

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

@Override
public boolean delete(UserSkill userSkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    UserSkill managedUserSkill = null;//  w  ww .  j a  va 2s.  com
    try {
        em.getTransaction().begin();
        managedUserSkill = em.merge(userSkill);
        em.remove(managedUserSkill);
        em.getTransaction().commit();

        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.test.CommandITCase.java

/**
 * Count articles.//from ww  w. j  a  v a 2  s .c  o m
 *
 * @return the long
 * @throws Exception the exception
 */
private long countArticles() throws Exception {
    EntityManager em = emf.createEntityManager();
    Query query = em.createQuery("SELECT COUNT(p.headline) FROM news p");
    Long countResult = (Long) query.getSingleResult();

    em.close();

    return countResult;
}

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

private JobInstance getRunningJI(int jobId) {
    EntityManager em = Helpers.getNewEm();
    JobInstance jj = null;//from  w  w w .ja  v  a2  s. c  om
    try {
        jj = em.find(JobInstance.class, jobId);
    } catch (Exception e) {
        throw new JqmRuntimeException("Could not query job instance", e);
    } finally {
        em.close();
    }
    return jj;
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.test.CommandITCase.java

/**
 * Count categories.//  w ww  .  ja  va  2s . c  o  m
 *
 * @return the long
 * @throws Exception the exception
 */
private long countCategories() throws Exception {
    EntityManager em = emf.createEntityManager();
    Query query = em.createQuery("SELECT COUNT(p.fs_id) FROM category p");
    Long countResult = (Long) query.getSingleResult();

    em.close();

    return countResult;
}

From source file:de.zib.gndms.GORFX.context.service.globus.resource.ExtTaskResourceHome.java

private void resumeTasks() throws Exception {

    logger.debug("Checking for aborted tasks.");
    EntityManager em = null;
    List<String> rs = null;
    try {/*w  w  w .ja v a  2s .  com*/
        em = system.getEntityManagerFactory().createEntityManager();
        Query q = em.createNamedQuery("unfinishedTaskIds");
        rs = q.getResultList();
        if (rs.size() == 0) {
            logger.debug("No tasks found :-)");
            return;
        }
    } finally {
        if (em != null && em.isOpen())
            em.close();
    }

    logger.debug("Try to resume " + rs.size() + " tasks");
    for (String id : rs) {
        logger.debug("Resuming " + id);
        Resource k = find(getKeyForId(id));
    }
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.test.CommandITCase.java

/**
 * Count meta categories.//w w  w  . j  a  v  a2s  .co m
 *
 * @return the long
 * @throws Exception the exception
 */
private long countMetaCategories() throws Exception {
    EntityManager em = emf.createEntityManager();
    Query query = em.createQuery("SELECT COUNT(p.fs_id) FROM metaCategory p");
    Long countResult = (Long) query.getSingleResult();

    em.close();

    return countResult;
}