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:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para a remoo de usurio.
 * @author Richel Sensineli//from w ww.j av  a  2 s.  c o m
 * @param id int - ID do usurio
 * @throws UsuarioNaoEncontradoException - usurio no encontrado
 */
@Override
public void removeUsuario(final int id) throws UsuarioNaoEncontradoException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();

    Usuario u = em.find(UsuarioImpl.class, id);

    em.getTransaction().begin();
    if (u == null) {
        throw new UsuarioNaoEncontradoException("usuario no encontrado");
    } else {
        em.remove(u);
        em.getTransaction().commit();
    }
    em.clear();
    em.close();
    emf.close();

}

From source file:eu.optimis.trustedinstance.DBStorage.java

@SuppressWarnings("finally")
public boolean update(DBStorageEntry entry) {

    boolean result = true;
    EntityManager em = emf.createEntityManager();

    try {//w w w  .  j av a 2 s . com
        em.getTransaction().begin();
        DBStorageEntry update = em.find(DBStorageEntry.class, entry.getKey());
        update.setLicenseToken(entry.getLicenseToken());
        em.getTransaction().commit();
    } catch (Exception e) {
        result = false;
    } finally {
        em.close();
        return result;
    }
}

From source file:hd.controller.AddImageToProjectServlet.java

public void persist(Object object) {
    EntityManager em = emf.createEntityManager();
    try {/*from w w w. j  a  v a2s. co m*/
        em.getTransaction().begin();
        em.persist(object);
        em.getTransaction().commit();
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
        em.getTransaction().rollback();
    } finally {
        em.close();
    }
}

From source file:com.headissue.pigeon.survey.answer.AnswerSurveyTest.java

<T> T find(Class<T> type, Object key) {
    EntityManager manager = factory.createEntityManager();
    try {// w w w. ja  v  a 2 s. c  o m
        return manager.find(type, key);
    } finally {
        manager.close();
    }
}

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

@Test
public void testDelete() {
    EntityManager em = emf.createEntityManager();
    BookDAOImpl bdao = new BookDAOImpl();
    bdao.setManager(em);//from w  ww. j ava  2s  . c  o  m
    Book b = new Book();
    b.setIdBook(1);
    em.getTransaction().begin();
    b = bdao.find(b);
    bdao.delete(b);
    em.getTransaction().commit();

    List<Book> books = em.createQuery("SELECT b FROM Book b", Book.class).getResultList();
    em.close();
    assertEquals(books.size(), 0);
}

From source file:com.headissue.pigeon.admin.AdminSurveyHandler.java

public List<SurveyOverviewValue> getAllSurveys() {
    EntityManager _manager = factory.createEntityManager();
    try {//from  w  w w.  j ava  2 s.c om
        TypedQuery<SurveyOverviewValue> q = _manager.createNamedQuery("survey.allSurveyOverview",
                SurveyOverviewValue.class);
        q.setParameter("status", SurveyStatus.ENABLED);
        return JPAUtils.getResultList(q);
    } finally {
        _manager.close();
    }
}

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

@Test
public void testCreateTroop() {
    EntityManager entityManager = emf.createEntityManager();
    entityManager.getTransaction().begin();
    Troop troopC = new Troop();
    troopC.setName("Testers");
    troopC.setMoney(new BigDecimal("30"));
    troopC.setMission("mise C");
    entityManager.getTransaction().commit();
    entityManager.close();

    troopDao.createTroop(troopC);/*from   ww  w.j a  v a 2  s . c  o  m*/

    Troop t1 = troopDao.findTroopById(troopC.getId());
    Assert.assertEquals(t1.getName(), "Testers");
    Assert.assertEquals(t1.getMoney().compareTo(new BigDecimal("30")), 0);
    Assert.assertEquals(t1.getMission(), "mise C");

}

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

@Test
public void testUpdateGoldForTroop() {
    EntityManager entityManager = emf.createEntityManager();
    entityManager.getTransaction().begin();
    Troop troopC = new Troop();
    troopC.setName("Testers");
    troopC.setMoney(new BigDecimal("30"));
    entityManager.persist(troopC);//from w  w w .j a va 2 s .  co  m
    entityManager.getTransaction().commit();
    entityManager.close();

    troopDao.updateGoldForTroop(troopC.getId(), new BigDecimal("5"));

    Troop t1 = troopDao.findTroopById(troopC.getId());
    Assert.assertEquals(t1.getMoney().compareTo(new BigDecimal("5")), 0);

}

From source file:it.infn.ct.futuregateway.apiserver.resources.observers.TaskObserver.java

@Override
public final void update(final Observable obs, final Object arg) {
    if (!(obs instanceof Task)) {
        log.error("Wrong abject associated with the oserver");
    }//from   w  ww  .  java  2s  .  co  m
    Task t = (Task) obs;
    if (t.getId() == null || t.getStatus() == null) {
        return;
    }
    log.debug("Task " + t.getId() + " updated");
    if (t.getStatus().equals(Task.STATUS.WAITING) && t.getApplicationDetail() != null) {
        if (t.getInputFiles() != null) {
            for (TaskFile tf : t.getInputFiles()) {
                if (tf.getStatus().equals(TaskFile.FILESTATUS.NEEDED)) {
                    return;
                }
            }
        }
        t.setStatus(Task.STATUS.READY);
        submit(t);
    }
    EntityManager em = emf.createEntityManager();
    EntityTransaction et = em.getTransaction();
    try {
        et.begin();
        em.merge(t);
        et.commit();
    } catch (RuntimeException re) {
        log.error("Impossible to update the task!");
        log.error(re);
        if (et != null && et.isActive()) {
            et.rollback();
        }
    } finally {
        em.close();
    }
}

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

@Test
public void testAssignMissionToTroop() {
    EntityManager entityManager = emf.createEntityManager();
    entityManager.getTransaction().begin();
    Troop troopC = new Troop();
    troopC.setName("Testers");
    troopC.setMoney(new BigDecimal("30"));
    entityManager.persist(troopC);/*  w ww .  ja v a2  s  . c o m*/
    entityManager.getTransaction().commit();
    entityManager.close();

    troopDao.assignMissionToTroop(troopC.getId(), "mise C");

    Troop t1 = troopDao.findTroopById(troopC.getId());
    Assert.assertEquals(t1.getMission(), "mise C");

    troopDao.assignMissionToTroop(troopC.getId(), null);

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

}