Пример #1
0
        /// <summary>
        /// Perform an actual commit on the given transaction.
        /// </summary>
        /// <param name="status">The status representation of the transaction.</param>
        /// <remarks>
        /// <p>
        /// An implementation does not need to check the rollback-only flag.
        /// </p>
        /// </remarks>
        /// <exception cref="Spring.Transaction.TransactionException">
        /// In the case of system errors.
        /// </exception>
        protected override void DoCommit(DefaultTransactionStatus status)
        {
            HibernateTransactionObject txObject = (HibernateTransactionObject)status.Transaction;

            if (status.Debug)
            {
                log.Debug("Committing Hibernate transaction on Session [" +
                          txObject.SessionHolder.Session + "]");
            }
            try
            {
                txObject.SessionHolder.Transaction.Commit();
            }
            // Note, unfortunate collision of namespaces/classname for NHibernate.TransactionException
            // and Spring.Data.NHibernate requires this wierd construct.
            catch (Exception ex)
            {
                Type nhibTxExceptiontype = TypeResolutionUtils.ResolveType("NHibernate.TransactionException, NHibernate");
                if (ex.GetType().Equals(nhibTxExceptiontype))
                {
                    // assumably from commit call to the underlying ADO.NET connection
                    throw new TransactionSystemException("Could not commit Hibernate transaction", ex);
                }
                HibernateException hibEx = ex as HibernateException;
                if (hibEx != null)
                {
                    // assumably failed to flush changes to database
                    throw ConvertHibernateAccessException(hibEx);
                }
                throw;
            }
        }
Пример #2
0
 /// <summary>
 /// Convert the given HibernateException to an appropriate exception from the
 /// Spring's DAO Exception hierarchy.
 /// Will automatically apply a specified IAdoExceptionTranslator to a
 /// Hibernate ADOException, else rely on Hibernate's default translation.
 /// </summary>
 /// <param name="ex">The Hibernate exception that occured.</param>
 /// <returns>A corresponding DataAccessException</returns>
 protected virtual DataAccessException ConvertHibernateException(HibernateException ex)
 {
     if (ex is ADOException)
     {
         return(ConvertAdoAccessException((ADOException)ex));
     }
     return(SessionFactoryUtils.ConvertHibernateAccessException(ex));
 }
Пример #3
0
        /// <summary>
        /// Convert the given HibernateException to an appropriate exception from the
        /// <code>Spring.Dao</code> hierarchy. Note that it is advisable to
        /// handle AdoException specifically by using a AdoExceptionTranslator for the
        /// underlying ADO.NET exception.
        /// </summary>
        /// <param name="ex">The Hibernate exception that occured.</param>
        /// <returns>DataAccessException instance</returns>
        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));
        }
 /// <summary>
 /// Convert the given HibernateException to an appropriate exception from
 /// the Spring.Dao hierarchy. Can be overridden in subclasses.
 /// </summary>
 /// <param name="ex">The HibernateException that occured.</param>
 /// <returns>The corresponding DataAccessException instance</returns>
 protected virtual DataAccessException ConvertHibernateAccessException(HibernateException ex)
 {
     if (AdoExceptionTranslator != null && ex is ADOException)
     {
         return(ConvertAdoAccessException((ADOException)ex, AdoExceptionTranslator));
     }
     else if (ex is ADOException)
     {
         return(ConvertAdoAccessException((ADOException)ex, DefaultAdoExceptionTranslator));
     }
     return(SessionFactoryUtils.ConvertHibernateAccessException(ex));
 }
Пример #5
0
        private void RollbackShardTransactions()
        {
            if (transactions == null)
            {
                return;
            }

            try
            {
                HibernateException firstRollbackException = null;

                foreach (ITransaction t in transactions)
                {
                    try
                    {
                        if (t.IsActive)
                        {
                            t.Rollback();
                        }
                    }
                    catch (HibernateException he)
                    {
                        Log.Warn("Cannot rollback underlying transaction", he);

                        // we're only going to rethrow the first commit exception we receive
                        if (firstRollbackException == null)
                        {
                            firstRollbackException = he;
                        }
                    }
                }

                if (firstRollbackException != null)
                {
                    throw new TransactionException("Rollback failed", firstRollbackException);
                }
            }
            finally
            {
                DisposeShardTransactions();
                transactions.Clear();
            }
        }
Пример #6
0
        public void SetProxyFactoryFactory(string typeName)
        {
            System.Type pffc;
            try
            {
                pffc = ReflectHelper.ClassForName(typeName);
            }
            catch(HibernateException he)
            {
                throw new HibernateException("Unable to load type '" + typeName + "' during configuration of proxy factory class.",
                                             he);
            }

            if (typeof(IProxyFactoryFactory).IsAssignableFrom(pffc) == false)
            {
                HibernateException he =
                    new HibernateException(pffc.FullName + " does not implement " +
                        typeof(IProxyFactoryFactory).FullName);
                throw he;
            }
            proxyFactoryFactory = pffc;
        }
Пример #7
0
        private void CommitShardTransactions()
        {
            if (transactions == null)
            {
                return;
            }

            HibernateException firstCommitException = null;

            foreach (ITransaction t in transactions)
            {
                try
                {
                    if (t.IsActive)
                    {
                        t.Commit();
                    }
                }
                catch (HibernateException he)
                {
                    Log.Warn("Exception committing underlying transaction", he);

                    // we're only going to rethrow the first commit exception we receive
                    if (firstCommitException == null)
                    {
                        firstCommitException = he;
                    }
                }
            }

            if (firstCommitException != null)
            {
                throw new TransactionException("Commit failed", firstCommitException);
            }

            DisposeShardTransactions();
            transactions.Clear();
        }
        private async Task CommitShardTransactionsAsync(CancellationToken cancellationToken)
        {
            if (transactions == null)
            {
                return;
            }

            HibernateException firstCommitException = null;

            foreach (ITransaction t in transactions)
            {
                try
                {
                    if (t.IsActive)
                    {
                        await t.CommitAsync(cancellationToken);
                    }
                }
                catch (HibernateException he)
                {
                    Log.Warn(he, "Exception committing underlying transaction");

                    // we're only going to rethrow the first commit exception we receive
                    if (firstCommitException == null)
                    {
                        firstCommitException = he;
                    }
                }
            }

            if (firstCommitException != null)
            {
                throw new TransactionException("Commit failed", firstCommitException);
            }

            DisposeShardTransactions();
            transactions.Clear();
        }
Пример #9
0
        public void Commit()
        {
            if (!begun)
            {
                throw new TransactionException("Transaction not succesfully started");
            }
            log.Debug("Starting transaction commit");
            BeforeTransactionCompletion();
            bool commitException = false;
            HibernateException firstCommitException = null;

            foreach (ITransaction t in transactions)
            {
                try
                {
                    t.Commit();
                }
                catch (HibernateException he)
                {
                    log.Warn("exception commiting underlying transaction", he);
                    commitException = true;
                    // we're only going to rethrow the first commit exception we receive
                    if (firstCommitException == null)
                    {
                        firstCommitException = he;
                    }
                }
            }
            if (commitException)
            {
                commitFailed = true;
                //afterTransactionCompletion(Status.STATUS_UNKNOWN);
                throw new TransactionException("Commit failed", firstCommitException);
            }
            //afterTransactionCompletion(Status.STATUS_COMMITTED);
            committed = true;
        }
Пример #10
0
        public void Rollback()
        {
            if (!begun && !commitFailed)
            {
                throw new TransactionException("Transaction not successfully started");
            }
            bool rollbackException = false;
            HibernateException firstRollbackException = null;

            foreach (ITransaction t in transactions)
            {
                if (t.WasCommitted)
                {
                    continue;
                }
                try
                {
                    t.Rollback();
                }
                catch (HibernateException he)
                {
                    log.Warn("exception rolling back underlying transaction", he);
                    rollbackException = true;
                    if (firstRollbackException == null)
                    {
                        firstRollbackException = he;
                    }
                }
            }
            if (rollbackException)
            {
                //we're only going to rethrow the first rollback exception
                throw new TransactionException("Rollback failed", firstRollbackException);
            }
            rolledBack = true;
        }
Пример #11
0
 /// <summary>
 /// Convert the given HibernateException to an appropriate exception from the
 /// <code>org.springframework.dao</code> hierarchy. Will automatically detect
 /// wrapped ADO.NET Exceptions and convert them accordingly.
 /// </summary>
 /// <param name="ex">HibernateException that occured.</param>
 /// <returns>
 /// The corresponding DataAccessException instance
 /// </returns>
 /// <remarks>
 /// The default implementation delegates to SessionFactoryUtils
 /// and convertAdoAccessException. Can be overridden in subclasses.
 /// </remarks>
 protected DataAccessException ConvertHibernateAccessException(HibernateException ex)
 {
     return(hibernateTemplate.ConvertHibernateAccessException(ex));
 }
Пример #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HibernateSystemException"/> class.
 /// </summary>
 /// <param name="cause">The cause.</param>
 public HibernateSystemException(HibernateException cause) : base(cause != null ? cause.Message : null, cause)
 {
 }