List of usage examples for javax.persistence EntityManager close
public void close();
From source file:org.noorganization.instalist.server.api.TagResource.java
/** * Get a single tag.//from ww w. jav a 2 s . c o m * @param _groupId The id of the group containing the tag. * @param _tagUUID The uuid of the requested tag. */ @GET @TokenSecured @Path("{taguuid}") @Produces({ "application/json" }) public Response getTag(@PathParam("groupid") int _groupId, @PathParam("taguuid") String _tagUUID) throws Exception { UUID toFind; try { toFind = UUID.fromString(_tagUUID); } catch (IllegalArgumentException _e) { return ResponseFactory.generateBadRequest(CommonEntity.INVALID_UUID); } EntityManager manager = DatabaseHelper.getInstance().getManager(); ITagController tagController = ControllerFactory.getTagController(manager); DeviceGroup group = manager.find(DeviceGroup.class, _groupId); Tag current = tagController.findByGroupAndUUID(group, toFind); if (current == null) { if (tagController.findDeletedByGroupAndUUID(group, toFind) == null) { manager.close(); return ResponseFactory.generateNotFound(new Error().withMessage("Tag was not " + "found.")); } manager.close(); return ResponseFactory.generateGone(new Error().withMessage("Tag was deleted " + "before.")); } manager.close(); TagInfo rtn = new TagInfo().withDeleted(false); rtn.setUUID(current.getUUID()); rtn.setName(current.getName()); rtn.setLastChanged(Date.from(current.getUpdated())); return ResponseFactory.generateOK(rtn); }
From source file:com.headissue.pigeon.admin.AdminSurveyHandler.java
public SurveyValue getSurvey(int _surveyId) { if (_surveyId <= 0) { return defaultSurvey; }/* ww w . jav a 2s. c o m*/ EntityManager _manager = factory.createEntityManager(); try { Survey _survey = _manager.find(Survey.class, _surveyId); if (_survey == null || _survey.getStatus() == SurveyStatus.DISABLED) { return defaultSurvey; } return adminSurveyService.fromSurvey(_survey); } catch (Exception e) { LogUtils.warn(log, e, "get the survey '%s' failed", _surveyId); return defaultSurvey; } finally { _manager.close(); } }
From source file:in.bookmylab.jpa.JpaDAO.java
/** * @param spmId // www . jav a2s.c o m * @return */ public SpmLabBooking getSpmByBookingId(Long bookingId) { EntityManager em = emf.createEntityManager(); SpmLabBooking spm = null; try { Query q = em.createNamedQuery("SpmLabBooking.findByBookingId"); spm = (SpmLabBooking) q.setParameter("bookingId", bookingId).getSingleResult(); } finally { em.close(); } return spm; }
From source file:in.bookmylab.jpa.JpaDAO.java
/** * @param xrdId /*w ww .j av a2 s. co m*/ * @return */ public XrdLabBooking getXrdByBookingId(Long bookingId) { EntityManager em = emf.createEntityManager(); XrdLabBooking xrd = null; try { Query q = em.createNamedQuery("XrdLabBooking.findByBookingId"); xrd = (XrdLabBooking) q.setParameter("bookingId", bookingId).getSingleResult(); } finally { em.close(); } return xrd; }
From source file:com.headissue.pigeon.admin.AdminSurveyHandler.java
public int createSurvey(SurveyParameter p) { checkNotNull(p, "parameter is null"); checkNotNull(p.getSurvey(), "survey values are null"); Survey _survey = adminSurveyService.createSurvey(p.getSurvey(), new Date(p.getTimestamp())); EntityManager _manager = factory.createEntityManager(); try {// w w w . ja v a2s . c o m _manager.getTransaction().begin(); _manager.persist(_survey); _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:entity.files.SYSFilesTools.java
public static void detachFiles(SYSFiles[] files, SYSFilesContainer sysFilesContainer) { FileTransferClient ftp = getFTPClient(); if (ftp != null) { EntityManager em = OPDE.createEM(); try {/*from w w w .j av a2s .c om*/ em.getTransaction().begin(); for (SYSFiles sysfile : files) { SYSFilesLink link = em.merge(sysFilesContainer.detachFile(sysfile)); em.remove(link); } em.getTransaction().commit(); } catch (OptimisticLockException ole) { OPDE.warn(ole); if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } if (ole.getMessage().indexOf("Class> entity.info.Resident") > -1) { OPDE.getMainframe().emptyFrame(); OPDE.getMainframe().afterLogin(); } OPDE.getDisplayManager().addSubMessage(DisplayManager.getLockMessage()); } catch (Exception ex) { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } OPDE.fatal(ex); } finally { em.close(); try { ftp.disconnect(); } catch (Exception e) { OPDE.error(e); } } } }
From source file:com.epam.training.taranovski.web.project.repository.implementation.EmployerRepositoryImplementation.java
@Override public boolean update(Employer employer) { EntityManager em = entityManagerFactory.createEntityManager(); boolean success = true; try {/* ww w . ja v a 2 s .c o m*/ em.getTransaction().begin(); em.merge(employer); em.flush(); em.getTransaction().commit(); success = true; } catch (RuntimeException e) { Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); success = false; } em.close(); } return success; }
From source file:facades.PersonFacadeDB.java
@Override public String getPersons() { String result = ""; EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName); EntityManager em = emf.createEntityManager(); EntityTransaction transaction = em.getTransaction(); transaction.begin();/*w ww. j ava2s. c o m*/ try { Query query = em.createNamedQuery("Person.findAll"); List<Person> people = query.getResultList(); try { result = om.writeValueAsString(people); } catch (JsonProcessingException ex) { Logger.getLogger(PersonFacadeDB.class.getName()).log(Level.SEVERE, null, ex); } } finally { em.close(); } return result; }
From source file:com.github.jrh3k5.membership.renewal.mailer.service.jpa.JpaEmailService.java
@Override public JpaEmailRecord getByName(String givenName, String familyName) { LOGGER.info("Looking up email info for {} {}", givenName, familyName); final EntityManager entityManager = entityManagerFactory.createEntityManager(); try {//from www .j a v a 2 s . c o m final Query query = entityManager.createQuery( "select e from EmailRecord e where e.givenName = :givenName and e.familyName = :familyName"); query.setParameter("givenName", givenName); query.setParameter("familyName", familyName); @SuppressWarnings("unchecked") final List<JpaEmailRecord> results = query.getResultList(); return results.size() == 1 ? results.get(0) : null; } finally { entityManager.close(); } }
From source file:org.noorganization.instalist.server.api.IngredientResource.java
/** * Get a list of ingredients./*from ww w.ja v a2 s. co m*/ * @param _groupId The id of the group containing various ingredients. * @param _changedSince Limits the result to elements that changed since the given date. ISO * 8601 time e.g. 2016-01-19T11:54:07+0100. Optional. */ @GET @TokenSecured @Produces({ "application/json" }) public Response getIngredients(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { List<Ingredient> ingedients; List<DeletedObject> deletedIngredients; EntityManager manager = DatabaseHelper.getInstance().getManager(); DeviceGroup group = manager.find(DeviceGroup.class, _groupId); if (_changedSince != null) { Instant changedSince; try { changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant(); } catch (ParseException _e) { manager.close(); return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE); } TypedQuery<Ingredient> IngredientsQuery = manager.createQuery( "select i from " + "Ingredient i where i.group = :group and i.updated > :updated", Ingredient.class); IngredientsQuery.setParameter("group", group); IngredientsQuery.setParameter("updated", changedSince); ingedients = IngredientsQuery.getResultList(); TypedQuery<DeletedObject> deletedIngredientsQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and " + "do.type = :type", DeletedObject.class); deletedIngredientsQuery.setParameter("group", group); deletedIngredientsQuery.setParameter("updated", changedSince); deletedIngredientsQuery.setParameter("type", DeletedObject.Type.INGREDIENT); deletedIngredients = deletedIngredientsQuery.getResultList(); } else { ingedients = new ArrayList<Ingredient>(group.getIngredients()); TypedQuery<DeletedObject> deletedIngredientsQuery = manager.createQuery( "select do from " + "DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); deletedIngredientsQuery.setParameter("group", group); deletedIngredientsQuery.setParameter("type", DeletedObject.Type.INGREDIENT); deletedIngredients = deletedIngredientsQuery.getResultList(); } manager.close(); ArrayList<IngredientInfo> rtn = new ArrayList<IngredientInfo>( ingedients.size() + deletedIngredients.size()); for (Ingredient current : ingedients) { IngredientInfo toAdd = new IngredientInfo().withDeleted(false); toAdd.setUUID(current.getUUID()); toAdd.setProductUUID(current.getProduct().getUUID()); toAdd.setRecipeUUID(current.getRecipe().getUUID()); toAdd.setAmount(current.getAmount()); toAdd.setLastChanged(Date.from(current.getUpdated())); rtn.add(toAdd); } for (DeletedObject current : deletedIngredients) { IngredientInfo toAdd = new IngredientInfo(); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(true); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }