List of usage examples for javax.persistence EntityManager close
public void close();
From source file:br.nom.abdon.gastoso.rest.server.MaisRs.java
@GET @Path("/saldos") public Response saldos(final @Context Request request, final @Context HttpHeaders httpHeaders, final @QueryParam("dia") LocalDate dia) { final EntityManager entityManager = emf.createEntityManager(); Response response;//from w w w.j a v a 2 s .c om if (dia == null) throw new WebApplicationException(Response.Status.BAD_REQUEST); try { final FiltroSaldos filtroSaldo = new FiltroSaldos(); filtroSaldo.setDia(dia); filtroSaldo.addOrdem(POR_CONTA); final List<Saldo> saldos = aggregateDao.list(entityManager, filtroSaldo); response = buildResponse(request, httpHeaders, new GenericEntity<List<Saldo>>(saldos) { }); } catch (DalException ex) { response = dealWith(ex); } finally { entityManager.close(); } return response; }
From source file:it.infn.ct.futuregateway.apiserver.v1.TaskCollectionService.java
/** * Retrieve a task list for the user.//w w w .j ava 2s . c o m * Tasks are retrieved from the storage for the user performing the request. * * @return A list of tasks */ private List<Task> retrieveTaskList() { List<Task> lstTasks = new LinkedList<>(); EntityManager em = getEntityManager(); EntityTransaction et = null; List<Object[]> taskList = null; try { et = em.getTransaction(); et.begin(); taskList = em.createNamedQuery("tasks.userAll").setParameter("user", getUser()).getResultList(); et.commit(); } catch (RuntimeException re) { if (et != null && et.isActive()) { et.rollback(); } log.error("Impossible to retrieve the task list"); log.error(re); throw new RuntimeException("Impossible to access the task list"); } finally { em.close(); } if (taskList != null && !taskList.isEmpty()) { for (Object[] elem : taskList) { int idElem = 0; Task tmpTask = new Task(); tmpTask.setId((String) elem[idElem++]); tmpTask.setDescription((String) elem[idElem++]); tmpTask.setStatus((Task.STATUS) elem[idElem++]); tmpTask.setDateCreated((Date) elem[idElem]); lstTasks.add(tmpTask); } } return lstTasks; }
From source file:com.epam.training.taranovski.web.project.repository.implementation.EmployerRepositoryImplementation.java
@Override public List<Vacancy> getActiveVacancys(Employer employer) { EntityManager em = entityManagerFactory.createEntityManager(); List<Vacancy> list = new LinkedList<>(); try {/* w ww . j av a 2 s. c om*/ em.getTransaction().begin(); TypedQuery<Vacancy> query = em.createNamedQuery("Vacancy.findActiveByEmployer", Vacancy.class); query.setParameter("employer", employer); list = query.getResultList(); em.getTransaction().commit(); } catch (RuntimeException e) { Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return list; }
From source file:org.noorganization.instalist.server.api.CategoriesResource.java
/** * Creates the category.// ww w .j a v a2 s . c o m * @param _groupId The group to add the category to. * @param _entity Information for the new category. * e.g. examples/category.example */ @POST @TokenSecured @Consumes("application/json") @Produces({ "application/json" }) public Response postCategory(@PathParam("groupid") int _groupId, CategoryInfo _entity) throws Exception { if (_entity.getUUID() == null || _entity.getName() == null || _entity.getName().length() == 0 || (_entity.getDeleted() != null && _entity.getDeleted())) return ResponseFactory.generateBadRequest(CommonEntity.INVALID_DATA); Instant lastChanged; if (_entity.getLastChanged() != null) { lastChanged = _entity.getLastChanged().toInstant(); if (lastChanged.isAfter(Instant.now())) return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE); } else lastChanged = Instant.now(); UUID newCatUUID; try { newCatUUID = UUID.fromString(_entity.getUUID()); } catch (IllegalArgumentException e) { return ResponseFactory.generateBadRequest(CommonEntity.INVALID_UUID); } EntityManager manager = DatabaseHelper.getInstance().getManager(); ICategoryController categoryController = ControllerFactory.getCategoryController(manager); try { categoryController.add(_groupId, newCatUUID, _entity.getName(), lastChanged); } catch (ConflictException _e) { return ResponseFactory.generateConflict( new Error().withMessage("The new category " + "stands in conflict with existing one.")); } finally { manager.close(); } return ResponseFactory.generateCreated(null); }
From source file:com.epam.training.taranovski.web.project.repository.implementation.BasicSkillRepositoryImplementation.java
@Override public List<BasicSkill> getSkillsNotInVacancy(Vacancy vacancy) { EntityManager em = entityManagerFactory.createEntityManager(); List<BasicSkill> list = new LinkedList<>(); try {/*from ww w .j a va 2 s . co m*/ em.getTransaction().begin(); TypedQuery<BasicSkill> query = em.createNamedQuery("BasicSkill.findSkillsNotInVacancy", BasicSkill.class); query.setParameter("vacancy", vacancy); list = query.getResultList(); em.getTransaction().commit(); } catch (RuntimeException e) { Logger.getLogger(BasicSkillRepositoryImplementation.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.BasicSkillRepositoryImplementation.java
@Override public List<BasicSkill> getSkillsNotInEmployee(Employee employee) { EntityManager em = entityManagerFactory.createEntityManager(); List<BasicSkill> list = new LinkedList<>(); try {//from w w w. j a va2 s . c om em.getTransaction().begin(); TypedQuery<BasicSkill> query = em.createNamedQuery("BasicSkill.findSkillsNotInEmployee", BasicSkill.class); query.setParameter("employee", employee); list = query.getResultList(); em.getTransaction().commit(); } catch (RuntimeException e) { Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return list; }
From source file:de.zib.gndms.GORFX.context.service.globus.resource.TaskResource.java
@Override public void remove() { if (taskAction != null) { Log log = taskAction.getLog();//from w ww . j a v a 2 s .com log.debug("Removing task resource: " + getID()); AbstractTask tsk = taskAction.getModel(); boolean cleanUp = false; if (tsk != null) { if (!tsk.isDone()) { // task is still running cancel it and cleanup entity manager taskAction.setCancelled(true); log.debug("cancel task " + tsk.getWid()); cleanUp = true; if (future != null) { future.cancel(true); try { // give cancel some time Thread.sleep(2000L); } catch (InterruptedException e) { logger.debug(e); } } try { EntityManager em = taskAction.getEntityManager(); if (em != null && em.isOpen()) { try { EntityTransaction tx = em.getTransaction(); if (tx.isActive()) tx.rollback(); } finally { em.close(); } } } catch (Exception e) { // don't bother with exceptions log.debug("Exception on task future cancel: " + e.toString(), e); } } EntityManager em = home.getEntityManagerFactory().createEntityManager(); TxFrame tx = new TxFrame(em); // cleanup if necessary try { try { Task t = em.find(Task.class, tsk.getId()); t.setPostMortem(true); tx.commit(); if (cleanUp) { log.debug("Triggering task cleanup"); try { taskAction.setOwnEntityManager(em); taskAction.cleanUpOnFail(t); } catch (Exception e) { log.debug("Exception on cleanup: " + e.toString()); } } // remove task from db log.debug("Removing task: " + t.getId()); tx.begin(); em.remove(t); tx.commit(); } finally { tx.finish(); if (em.isOpen()) em.close(); } } catch (Exception e) { log.debug("Exception on task resource removal: " + e.toString()); e.printStackTrace(); } } } }
From source file:ec.edu.chyc.manejopersonal.controller.PersonaJpaController.java
public List<Persona> listPersonasConContrato() throws Exception { EntityManager em = null; try {//from w w w.j a va2 s .co m em = getEntityManager(); Query q = em.createQuery("select p from Persona p where p.contratosCollection is not empty"); List<Persona> list = q.getResultList(); return list; } finally { if (em != null) { em.close(); } } }
From source file:com.epam.training.taranovski.web.project.repository.implementation.UserRepositoryImplementation.java
@Override public boolean nameExistsInDB(String name) { EntityManager em = entityManagerFactory.createEntityManager(); boolean exists = false; try {/*from ww w . ja v a2 s . co m*/ em.getTransaction().begin(); TypedQuery<User> query = em.createNamedQuery("User.findByLogin", User.class); query.setParameter("login", name); query.getSingleResult(); em.getTransaction().commit(); exists = true; } catch (NoResultException ex) { Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(ex); exists = false; } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return exists; }
From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancyRepositoryImplementation.java
@Override public List<Vacancy> getAll() { EntityManager em = entityManagerFactory.createEntityManager(); List<Vacancy> list = new LinkedList<>(); try {/*from w ww . j a va 2 s . c o m*/ em.getTransaction().begin(); TypedQuery<Vacancy> query = em.createNamedQuery("Vacancy.findAll", Vacancy.class); 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; }