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:com.nokia.helium.metadata.tests.TestORMFMPPLoader.java

/**
 * Populates the LogFile table with basic data.
 * @throws MetadataException//from  w  w w  .  jav  a2 s  .co m
 * @throws IOException
 */
@Before
public void populateDatabase() throws MetadataException, IOException {
    File tempdir = new File(System.getProperty("test.temp.dir"));
    tempdir.mkdirs();
    database = new File(tempdir, "test_db");
    if (database.exists()) {
        FileUtils.forceDelete(database);
    }
    EntityManagerFactory factory = FactoryManager.getFactoryManager().getEntityManagerFactory(database);
    EntityManager em = factory.createEntityManager();
    try {
        em.getTransaction().begin();
        for (int i = 0; i < 2000; i++) {
            LogFile lf = new LogFile();
            lf.setPath("log" + String.format("%04d", i));
            em.persist(lf);
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().commit();
        }
        em.close();
        factory.close();
    }
}

From source file:cz.muni.fi.dndtroops.test.TroopDaoImplTest.java

@Test
public void testDeleteTroop() {
    EntityManager entityManager = emf.createEntityManager();
    entityManager.getTransaction().begin();
    Troop troopC = new Troop();
    troopC.setName("Testers");
    troopC.setMoney(new BigDecimal("30"));
    troopC.setMission("mise C");
    entityManager.persist(troopC);/* w ww.  j  a v a2 s  . c  om*/
    entityManager.getTransaction().commit();
    entityManager.close();

    Troop t1 = troopDao.findTroopById(troopC.getId());
    troopDao.deleteTroop(t1);

    Troop t2 = troopDao.findTroopById(troopC.getId());
    Assert.assertEquals(t2, null);

}

From source file:com.github.jrh3k5.membership.renewal.mailer.service.jpa.JpaMembershipService.java

@Override
public JpaMembershipRecord addMembershipRecord(String givenName, String familyName, Date membershipDate) {
    // Does the record already exist? If so, update it
    // Do it within an entity so that the dates collection can be modified while attached
    final EntityManager entityManager = entityManagerFactory.createEntityManager();
    try {/*from  ww w.ja va2 s . c  om*/
        final JpaMembershipRecord byName = getByName(entityManager, givenName, familyName);
        if (byName != null) {
            byName.addMembershipDate(membershipDate);
            updateMembership(entityManager, byName);
            return byName;
        }
    } finally {
        entityManager.close();
    }

    final JpaMembershipRecord newRecord = new JpaMembershipRecord();
    newRecord.setFamilyName(familyName);
    newRecord.setGivenName(givenName);
    newRecord.addMembershipDate(membershipDate);
    persistEntity(newRecord);
    return newRecord;
}

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para a criao de usurio
 * @author Richel Sensineli\/*w  ww  .  j av a 2s  .  c  o m*/
 * @param nome String - Nome do usurio
 * @param sobrenome String - Nome do usurio
 * @return Usuario usuario - Objeto Usurio
 */
@Override
public Usuario criaUsuario(final String nome, final String sobrenome) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();

    UsuarioImpl user = new UsuarioImpl();
    user.setNome(nome);
    user.setSobrenome(sobrenome);

    em.getTransaction().begin();
    try {
        em.persist(user);
        em.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        em.getTransaction().rollback();
    }

    em.clear();
    em.close();
    emf.close();

    return user;
}

From source file:org.SRV.testServlet.java

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request//ww w  . j a  v  a  2s .  co  m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    entityManagerFactory = Persistence.createEntityManagerFactory("org.SRV_war_1.0.0-BUILD-SNAPSHOTPU");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    //        List<Essence> essence = entityManager.createQuery("select * from essence").getResultList();        
    //Query query = entityManager.createQuery("FROM Essence u WHERE  u.id = :id").setParameter("id", 1);
    //List<Essence> resultList = query.getResultList();
    List<Essence> resultList = entityManager.createQuery("SELECT e FROM Essence e").getResultList();
    entityManager.close();
    entityManagerFactory.close();
    String json = new Gson().toJson(resultList);
    response.getWriter().write(json);
}

From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java

/**
 * //from ww  w .  ja v  a  2 s  .  c  o m
 * @return 
 */
public List<StatusTrail> getAllIncompleteTasks() {
    EntityManager em = getEntityManager();
    try {
        return em
                .createQuery(
                        "SELECT st FROM StatusTrail st WHERE  "
                                + " st.status = 'not_done' AND st.givenUp = 0 ORDER BY st.id DESC",
                        StatusTrail.class)
                .getResultList();
    } finally {
        em.close();
    }
}

From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java

/**
 * Tasks whose status:not_done and givenUp:zero
 * @param ds//from ww w .ja va2  s  .c om
 * @return 
 */
public List<StatusTrail> getIncompleteTasks(DataSource ds) {
    EntityManager em = getEntityManager();
    try {
        return em.createQuery(
                "SELECT st FROM StatusTrail st WHERE st.dataSourceId = :dataSourceId "
                        + "AND st.status = 'not_done' AND st.givenUp = 0 ORDER BY st.id DESC",
                StatusTrail.class).setParameter("dataSourceId", ds.getId()).getResultList();
    } finally {
        em.close();
    }
}

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para a atualizao do usurio
 * @author Richel Sensineli/*from   w w  w. ja  v a 2 s.c  om*/
 * @param id int - ID do usurio
 * @param nome String - Nome do usurio
 * @param sobrenome String - Nome do usurio
 */
@Override
public void updateUsuario(final int id, final String nome, final String sobrenome)
        throws UsuarioNaoEncontradoException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();

    UsuarioImpl user = em.find(UsuarioImpl.class, id);
    user.setNome(nome);
    user.setSobrenome(sobrenome);

    em.getTransaction().begin();
    try {
        em.merge(user);
        em.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        em.getTransaction().rollback();
    }

    em.clear();
    em.close();
    emf.close();
}

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

/**
 * @return/*from  w ww . jav  a 2s .c o m*/
 */
public List<ResourceType> getResourceTypes() {
    EntityManager em = emf.createEntityManager();
    try {
        Query q = em.createNamedQuery("ResourceType.findAllActive");
        return q.getResultList();
    } finally {
        em.close();
    }
}

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

private History getEndedJI(int jobId) {
    EntityManager em = Helpers.getNewEm();
    History h = null;/*from  ww  w  . j  ava  2s.c  o m*/
    try {
        h = em.find(History.class, jobId);
    } catch (Exception e) {
        throw new JqmRuntimeException("Could not query history", e);
    } finally {
        em.close();
    }
    return h;
}