List of usage examples for javax.persistence EntityTransaction commit
public void commit();
From source file:uk.ac.horizon.ug.locationbasedgame.author.CRUDServlet.java
/** Create on POST. * E.g. curl -d '{...}' http://localhost:8888/author/configuration/ * @param req/*from ww w. jav a 2 s. c om*/ * @param resp * @throws ServletException * @throws IOException */ private void doCreate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub try { Object o = parseObject(req); Key key = validateCreate(o); // try adding EntityManager em = EMF.get().createEntityManager(); EntityTransaction et = em.getTransaction(); try { et.begin(); if (em.find(getObjectClass(), key) != null) throw new RequestException(HttpServletResponse.SC_CONFLICT, "object already exists (" + key + ")"); em.persist(o); et.commit(); logger.info("Added " + o); } finally { if (et.isActive()) et.rollback(); em.close(); } resp.setCharacterEncoding(ENCODING); resp.setContentType(JSON_MIME_TYPE); Writer w = new OutputStreamWriter(resp.getOutputStream(), ENCODING); JSONWriter jw = new JSONWriter(w); listObject(jw, o); w.close(); } catch (RequestException e) { resp.sendError(e.getErrorCode(), e.getMessage()); } catch (Exception e) { logger.log(Level.WARNING, "Getting object of type " + getObjectClass(), e); resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); return; } }
From source file:org.opencastproject.adminui.usersettings.UserSettingsService.java
/** * Delete a user setting by using a unique id to find it. * * @param id/* ww w.j a v a 2 s . co m*/ * The unique id for the user setting. * @throws UserSettingsServiceException */ public void deleteUserSetting(long id) throws UserSettingsServiceException { EntityManager em = null; EntityTransaction tx = null; try { em = emf.createEntityManager(); UserSettingDto userSettingsDto = em.find(UserSettingDto.class, id); tx = em.getTransaction(); tx.begin(); em.remove(userSettingsDto); tx.commit(); } catch (Exception e) { logger.error("Could not delete user setting '%d': %s", id, ExceptionUtils.getStackTrace(e)); if (tx.isActive()) tx.rollback(); throw new UserSettingsServiceException(e); } finally { if (em != null) em.close(); } }
From source file:com.busimu.core.dao.impl.UserMngDaoPolicyJpaImpl.java
/** * {@inheritDoc}/*w w w . j av a 2 s . c om*/ */ @Override public User activateUser(User user, String licenseCode) { EntityManager em = ((EntityManagerHolder) TransactionSynchronizationManager.getResource(emf)) .getEntityManager(); EntityTransaction tx = em.getTransaction(); License l = getLicenseByCode(em, licenseCode); if (l != null) { if (!tx.isActive()) { tx.begin(); } user.setType(User.Type.valueOf(l.getType().name())); user.setStatus(User.Status.ACTIVE); user = em.merge(user); em.remove(l); tx.commit(); } return user; }
From source file:org.opencastproject.comments.persistence.CommentDatabaseImpl.java
@Override public void deleteComment(long commentId) throws CommentDatabaseException, NotFoundException { EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try {//from w w w . ja v a 2 s .c o m tx.begin(); Option<CommentDto> dto = CommentDatabaseUtils.find(Option.some(commentId), em, CommentDto.class); if (dto.isNone()) throw new NotFoundException("Comment with ID " + commentId + " does not exist"); CommentDatabaseUtils.deleteReplies(dto.get().getReplies(), em); dto.get().setReplies(new ArrayList<CommentReplyDto>()); em.remove(dto.get()); tx.commit(); } catch (NotFoundException e) { throw e; } catch (Exception e) { logger.error("Could not delete comment: {}", ExceptionUtils.getStackTrace(e)); if (tx.isActive()) tx.rollback(); throw new CommentDatabaseException(e); } finally { if (em != null) em.close(); } }
From source file:org.opencastproject.kernel.security.persistence.OrganizationDatabaseImpl.java
/** * @see org.opencastproject.kernel.security.persistence.OrganizationDatabase#deleteOrganization(java.lang.String) *//*from w w w . j a va2 s . c o m*/ @Override public void deleteOrganization(String orgId) throws OrganizationDatabaseException, NotFoundException { EntityManager em = null; EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); JpaOrganization organization = getOrganizationEntity(orgId); if (organization == null) throw new NotFoundException("Organization " + orgId + " does not exist"); em.remove(organization); tx.commit(); } catch (NotFoundException e) { throw e; } catch (Exception e) { logger.error("Could not delete organization: {}", e.getMessage()); if (tx.isActive()) { tx.rollback(); } throw new OrganizationDatabaseException(e); } finally { if (em != null) em.close(); } }
From source file:com.eucalyptus.images.Images.java
public static ImageInfo lookupImage(String imageId) { final EntityTransaction db = Entities.get(ImageInfo.class); try {// w w w .ja va 2 s. c o m final ImageInfo found = Entities.uniqueResult(Images.exampleWithImageId(imageId)); db.commit(); return found; } catch (final NoSuchElementException ex) { db.rollback(); throw ex; } catch (final Exception ex) { db.rollback(); throw Exceptions.toUndeclared(ex); } finally { if (db.isActive()) db.rollback(); } }
From source file:org.sakaiproject.kernel.authz.simple.SubjectPermissionListener.java
/** * {@inheritDoc}// w ww. j a va 2s . co m * * @see org.sakaiproject.kernel.jcr.api.JcrContentListener#onEvent(int, * java.lang.String, java.lang.String, java.lang.String) */ public void onEvent(int type, String userID, String filePath, String fileName) { if (fileName.equals(KernelConstants.GROUP_FILE_NAME)) { InputStream in = null; try { in = jcrNodeFactoryService.getInputStream(filePath); String groupBody = IOUtils.readFully(in, "UTF-8"); if (groupBody != null && groupBody.length() > 0) { EntityTransaction transaction = entityManager.getTransaction(); if (!transaction.isActive()) { transaction.begin(); } // update the index for subjects and groups updateSubjectPermissionIndex(groupBody); // update the index used for searching sites updateSiteIndex(groupBody, filePath); transaction.commit(); } } catch (UnsupportedEncodingException e) { LOG.error(e); } catch (IOException e) { LOG.warn("Failed to read userenv " + filePath + " cause :" + e.getMessage()); if (debug) LOG.debug(e); } catch (RepositoryException e) { LOG.warn("Failed to read userenv for " + filePath + " cause :" + e.getMessage()); if (debug) LOG.debug(e); } catch (JCRNodeFactoryServiceException e) { LOG.warn("Failed to read userenv for " + filePath + " cause :" + e.getMessage()); if (debug) LOG.debug(e); } finally { try { in.close(); } catch (Exception ex) {// not interested in this } } } }
From source file:com.sdl.odata.datasource.jpa.JPADataSource.java
@Override public void delete(ODataUri uri, EntityDataModel entityDataModel) throws ODataException { Option<Object> entity = extractEntityWithKeys(uri, entityDataModel); if (entity.isDefined()) { Object jpaEntity = entityMapper.convertODataEntityToDS(entity.get(), entityDataModel); if (jpaEntity != null) { EntityManager entityManager = getEntityManager(); EntityTransaction transaction = entityManager.getTransaction(); try { transaction.begin();//from w ww . j a v a 2s.c o m Object attached = entityManager.merge(jpaEntity); entityManager.remove(attached); } catch (PersistenceException e) { LOG.error("Could not remove entity: {}", entity); throw new ODataDataSourceException("Could not remove entity", e); } finally { if (transaction.isActive()) { transaction.commit(); } else { transaction.rollback(); } } } else { throw new ODataDataSourceException("Could not remove entity, could not be loaded"); } } }
From source file:com.eucalyptus.images.Images.java
public static void setImageState(String imageId, ImageMetadata.State state) throws NoSuchImageException { final EntityTransaction db = Entities.get(ImageInfo.class); try {/*ww w .j a v a 2s. com*/ ImageInfo img = Entities.uniqueResult(Images.exampleWithImageId(imageId)); img.setState(state); db.commit(); } catch (final Exception e) { db.rollback(); throw new NoSuchImageException("Failed to update image state: " + imageId); } finally { if (db.isActive()) db.rollback(); } }
From source file:com.eucalyptus.images.Images.java
public static void registerFromPendingImage(String imageId) throws Exception { EntityTransaction tx = Entities.get(PutGetImageInfo.class); try {// ww w . ja v a 2 s . c o m ImageInfo ret = Entities.uniqueResult(Images.exampleWithImageId(imageId)); ret.setState(State.available); tx.commit(); } catch (Exception e) { tx.rollback(); throw new EucalyptusCloudException( "Failed to update image: " + imageId + " because of: " + e.getMessage(), e); } }