@Override
 public RuntimeException convertCommitException(RuntimeException e) {
   if (sharedSessionContract.getFactory().getSessionFactoryOptions().isJpaBootstrap()) {
     Throwable wrappedException;
     if (e instanceof PersistenceException) {
       Throwable cause = e.getCause() == null ? e : e.getCause();
       if (cause instanceof HibernateException) {
         wrappedException = convert((HibernateException) cause);
       } else {
         wrappedException = cause;
       }
     } else if (e instanceof HibernateException) {
       wrappedException = convert((HibernateException) e);
     } else {
       wrappedException = e;
     }
     try {
       // as per the spec we should rollback if commit fails
       sharedSessionContract.getTransaction().rollback();
     } catch (Exception re) {
       // swallow
     }
     return new RollbackException("Error while committing the transaction", wrappedException);
   } else {
     return e;
   }
 }
 @Override
 public RuntimeException convert(HibernateException e, LockOptions lockOptions) {
   Throwable cause = e;
   if (cause instanceof StaleStateException) {
     final PersistenceException converted = wrapStaleStateException((StaleStateException) cause);
     handlePersistenceException(converted);
     return converted;
   } else if (cause instanceof LockingStrategyException) {
     final PersistenceException converted =
         wrapLockException((HibernateException) cause, lockOptions);
     handlePersistenceException(converted);
     return converted;
   } else if (cause instanceof org.hibernate.exception.LockTimeoutException) {
     final PersistenceException converted =
         wrapLockException((HibernateException) cause, lockOptions);
     handlePersistenceException(converted);
     return converted;
   } else if (cause instanceof org.hibernate.PessimisticLockException) {
     final PersistenceException converted =
         wrapLockException((HibernateException) cause, lockOptions);
     handlePersistenceException(converted);
     return converted;
   } else if (cause instanceof org.hibernate.QueryTimeoutException) {
     final QueryTimeoutException converted = new QueryTimeoutException(cause.getMessage(), cause);
     handlePersistenceException(converted);
     return converted;
   } else if (cause instanceof ObjectNotFoundException) {
     final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
     handlePersistenceException(converted);
     return converted;
   } else if (cause instanceof org.hibernate.NonUniqueObjectException) {
     final EntityExistsException converted = new EntityExistsException(cause.getMessage());
     handlePersistenceException(converted);
     return converted;
   } else if (cause instanceof org.hibernate.NonUniqueResultException) {
     final NonUniqueResultException converted = new NonUniqueResultException(cause.getMessage());
     handlePersistenceException(converted);
     return converted;
   } else if (cause instanceof UnresolvableObjectException) {
     final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
     handlePersistenceException(converted);
     return converted;
   } else if (cause instanceof QueryException) {
     return new IllegalArgumentException(cause);
   } else if (cause instanceof MultipleBagFetchException) {
     return new IllegalArgumentException(cause);
   } else if (cause instanceof TransientObjectException) {
     try {
       sharedSessionContract.markForRollbackOnly();
     } catch (Exception ne) {
       // we do not want the subsequent exception to swallow the original one
       log.unableToMarkForRollbackOnTransientObjectException(ne);
     }
     return new IllegalStateException(e); // Spec 3.2.3 Synchronization rules
   } else {
     final PersistenceException converted = new PersistenceException(cause);
     handlePersistenceException(converted);
     return converted;
   }
 }
 protected PersistenceException wrapStaleStateException(StaleStateException e) {
   PersistenceException pe;
   if (e instanceof StaleObjectStateException) {
     final StaleObjectStateException sose = (StaleObjectStateException) e;
     final Serializable identifier = sose.getIdentifier();
     if (identifier != null) {
       try {
         final Object entity = sharedSessionContract.load(sose.getEntityName(), identifier);
         if (entity instanceof Serializable) {
           // avoid some user errors regarding boundary crossing
           pe = new OptimisticLockException(e.getMessage(), e, entity);
         } else {
           pe = new OptimisticLockException(e.getMessage(), e);
         }
       } catch (EntityNotFoundException enfe) {
         pe = new OptimisticLockException(e.getMessage(), e);
       }
     } else {
       pe = new OptimisticLockException(e.getMessage(), e);
     }
   } else {
     pe = new OptimisticLockException(e.getMessage(), e);
   }
   return pe;
 }
 @Override
 public RuntimeException convert(RuntimeException e, LockOptions lockOptions) {
   RuntimeException result = e;
   if (e instanceof HibernateException) {
     result = convert((HibernateException) e, lockOptions);
   } else {
     sharedSessionContract.markForRollbackOnly();
   }
   return result;
 }
  private void handlePersistenceException(PersistenceException e) {
    if (e instanceof NoResultException) {
      return;
    }
    if (e instanceof NonUniqueResultException) {
      return;
    }
    if (e instanceof LockTimeoutException) {
      return;
    }
    if (e instanceof QueryTimeoutException) {
      return;
    }

    try {
      sharedSessionContract.markForRollbackOnly();
    } catch (Exception ne) {
      // we do not want the subsequent exception to swallow the original one
      log.unableToMarkForRollbackOnPersistenceException(ne);
    }
  }
 @Override
 public JDBCException convert(SQLException e, String message) {
   return sharedSessionContract.getJdbcServices().getSqlExceptionHelper().convert(e, message);
 }