Exemplo n.º 1
0
 /**
  * @delegate Do not change semantics here.
  * @see javax.persistence.EntityTransaction#begin()
  */
 @Override
 public void begin() throws RecoverablePersistenceException {
   try {
     this.txState.begin();
   } catch (final RecoverablePersistenceException ex) {
     PersistenceExceptions.throwFiltered(ex);
     removeTransaction(this);
   } catch (final RuntimeException ex) {
     PersistenceExceptions.throwFiltered(ex);
     removeTransaction(this);
     throw ex;
   }
 }
Exemplo n.º 2
0
 TransactionState(final String ctx) {
   this.startTime = System.currentTimeMillis();
   this.txUuid = String.format("%s:%s", ctx, UUID.randomUUID().toString());
   this.stopWatch = new StopWatch();
   this.stopWatch.start();
   this.owner = Logs.isExtrrreeeme() ? Threads.currentStackString() : "n/a";
   try {
     this.eventLog(TxStep.BEGIN, TxEvent.CREATE);
     final EntityManagerFactory anemf =
         (EntityManagerFactoryImpl) PersistenceContexts.getEntityManagerFactory(ctx);
     checkParam(anemf, notNullValue());
     this.em = anemf.createEntityManager();
     checkParam(this.em, notNullValue());
     this.transaction = this.em.getTransaction();
     this.transaction.begin();
     this.session = new WeakReference<Session>((Session) this.em.getDelegate());
     this.eventLog(TxStep.END, TxEvent.CREATE);
   } catch (final Throwable ex) {
     Logs.exhaust().error(ex, ex);
     this.eventLog(TxStep.FAIL, TxEvent.CREATE);
     this.rollback();
     throw new RuntimeException(PersistenceExceptions.throwFiltered(ex));
   } finally {
     outstanding.put(this.txUuid, this);
   }
 }
Exemplo n.º 3
0
 /**
  * Invokes underlying merge implementation per jsr-220
  *
  * @param object The object to merge
  * @param <T> The return type
  * @return The persistent instance for the object
  */
 public static <T> T mergeDirect(final T object) {
   try {
     return getTransaction(object).getTxState().getEntityManager().merge(object);
   } catch (final RuntimeException ex) {
     PersistenceExceptions.throwFiltered(ex);
     throw ex;
   }
 }
Exemplo n.º 4
0
 /**
  *
  *
  * <table>
  * <tbody>
  * <tr valign="top">
  * <th>Scenario</th>
  * <th><tt>EntityManager.persist</tt></th>
  * <th><tt>EntityManager.merge</tt></th>
  * <th><tt>SessionManager.saveOrUpdate</tt></th>
  * </tr>
  * <tr valign="top">
  * <th>Object passed was never persisted</th>
  *
  * <td>1. Object added to persistence context as new entity<br>
  * 2. New entity inserted into database at flush/commit</td>
  * <td>1. State copied to new entity.<br>
  * 2. New entity added to persistence context<br>
  * 3. New entity inserted into database at flush/commit<br>
  * 4. New entity returned</td>
  * <td>1. Object added to persistence context as new entity<br>
  * 2. New entity inserted into database at flush/commit</td>
  * </tr>
  * <tr valign="top">
  * <th>Object was previously persisted, but not loaded in this persistence context</th>
  * <td>1. <tt>EntityExistsException</tt> thrown (or a <tt>PersistenceException</tt> at
  * flush/commit)</td>
  *
  * <td>2. Existing entity loaded.<br>
  * 2. State copied from object to loaded entity<br>
  * 3. Loaded entity updated in database at flush/commit<br>
  * 4. Loaded entity returned</td>
  * <td>1. Object added to persistence context<br>
  * 2. Loaded entity updated in database at flush/commit</td>
  * </tr>
  * <tr valign="top">
  * <th>Object was previously persisted and already loaded in this persistence context</th>
  * <td>1. <tt>EntityExistsException</tt> thrown (or a <tt>PersistenceException</tt> at flush or
  * commit time)</td>
  *
  * <td>1. State from object copied to loaded entity<br>
  * 2. Loaded entity updated in database at flush/commit<br>
  * 3. Loaded entity returned</td>
  * <td>1. <tt>NonUniqueObjectException</tt> thrown</td>
  * </tr>
  * </tbody>
  * </table>
  *
  * @param newObject
  */
 public <T> T merge(final T newObject) {
   try {
     return this.getEntityManager().merge(newObject);
   } catch (final RuntimeException ex) {
     PersistenceExceptions.throwFiltered(ex);
     throw ex;
   }
 }
Exemplo n.º 5
0
 public static <T> void refresh(final T newObject, final LockModeType lockMode)
     throws ConstraintViolationException {
   try {
     getTransaction(newObject).getTxState().getEntityManager().refresh(newObject, lockMode);
   } catch (final RuntimeException ex) {
     PersistenceExceptions.throwFiltered(ex);
     throw ex;
   }
 }
Exemplo n.º 6
0
 /**
  * Invokes underlying persist implementation per jsr-220
  *
  * @throws ConstraintViolationException
  * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-1273
  * @param newObject
  * @return
  */
 public static <T> T persist(final T newObject) throws ConstraintViolationException {
   try {
     getTransaction(newObject).getTxState().getEntityManager().persist(newObject);
     return newObject;
   } catch (final RuntimeException ex) {
     PersistenceExceptions.throwFiltered(ex);
     throw ex;
   }
 }
Exemplo n.º 7
0
 /**
  * Invokes underlying persist implementation per jsr-220
  *
  * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-1273
  * @param newObject
  * @return
  */
 public <T> T persist(final T newObject) {
   try {
     this.getEntityManager().persist(newObject);
     return newObject;
   } catch (final RuntimeException ex) {
     PersistenceExceptions.throwFiltered(ex);
     throw ex;
   }
 }
Exemplo n.º 8
0
 private void doRollback() {
   try {
     if ((this.transaction != null) && this.transaction.isActive()) {
       this.transaction.rollback();
     }
   } catch (final Throwable e) {
     PersistenceExceptions.throwFiltered(e);
   } finally {
     this.doCleanup();
   }
 }
Exemplo n.º 9
0
 /**
  * @delegate Do not change semantics here.
  * @see javax.persistence.EntityTransaction#commit()
  */
 @Override
 public void commit() throws RecoverablePersistenceException {
   removeTransaction(this);
   if ((this.txState != null) && this.txState.isActive()) {
     try {
       this.txState.commit();
     } catch (final RuntimeException ex) {
       throw PersistenceExceptions.throwFiltered(ex);
     }
   } else {
     Logs.extreme().error("Duplicate call to commit( ): " + Threads.currentStackString());
   }
 }
Exemplo n.º 10
0
 /**
  * Private for a reason.
  *
  * @see {@link CascadingTx#get(Class)}
  * @param persistenceContext
  * @throws RecoverablePersistenceException
  */
 @SuppressWarnings("unchecked")
 CascadingTx(final String ctx) throws RecoverablePersistenceException {
   final StackTraceElement ste = Threads.currentStackFrame(4);
   final String uuid = UUID.randomUUID().toString();
   this.record = new TxRecord(ctx, uuid, ste);
   try {
     this.txState = new TxState(ctx);
   } catch (final RuntimeException ex) {
     Logs.extreme().error(ex, ex);
     this.rollback();
     throw PersistenceExceptions.throwFiltered(ex);
   }
 }
Exemplo n.º 11
0
 /**
  * @see EntityWrapper#merge(Object)
  * @param newObject
  * @throws PersistenceException
  */
 public <T> T mergeAndCommit(T newObject) {
   try {
     newObject = this.getEntityManager().merge(newObject);
     this.commit();
     return newObject;
   } catch (final RuntimeException ex) {
     try {
       PersistenceExceptions.throwFiltered(ex);
       throw ex;
     } finally {
       this.rollback();
     }
   }
 }
Exemplo n.º 12
0
 @Override
 public void commit() {
   this.eventLog(TxStep.BEGIN, TxEvent.COMMIT);
   try {
     this.transaction.commit();
     this.eventLog(TxStep.END, TxEvent.COMMIT);
   } catch (final RuntimeException e) {
     this.rollback();
     this.eventLog(TxStep.FAIL, TxEvent.COMMIT);
     PersistenceExceptions.throwFiltered(e);
     throw e;
   } finally {
     this.cleanup();
   }
 }
Exemplo n.º 13
0
 @Override
 public void rollback() {
   this.eventLog(TxStep.BEGIN, TxEvent.ROLLBACK);
   try {
     if ((this.transaction != null) && this.transaction.isActive()) {
       this.transaction.rollback();
     }
     this.eventLog(TxStep.END, TxEvent.ROLLBACK);
   } catch (final Throwable e) {
     this.eventLog(TxStep.FAIL, TxEvent.ROLLBACK);
     PersistenceExceptions.throwFiltered(e);
   } finally {
     this.cleanup();
   }
 }
Exemplo n.º 14
0
  /**
   *
   *
   * <table>
   * <tbody>
   * <tr valign="top">
   * <th>Scenario</th>
   * <th><tt>EntityManager.persist</tt></th>
   * <th><tt>EntityManager.merge</tt></th>
   * <th><tt>SessionManager.saveOrUpdate</tt></th>
   * </tr>
   * <tr valign="top">
   * <th>Object passed was never persisted</th>
   *
   * <td>1. Object added to persistence context as new entity<br>
   * 2. New entity inserted into database at flush/commit</td>
   * <td>1. State copied to new entity.<br>
   * 2. New entity added to persistence context<br>
   * 3. New entity inserted into database at flush/commit<br>
   * 4. New entity returned</td>
   * <td>1. Object added to persistence context as new entity<br>
   * 2. New entity inserted into database at flush/commit</td>
   * </tr>
   * <tr valign="top">
   * <th>Object was previously persisted, but not loaded in this persistence context</th>
   * <td>1. <tt>EntityExistsException</tt> thrown (or a <tt>PersistenceException</tt> at
   * flush/commit)</td>
   *
   * <td>2. Existing entity loaded.<br>
   * 2. State copied from object to loaded entity<br>
   * 3. Loaded entity updated in database at flush/commit<br>
   * 4. Loaded entity returned</td>
   * <td>1. Object added to persistence context<br>
   * 2. Loaded entity updated in database at flush/commit</td>
   * </tr>
   * <tr valign="top">
   * <th>Object was previously persisted and already loaded in this persistence context</th>
   * <td>1. <tt>EntityExistsException</tt> thrown (or a <tt>PersistenceException</tt> at flush or
   * commit time)</td>
   *
   * <td>1. State from object copied to loaded entity<br>
   * 2. Loaded entity updated in database at flush/commit<br>
   * 3. Loaded entity returned</td>
   * <td>1. <tt>NonUniqueObjectException</tt> thrown</td>
   * </tr>
   * </tbody>
   * </table>
   *
   * @throws ConstraintViolationException
   * @param newObject
   * @throws NoSuchElementException
   * @throws TransactionException
   */
  public static <T> T merge(final T newObject) throws ConstraintViolationException {
    if (!isPersistent(newObject)) {
      try {
        return uniqueResult(newObject);
      } catch (final Exception ex) {
        return persist(newObject);
      }
    } else {
      try {
        final T persistedObject =
            getTransaction(newObject).getTxState().getEntityManager().merge(newObject);
        return persistedObject == newObject ? newObject : persistedObject;
      } catch (final RuntimeException ex) {

        PersistenceExceptions.throwFiltered(ex);
        throw ex;
      }
    }
  }
Exemplo n.º 15
0
 public static <T> T uniqueResult(final T example)
     throws TransactionException, NoSuchElementException {
   try {
     final Object pk = resolvePrimaryKey(example);
     final String natId = resolveNaturalId(example);
     if (pk != null) {
       return maybePrimaryKey(example);
     } else if (natId != null) {
       return maybeNaturalId(example);
     } else {
       return maybeDefinitelyExample(example);
     }
   } catch (final NoSuchElementException ex) {
     throw ex;
   } catch (final RuntimeException ex) {
     Logs.extreme().trace(ex, ex);
     final Exception newEx = PersistenceExceptions.throwFiltered(ex);
     throw new TransactionInternalException(newEx.getMessage(), newEx);
   }
 }
Exemplo n.º 16
0
 /**
  * Returns the unique result from the database that exactly matches <code>example</code>. The
  * differences between this method and {@link #getUnique(Object)} are:
  *
  * <ol>
  *   <li>{@link #getUnique(Object)} uses <i><code>enableLike</code></i> match and this method does
  *       not. <i><code>enableLike</code></i> criteria trips hibernate when special characters are
  *       involved. So it has been replaced by exact "=" (equals to)
  *   <li>Unique result logic is correctly implemented in this method. If the query returns more
  *       than one result, this method correctly throws an exception wrapping <code>
  *       NonUniqueResultException</code>. {@link #getUnique(Object)} does not throw an exception
  *       in this case and returns a result as long as it finds one or more matching results
  *       (because of the following properties set on the query: <code>setMaxResults(1)</code>,
  *       <code>setFetchSize(1)</code> and <code>setFirstResult(0)</code>)
  * </ol>
  *
  * @param example
  * @return
  * @throws EucalyptusCloudException
  */
 @SuppressWarnings("unchecked")
 public <T> T getUniqueEscape(final T example) throws EucalyptusCloudException {
   try {
     Object id = null;
     try {
       id =
           this.getEntityManager()
               .getEntityManagerFactory()
               .getPersistenceUnitUtil()
               .getIdentifier(example);
     } catch (final Exception ex) {
     }
     if (id != null) {
       final T res = (T) this.getEntityManager().find(example.getClass(), id);
       if (res == null) {
         throw new NoSuchElementException("@Id: " + id);
       } else {
         return res;
       }
     } else if ((example instanceof HasNaturalId)
         && (((HasNaturalId) example).getNaturalId() != null)) {
       final String natId = ((HasNaturalId) example).getNaturalId();
       final T ret =
           (T)
               this.createCriteria(example.getClass())
                   .setLockMode(LockMode.NONE)
                   .setCacheable(true)
                   .add(Restrictions.naturalId().set("naturalId", natId))
                   .uniqueResult();
       if (ret == null) {
         throw new NoSuchElementException("@NaturalId: " + natId);
       }
       return ret;
     } else {
       final T ret =
           (T)
               this.createCriteria(example.getClass())
                   .setLockMode(LockMode.NONE)
                   .setCacheable(true)
                   .add(Example.create(example))
                   .uniqueResult();
       if (ret == null) {
         throw new NoSuchElementException("example: " + LogUtil.dumpObject(example));
       }
       return ret;
     }
   } catch (final NonUniqueResultException ex) {
     throw new EucalyptusCloudException(
         "Get unique failed for "
             + example.getClass().getSimpleName()
             + " because "
             + ex.getMessage(),
         ex);
   } catch (final NoSuchElementException ex) {
     throw new EucalyptusCloudException(
         "Get unique failed for "
             + example.getClass().getSimpleName()
             + " using "
             + ex.getMessage(),
         ex);
   } catch (final Exception ex) {
     final Exception newEx = PersistenceExceptions.throwFiltered(ex);
     throw new EucalyptusCloudException(
         "Get unique failed for "
             + example.getClass().getSimpleName()
             + " because "
             + newEx.getMessage(),
         newEx);
   }
 }