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.epam.training.taranovski.web.project.repository.implementation.EmployeeRepositoryImplementation.java

@Override
public List<Employee> getAll() {
    EntityManager em = entityManagerFactory.createEntityManager();
    List<Employee> list = new LinkedList<>();
    try {/*from   w w w.  j a v a  2 s  . com*/
        em.getTransaction().begin();

        TypedQuery<Employee> query = em.createNamedQuery("Employee.findAll", Employee.class);
        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:op.care.info.DlgDiag.java

private void reloadTable() {
    if (txtSuche.getText().isEmpty()) {
        lstDiag.setModel(new DefaultListModel());
        return;//from   w  w w  . j a v  a2 s.com
    }

    EntityManager em = OPDE.createEM();
    Query query = em.createQuery(
            "SELECT i FROM ICD i WHERE i.icd10 LIKE :icd10 OR i.text like :text ORDER BY i.icd10 ");

    String suche = "%" + txtSuche.getText() + "%";

    query.setParameter("icd10", suche);
    query.setParameter("text", suche);

    lstDiag.setModel(SYSTools.list2dlm(query.getResultList()));
    em.close();

    lstDiag.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

}

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

/**
 * Deletes the recipe./*from   ww  w.  j a  v  a2  s.  c  o m*/
 * @param _groupId The id of the group still containing the recipe.
 * @param _recipeUUID the uuid of the recipe to delete.
 */
@DELETE
@TokenSecured
@Path("{recipeuuid}")
@Produces({ "application/json" })
public Response deleteRecipe(@PathParam("groupid") int _groupId, @PathParam("recipeuuid") String _recipeUUID)
        throws Exception {
    UUID toDelete;
    try {
        toDelete = UUID.fromString(_recipeUUID);
    } catch (IllegalArgumentException _e) {
        return ResponseFactory.generateBadRequest(CommonEntity.INVALID_UUID);
    }

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

    return ResponseFactory.generateOK(null);
}

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

@Override
public boolean update(OfferBid something) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = false;

    try {//from w  w  w  .  j  ava 2 s .  co m
        em.getTransaction().begin();
        em.merge(something);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return success;
}

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

public int overwriteSurvey(SurveyParameter p) {
    checkNotNull(p, "parameter is null");
    checkNotNull(p.getSurvey(), "survey values are null");
    EntityManager _manager = factory.createEntityManager();
    try {/*from www.  j  a  v  a2  s  .  co m*/
        _manager.getTransaction().begin();
        Survey _survey = _manager.find(Survey.class, p.getSurveyId());
        adminSurveyService.mergeSurvey(_survey, p.getSurvey(), new Date(p.getTimestamp()), _manager);
        _manager.getTransaction().commit();
        return _survey.getId();
    } catch (Exception e) {
        LogUtils.warn(log, e, "save the survey failed");
        if (_manager.getTransaction().isActive()) {
            _manager.getTransaction().rollback();
        }
        return 0;
    } finally {
        _manager.close();
    }
}

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

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

        TypedQuery<Employee> query = em.createNamedQuery("Employee.findAllFreeEmployees", Employee.class);
        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:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public boolean create(OfferBid something) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = false;

    try {//from  www.j av  a 2s.  c o  m
        em.getTransaction().begin();
        em.persist(something);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return success;
}

From source file:facades.PersonFacadeDB.java

@Override
public String getPerson(Integer id) throws NotFoundException {
    String result = "";
    //get person with this id from DB
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();//from www .j  a va 2s .c o m
    try {
        Person p = em.find(Person.class, id);

        result = om.writeValueAsString(p);
        //            Query query = em.createNamedQuery("Person.findById").setParameter("id", id);
        //            List<Person> people = query.getResultList();

        //result = gson.toJson(people.get(0));
        //result = om.writeValueAsString(people.get(0));
    } catch (Exception e) {
        throw new NotFoundException("No person exists for the given id");
    } finally {
        em.close();
    }
    return result;
}

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

@Test
public void testFindAllPrintedBooksByLoan() {
    EntityManager em = emf.createEntityManager();
    PrintedBookDAOImpl pbDAO = new PrintedBookDAOImpl();
    pbDAO.setManager(em);//from   www .j a va 2 s  .  c  o m

    Book b = new Book();
    b.setIdBook(1);

    PrintedBook book = pbDAO.findPrintedBookById(1);
    book.setIdPrintedBook(1);
    HashSet<PrintedBook> set = new HashSet<PrintedBook>();

    Loan l = new Loan();
    l.setIdLoan(1);
    l.setPrintedBook(book);
    book.setLoan(l);
    book.setState(Boolean.TRUE);
    pbDAO.update(book);

    List<PrintedBook> li = pbDAO.findPrintedBooksByLoan(b, l);
    em.close();
    assertEquals(1, li.size());
}

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

JobManagerHandler(JobInstance ji, Map<String, String> prms) {
    p = new Properties();
    p.put("emf", Helpers.getEmf());

    EntityManager em = Helpers.getNewEm();
    this.ji = em.find(JobInstance.class, ji.getId());
    params = prms;/*from  ww w. jav a 2 s. co  m*/

    defaultCon = em.createQuery("SELECT gp.value FROM GlobalParameter gp WHERE gp.key = 'defaultConnection'",
            String.class).getSingleResult();

    this.jd = this.ji.getJd();
    this.application = this.jd.getApplication();
    this.sessionId = this.ji.getSessionID();
    this.node = this.ji.getNode();
    this.node.getDlRepo();
    em.close();
}