Esempio n. 1
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);
   }
 }
Esempio n. 2
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;
   }
 }
Esempio n. 3
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;
   }
 }
Esempio n. 4
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();
     }
   }
 }
Esempio n. 5
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();
   }
 }
Esempio n. 6
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();
   }
 }
Esempio n. 7
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);
   }
 }