List of usage examples for javax.persistence EntityTransaction isActive
public boolean isActive();
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 {// w w w . j a va2s.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.apache.juddi.replication.ReplicationNotifier.java
@Deprecated private Node getNode(String messageSender) { EntityManager em = PersistenceManager.getEntityManager(); EntityTransaction tx = null; try {/* www. jav a 2s. c o m*/ tx = em.getTransaction(); tx.begin(); Node api = new Node(); org.apache.juddi.model.Node find = em.find(org.apache.juddi.model.Node.class, messageSender); if (find != null) { MappingModelToApi.mapNode(find, api); } tx.commit(); return api; } catch (Exception ex) { log.error("error", ex); if (tx != null && tx.isActive()) { tx.rollback(); } } finally { em.close(); } return null; }
From source file:nl.b3p.kaartenbalie.service.SecurityRealm.java
@Override public Principal getAuthenticatedPrincipal(String username, String password) { Object identity = null;//from w ww . j av a 2 s. c o m EntityTransaction tx = null; try { identity = MyEMFDatabase.createEntityManager(MyEMFDatabase.REALM_EM); EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.REALM_EM); tx = em.getTransaction(); tx.begin(); try { User user = (User) em.createQuery("from User u where " + "lower(u.username) = lower(:username) ") .setParameter("username", username).getSingleResult(); return user; } catch (NoResultException nre) { return null; } } catch (Exception e) { log.error("Exception getting authenticated user from database", e); if (tx != null && tx.isActive()) { tx.rollback(); } } finally { if (tx != null && tx.isActive() && !tx.getRollbackOnly()) { tx.commit(); } MyEMFDatabase.closeEntityManager(identity, MyEMFDatabase.REALM_EM); } return null; }
From source file:it.infn.ct.futuregateway.apiserver.v1.TaskCollectionService.java
/** * Register a new task./* w w w. j a v a2s. co m*/ * * @param task The task to register * @return The task registered */ @POST @Status(Response.Status.CREATED) @Consumes({ MediaType.APPLICATION_JSON, Constants.INDIGOMIMETYPE }) @Produces(Constants.INDIGOMIMETYPE) public final Task createTask(final Task task) { if (task.getApplicationId() == null) { throw new BadRequestException("A valid application for the task" + " must be provided"); } task.addObserver(new TaskObserver(getEntityManagerFactory(), getSubmissionThreadPool())); task.setDateCreated(new Date()); task.setUserName(getUser()); task.setStatus(Task.STATUS.WAITING); EntityManager em = getEntityManager(); EntityTransaction et = null; try { et = em.getTransaction(); et.begin(); Application app = em.find(Application.class, task.getApplicationId()); if (app == null) { throw new BadRequestException("Application id not valid"); } task.setApplicationDetail(app); em.persist(task); et.commit(); log.debug("New task registered: " + task.getId()); } catch (BadRequestException bre) { throw bre; } catch (RuntimeException re) { log.error("Impossible to create a task"); log.debug(re); throw re; } finally { if (et != null && et.isActive()) { et.rollback(); } em.close(); } return task; }
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 {/*www .j av a 2 s.c o m*/ 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.google.inject.persist.jpa.KuneJpaLocalTxnInterceptor.java
@Override public Object invoke(final MethodInvocation methodInvocation) throws Throwable, DefaultException { // Should we start a unit of work? if (!emProvider.isWorking()) { emProvider.begin();/*from w w w .java 2s . c om*/ didWeStartWork.set(true); LOG.debug("Starting transaction"); } final KuneTransactional transactional = readTransactionMetadata(methodInvocation); final EntityManager em = this.emProvider.get(); // Allow 'joining' of transactions if there is an enclosing // @KuneTransactional method. if (em.getTransaction().isActive()) { LOG.debug("Joining a previous transaction"); return methodInvocation.proceed(); } final EntityTransaction txn = em.getTransaction(); txn.begin(); Object result; try { result = methodInvocation.proceed(); } catch (final Exception e) { // commit transaction only if rollback didnt occur LOG.debug("Exception in transaction", e); if (rollbackIfNecessary(transactional, e, txn)) { txn.commit(); } // propagate whatever exception is thrown anyway throw e; } finally { // Close the em if necessary (guarded so this code doesn't run unless // catch fired). if (null != didWeStartWork.get() && !txn.isActive()) { didWeStartWork.remove(); unitOfWork.end(); } } // everything was normal so commit the txn (do not move into try block above // as it // interferes with the advised method's throwing semantics) try { LOG.debug("Trying to commit transaction"); txn.commit(); } finally { LOG.debug("Transaction commited"); // close the em if necessary if (null != didWeStartWork.get()) { didWeStartWork.remove(); unitOfWork.end(); } } // or return result return result; }
From source file:com.eucalyptus.images.Images.java
public static KernelImageInfo lookupKernel(final String kernelId) { EntityTransaction tx = Entities.get(KernelImageInfo.class); KernelImageInfo ret = new KernelImageInfo(); try {/*w w w . j a v a 2s . c o m*/ ret = Entities.uniqueResult(Images.exampleKernelWithImageId(kernelId)); tx.commit(); } catch (Exception e) { LOG.error("Kernel '" + kernelId + "' does not exist" + e); throw new NoSuchElementException("InvalidAMIID.NotFound"); } finally { if (tx.isActive()) tx.rollback(); } return ret; }
From source file:org.eclipse.smila.binarystorage.persistence.jpa.JPABinaryPersistence.java
/** * Stores the given BinaryStorageDao, updating an existing one or creating a new one. * * @param dao/*from w w w .j a va 2s . c o m*/ * the BinaryStorageDao to store * @throws BinaryStorageException * if any error occurs */ // TODO: don't know if this synchronize is good, was needed to pass the JUNit test TestConcurrentBSSAccessJPA private synchronized void store(final BinaryStorageDao dao) throws BinaryStorageException { _lock.readLock().lock(); try { final EntityManager em = createEntityManager(); final EntityTransaction transaction = em.getTransaction(); try { transaction.begin(); if (findBinaryStorageDao(em, dao.getId()) == null) { em.persist(dao); } else { em.merge(dao); } transaction.commit(); if (_log.isTraceEnabled()) { _log.trace("stored content of id:" + dao.getId()); } } catch (final Exception e) { if (transaction.isActive()) { transaction.rollback(); } throw new BinaryStorageException(e, "error storing record id: " + dao.getId()); } finally { closeEntityManager(em); } } finally { _lock.readLock().unlock(); } }
From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java
public UserAction addUserTrackingEvent(UserAction a) throws UserTrackingException { EntityManager em = null;// w ww. j av a 2s . co m EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); em.persist(a); tx.commit(); return a; } catch (Exception e) { if (tx.isActive()) { tx.rollback(); } throw new UserTrackingException(e); } finally { if (em != null && em.isOpen()) { em.close(); } } }
From source file:com.eucalyptus.images.Images.java
public static RamdiskImageInfo lookupRamdisk(final String ramdiskId) { EntityTransaction tx = Entities.get(RamdiskImageInfo.class); RamdiskImageInfo ret = new RamdiskImageInfo(); try {/*from ww w .ja v a 2 s.c om*/ ret = Entities.uniqueResult(Images.exampleRamdiskWithImageId(ramdiskId)); tx.commit(); } catch (Exception e) { LOG.error("Ramdisk '" + ramdiskId + "' does not exist" + e); throw new NoSuchElementException("InvalidAMIID.NotFound"); } finally { if (tx.isActive()) tx.rollback(); } return ret; }