List of usage examples for javax.persistence EntityTransaction begin
public void begin();
From source file:org.apache.juddi.subscription.SubscriptionNotifier.java
/** * Deletes the subscription. i.e. when it is expired. * @param subscription//from ww w . ja v a 2 s .c o m */ protected void deleteSubscription(Subscription subscription) { EntityManager em = PersistenceManager.getEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); em.remove(subscription); tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } em.close(); } }
From source file:org.eclipse.smila.binarystorage.persistence.jpa.JPABinaryPersistence.java
/** * {@inheritDoc}// ww w .jav a 2 s. c o m * * @see org.eclipse.smila.binarystorage.internal.impl.persistence.BinaryPersistence#deleteBinary(java.lang.String) */ @Override public void deleteBinary(final String key) throws BinaryStorageException { if (key == null) { throw new BinaryStorageException("parameter key is null"); } _lock.readLock().lock(); try { final EntityManager em = createEntityManager(); try { final BinaryStorageDao dao = findBinaryStorageDao(em, key); if (dao != null) { final EntityTransaction transaction = em.getTransaction(); try { transaction.begin(); em.remove(dao); transaction.commit(); } catch (final Exception e) { if (transaction.isActive()) { transaction.rollback(); } throw new BinaryStorageException(e, "error removing record id: " + key); } } else { if (_log.isDebugEnabled()) { _log.debug("could not remove id: " + key + ". no binary object with this id exists."); } } } finally { closeEntityManager(em); } } finally { _lock.readLock().unlock(); } }
From source file:org.apache.juddi.subscription.SubscriptionNotifier.java
/** * Obtains all subscriptions in the system. * @return Collection of All Subscriptions in the system. *///from w w w . j av a 2 s. c om @SuppressWarnings("unchecked") protected Collection<Subscription> getAllAsyncSubscriptions() { Collection<Subscription> subscriptions = null; EntityManager em = PersistenceManager.getEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); Query query = em.createQuery("SELECT s FROM Subscription s WHERE s.bindingKey IS NOT NULL"); subscriptions = (Collection<Subscription>) query.getResultList(); tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } em.close(); } return subscriptions; }
From source file:org.eclipse.smila.recordstorage.impl.RecordStorageImpl.java
/** * {@inheritDoc}/*from w w w. ja va 2 s. c om*/ */ @Override public void storeRecord(final Record record) throws RecordStorageException { if (record == null) { throw new RecordStorageException("parameter record is null"); } _lock.readLock().lock(); try { final EntityManager em = createEntityManager(); final EntityTransaction transaction = em.getTransaction(); try { final RecordDao dao = new RecordDao(record); transaction.begin(); if (findRecordDao(em, record.getId()) == null) { em.persist(dao); } else { em.merge(dao); } transaction.commit(); if (_log.isTraceEnabled()) { _log.trace("stored record Id:" + record.getId()); } } catch (final Exception e) { if (transaction.isActive()) { transaction.rollback(); } throw new RecordStorageException(e, "error storing record id: " + record.getId()); } finally { closeEntityManager(em); } } finally { _lock.readLock().unlock(); } }
From source file:org.opencastproject.scheduler.impl.persistence.SchedulerServiceDatabaseImpl.java
@Override public void deleteEvent(long eventId) throws NotFoundException, SchedulerServiceDatabaseException { EntityManager em = null;// w w w .j a va 2 s.c o m EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); EventEntity entity = em.find(EventEntity.class, eventId); if (entity == null) { throw new NotFoundException("Event with ID " + eventId + " does not exist"); } em.remove(entity); tx.commit(); } catch (Exception e) { if (tx.isActive()) { tx.rollback(); } if (e instanceof NotFoundException) { throw (NotFoundException) e; } logger.error("Could not delete series: {}", e.getMessage()); throw new SchedulerServiceDatabaseException(e); } finally { if (em != null) em.close(); } }
From source file:org.opencastproject.series.impl.persistence.SeriesServiceDatabaseImpl.java
@Override public void deleteSeries(String seriesId) throws SeriesServiceDatabaseException, NotFoundException { EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try {//ww w.ja v a 2 s .c o m tx.begin(); SeriesEntity entity = getSeriesEntity(seriesId, em); if (entity == null) { throw new NotFoundException("Series with ID " + seriesId + " does not exist"); } // Ensure this user is allowed to delete this series String accessControlXml = entity.getAccessControl(); if (accessControlXml != null) { AccessControlList acl = AccessControlParser.parseAcl(accessControlXml); User currentUser = securityService.getUser(); Organization currentOrg = securityService.getOrganization(); if (!AccessControlUtil.isAuthorized(acl, currentUser, currentOrg, EDIT_SERIES_PERMISSION)) { throw new UnauthorizedException( currentUser + " is not authorized to update series " + seriesId); } } em.remove(entity); tx.commit(); } catch (NotFoundException e) { throw e; } catch (Exception e) { logger.error("Could not delete series: {}", e.getMessage()); if (tx.isActive()) { tx.rollback(); } throw new SeriesServiceDatabaseException(e); } finally { em.close(); } }
From source file:org.opencastproject.userdirectory.jpa.JpaUserAndRoleProvider.java
/** * A utility class to load the user directory. * //ww w.j av a 2 s . c o m * @param user * the user object */ public void addUser(JpaUser user) { // Create a JPA user with an encoded password. String encodedPassword = PasswordEncoder.encode(user.getPassword(), user.getUsername()); user = new JpaUser(user.getUsername(), encodedPassword, user.getOrganization(), user.getRoles()); // Then save the user EntityManager em = null; EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); em.persist(user); tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } if (em != null) em.close(); } }
From source file:org.opencastproject.series.impl.persistence.SeriesServiceDatabaseImpl.java
/** * {@inheritDoc}//from www . j a v a 2 s. c o m * * @see org.opencastproject.series.impl.SeriesServiceDatabase#getSeries(java.lang.String) */ @Override public DublinCoreCatalog getSeries(String seriesId) throws NotFoundException, SeriesServiceDatabaseException { EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); SeriesEntity entity = getSeriesEntity(seriesId, em); if (entity == null) { throw new NotFoundException("No series with id=" + seriesId + " exists"); } // Ensure this user is allowed to read this series String accessControlXml = entity.getAccessControl(); if (accessControlXml != null) { AccessControlList acl = AccessControlParser.parseAcl(accessControlXml); User currentUser = securityService.getUser(); Organization currentOrg = securityService.getOrganization(); // There are several reasons a user may need to load a series: to read content, to edit it, or add content if (!AccessControlUtil.isAuthorized(acl, currentUser, currentOrg, READ_CONTENT_PERMISSION) && !AccessControlUtil.isAuthorized(acl, currentUser, currentOrg, CONTRIBUTE_CONTENT_PERMISSION) && !AccessControlUtil.isAuthorized(acl, currentUser, currentOrg, EDIT_SERIES_PERMISSION)) { throw new UnauthorizedException(currentUser + " is not authorized to see series " + seriesId); } } return dcService.load(IOUtils.toInputStream(entity.getDublinCoreXML(), "UTF-8")); } catch (NotFoundException e) { throw e; } catch (Exception e) { logger.error("Could not update series: {}", e.getMessage()); if (tx.isActive()) { tx.rollback(); } throw new SeriesServiceDatabaseException(e); } finally { em.close(); } }
From source file:org.opencastproject.scheduler.impl.persistence.SchedulerServiceDatabaseImpl.java
@Override public void storeEvents(DublinCoreCatalog... events) throws SchedulerServiceDatabaseException { EntityManager em = null;/* ww w . jav a 2 s . co m*/ EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); for (DublinCoreCatalog event : events) { Long eventId = Long.parseLong(event.getFirst(DublinCore.PROPERTY_IDENTIFIER)); String dcXML; try { dcXML = serializeDublinCore(event); } catch (Exception e1) { logger.error("Could not serialize Dublin Core: {}", e1); throw new SchedulerServiceDatabaseException(e1); } EventEntity entity = new EventEntity(); entity.setEventId(eventId); entity.setEventDublinCore(dcXML); em.persist(entity); } tx.commit(); } catch (SchedulerServiceDatabaseException e) { throw e; } catch (Exception e) { if (tx.isActive()) { tx.rollback(); } logger.error("Could not store events: {}", e); throw new SchedulerServiceDatabaseException(e); } finally { if (em != null) em.close(); } }
From source file:org.opencastproject.scheduler.impl.persistence.SchedulerServiceDatabaseImpl.java
@Override public void updateEvent(DublinCoreCatalog event) throws NotFoundException, SchedulerServiceDatabaseException { if (event == null) { throw new SchedulerServiceDatabaseException("Cannot update <null> event"); }//from w w w. j a v a 2 s . c o m Long eventId = Long.parseLong(event.getFirst(DublinCore.PROPERTY_IDENTIFIER)); String dcXML; try { dcXML = serializeDublinCore(event); } catch (Exception e1) { logger.error("Could not serialize Dublin Core: {}", e1); throw new SchedulerServiceDatabaseException(e1); } EntityManager em = null; EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); EventEntity entity = em.find(EventEntity.class, eventId); if (entity == null) { throw new NotFoundException("Event with ID " + eventId + " does not exist."); } entity.setEventDublinCore(dcXML); em.merge(entity); tx.commit(); } catch (NotFoundException e) { throw e; } catch (Exception e) { if (tx.isActive()) { tx.rollback(); } logger.error("Could not store event: {}", e.getMessage()); throw new SchedulerServiceDatabaseException(e); } finally { if (em != null) em.close(); } }