Spring.Data.NHibernate.SessionFactoryUtils.ConvertHibernateAccessException C# (CSharp) Method

ConvertHibernateAccessException() public static method

Convert the given HibernateException to an appropriate exception from the Spring.Dao hierarchy. Note that it is advisable to handle AdoException specifically by using a AdoExceptionTranslator for the underlying ADO.NET exception.
public static ConvertHibernateAccessException ( NHibernate.HibernateException ex ) : Spring.Dao.DataAccessException
ex NHibernate.HibernateException The Hibernate exception that occured.
return Spring.Dao.DataAccessException
        public static DataAccessException ConvertHibernateAccessException(HibernateException ex)
        {
            if (ex is ADOException)
            {
                // ADOException during Hibernate access: only passed in here from custom code,
                // as HibernateTemplate etc will use AdoExceptionTranslator-based handling.
                return new HibernateAdoException("Ado Exception", (ADOException) ex);
            }
            if (ex is UnresolvableObjectException)
            {
                return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex);
            }
            if (ex is ObjectDeletedException)
            {
                return new InvalidDataAccessApiUsageException(ex.Message, ex);
            }
            if (ex is WrongClassException)
            {
                return new HibernateObjectRetrievalFailureException((WrongClassException) ex);
            }
            if (ex is StaleObjectStateException)
            {
                return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex);
            }
            if (ex is StaleStateException)
            {
                return new HibernateOptimisticLockingFailureException((StaleStateException)ex);
            }
            if (ex is QueryException)
            {
                return new HibernateQueryException((QueryException) ex);
            }

            if (ex is PersistentObjectException)
            {
                return new InvalidDataAccessApiUsageException(ex.Message, ex);
            }
            if (ex is TransientObjectException)
            {
                return new InvalidDataAccessApiUsageException(ex.Message, ex);
            }

            if (ex is PropertyValueException)
            {
                return new DataIntegrityViolationException(ex.Message, ex);
            }
            if (ex is PersistentObjectException)
            {
                return new InvalidDataAccessApiUsageException(ex.Message, ex);
            }
            if (ex is NonUniqueResultException)
            {
                return new IncorrectResultSizeDataAccessException(ex.Message, 1);
            }
            // fallback
            return new HibernateSystemException(ex);
        }

Usage Example

示例#1
0
 /// <summary>
 /// Invoked before transaction commit (before
 /// <see cref="Spring.Transaction.Support.ITransactionSynchronization.BeforeCompletion"/>)
 /// </summary>
 /// <param name="readOnly">
 /// If the transaction is defined as a read-only transaction.
 /// </param>
 /// <remarks>
 /// <p>
 /// Can flush transactional sessions to the database.
 /// </p>
 /// <p>
 /// Note that exceptions will get propagated to the commit caller and
 /// cause a rollback of the transaction.
 /// </p>
 /// </remarks>
 public override void BeforeCommit(bool readOnly)
 {
     if (!readOnly)
     {
         // read-write transaction -> flush the Hibernate Session
         log.Debug("Flushing Hibernate Session on transaction synchronization");
         ISession session = this.sessionHolder.Session;
         //Further check: only flush when not FlushMode.NEVER
         if (session.FlushMode != FlushMode.Never)
         {
             try
             {
                 session.Flush();
                 //TODO can throw System.ObjectDisposedException...
             }
             catch (ADOException ex)
             {
                 if (this.adoExceptionTranslator != null)
                 {
                     //TODO investigate how ADOException wraps inner exception.
                     throw this.adoExceptionTranslator.Translate(
                               "Hibernate transaction synchronization: " + ex.Message, null, ex.InnerException);
                 }
                 else
                 {
                     throw new HibernateAdoException("ADO.NET Exception", ex);
                 }
             }
             catch (HibernateException ex)
             {
                 throw SessionFactoryUtils.ConvertHibernateAccessException(ex);
             }
         }
     }
 }
All Usage Examples Of Spring.Data.NHibernate.SessionFactoryUtils::ConvertHibernateAccessException