Beispiel #1
0
        /// <summary>
        /// Translate the given <see cref="System.SystemException"/> into a generic data access exception.
        /// </summary>
        /// <param name="task">A readable string describing the task being attempted.</param>
        /// <param name="sql">The SQL query or update that caused the problem. May be null.</param>
        /// <param name="exception">
        /// The <see cref="System.Exception"/> encountered by the ADO.NET implementation.
        /// </param>
        /// <returns>
        /// A <see cref="Spring.Dao.DataAccessException"/> appropriate for the supplied
        /// <paramref name="exception"/>.
        /// </returns>
        public virtual DataAccessException Translate(string task, string sql, Exception exception)
        {
            if (task == null)
            {
                task = "";
            }
            if (sql == null)
            {
                sql = "";
            }
            string errorCode = ExtractErrorCode(exception);

            DataAccessException dex = DoTranslate(task, sql, errorCode, exception);

            if (dex != null)
            {
                // Specific exception match found.
                return(dex);
            }
            // Looking for a fallback...
            if (log.IsDebugEnabled)
            {
                log.Debug("Unable to translate exception with errorCode '" + errorCode + "', will use the fallback translator");
            }
            IAdoExceptionTranslator fallback = FallbackTranslator;

            if (fallback != null)
            {
                return(FallbackTranslator.Translate(task, sql, exception));
            }
            // We couldn't identify it more precisely.
            return(new UncategorizedAdoException(task, sql, errorCode, exception));
        }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpringSessionSynchronization"/> class.
 /// </summary>
 public SpringSessionSynchronization(SessionHolder sessionHolder, ISessionFactory sessionFactory,
                                     IAdoExceptionTranslator adoExceptionTranslator, bool newSession)
 {
     this.sessionHolder          = sessionHolder;
     this.sessionFactory         = sessionFactory;
     this.adoExceptionTranslator = adoExceptionTranslator;
     this.newSession             = newSession;
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="SpringSessionSynchronization"/> class.
        /// </summary>
        public 	SpringSessionSynchronization(SessionHolder sessionHolder, ISessionFactory sessionFactory,
               	                             IAdoExceptionTranslator adoExceptionTranslator, bool newSession)
		{
            this.sessionHolder = sessionHolder;
            this.sessionFactory = sessionFactory;
            this.adoExceptionTranslator = adoExceptionTranslator;
            this.newSession = newSession;
		}
        private void CheckTranslation(IAdoExceptionTranslator translator, string errorCode, Type exType)
        {
            TestSqlException    sex = new TestSqlException("", errorCode);
            DataAccessException ex  = translator.Translate("", "", sex);

            Assert.IsTrue(exType.IsAssignableFrom(ex.GetType()));
            Assert.IsTrue(ex.InnerException == sex);
        }
        public void ExceptionTranslator()
        {
            ISessionFactory         sessionFactory = ctx["SessionFactory"] as ISessionFactory;
            HibernateTemplate       template       = new HibernateTemplate(sessionFactory);
            IAdoExceptionTranslator translator     = template.AdoExceptionTranslator;

            Assert.IsNotNull(translator, "ADO.NET exception translator should not be null");

            Assert.That(translator, Is.InstanceOf(typeof(ErrorCodeExceptionTranslator)));
        }
 private static ISession GetSession(
     ISessionFactory sessionFactory, IInterceptor entityInterceptor,
     IAdoExceptionTranslator adoExceptionTranslator, bool allowCreate)
 {
     try
     {
         return(DoGetSession(sessionFactory, entityInterceptor, adoExceptionTranslator, allowCreate));
     }
     catch (HibernateException ex)
     {
         throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
     }
 }
 /// <summary>
 /// Converts a Hibernate ADOException to a Spring DataAccessExcption, extracting the underlying error code from
 /// ADO.NET.  Will extract the ADOException Message and SqlString properties and pass them to the translate method
 /// of the provided IAdoExceptionTranslator.
 /// </summary>
 /// <param name="translator">The IAdoExceptionTranslator, may be a user provided implementation as configured on
 /// HibernateTemplate.
 /// </param>
 /// <param name="ex">The ADOException throw</param>
 /// <returns>The translated DataAccessException or UncategorizedAdoException in case of an error in translation
 /// itself.</returns>
 public static DataAccessException ConvertAdoAccessException(IAdoExceptionTranslator translator, ADOException ex)
 {
     try
     {
         string sqlString = (ex.SqlString != null)
                                ? ex.SqlString.ToString()
                                : string.Empty;
         return(translator.Translate(
                    "Hibernate operation: " + ex.Message, sqlString, ex.InnerException));
     } catch (Exception e)
     {
         log.Error("Exception thrown during exception translation. Message = [" + e.Message + "]", e);
         log.Error("Exception that was attempted to be translated was [" + ex.Message + "]", ex);
         if (ex.InnerException != null)
         {
             log.Error("  Inner Exception was [" + ex.InnerException.Message + "]", ex.InnerException);
         }
         throw new UncategorizedAdoException(e.Message, "", "", e);
     }
 }
 /// <summary>
 /// Convert the given ADOException to an appropriate exception from the
 /// the Spring.Dao hierarchy. Can be overridden in subclasses.
 /// </summary>
 /// <param name="ex">The ADOException that occured, wrapping the underlying
 /// ADO.NET thrown exception.</param>
 /// <param name="translator">The translator to convert hibernate ADOExceptions.</param>
 /// <returns>
 /// The corresponding DataAccessException instance
 /// </returns>
 protected virtual DataAccessException ConvertAdoAccessException(ADOException ex, IAdoExceptionTranslator translator)
 {
     return(translator.Translate("Hibernate flushing: " + ex.Message, null, ex.InnerException));
 }
 /// <summary>
 /// Converts a Hibernate ADOException to a Spring DataAccessExcption, extracting the underlying error code from 
 /// ADO.NET.  Will extract the ADOException Message and SqlString properties and pass them to the translate method
 /// of the provided IAdoExceptionTranslator.
 /// </summary>
 /// <param name="translator">The IAdoExceptionTranslator, may be a user provided implementation as configured on
 /// HibernateTemplate.
 /// </param>
 /// <param name="ex">The ADOException throw</param>
 /// <returns>The translated DataAccessException or UncategorizedAdoException in case of an error in translation
 /// itself.</returns>
 public static DataAccessException ConvertAdoAccessException(IAdoExceptionTranslator translator, ADOException ex)
 {
     try
     {
         string sqlString = (ex.SqlString != null)
                                ? ex.SqlString.ToString()
                                : string.Empty;
         return translator.Translate(
             "Hibernate operation: " + ex.Message, sqlString, ex.InnerException);
     } catch (Exception e)
     {
         log.Error("Exception thrown during exception translation. Message = [" + e.Message + "]", e);
         log.Error("Exception that was attempted to be translated was [" + ex.Message + "]", ex);                
         if (ex.InnerException != null)
         {
             log.Error("  Inner Exception was [" + ex.InnerException.Message + "]", ex.InnerException);
         }
         throw new UncategorizedAdoException(e.Message, "", "", e);
     }
 }
Beispiel #10
0
        private static ISession DoGetSession(
            ISessionFactory sessionFactory, IInterceptor entityInterceptor,
            IAdoExceptionTranslator adoExceptionTranslator, bool allowCreate)
        {
            AssertUtils.ArgumentNotNull(sessionFactory, "sessionFactory", "SessionFactory can not be null");

            SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.GetResource(sessionFactory);
            if (sessionHolder != null && !sessionHolder.IsEmpty) 
            {
                // pre-bound Hibernate Session
                ISession session = null;
                if (TransactionSynchronizationManager.SynchronizationActive &&
                    sessionHolder.DoesNotHoldNonDefaultSession)
                {
                    // Spring transaction management is active ->
                    // register pre-bound Session with it for transactional flushing.
                    session = sessionHolder.ValidatedSession;
                    if (session != null && !sessionHolder.SynchronizedWithTransaction) 
                    {
                        log.Debug("Registering Spring transaction synchronization for existing Hibernate Session");
                        TransactionSynchronizationManager.RegisterSynchronization(
                            new SpringSessionSynchronization(sessionHolder, sessionFactory, adoExceptionTranslator, false));
                        sessionHolder.SynchronizedWithTransaction = true;
                        // Switch to FlushMode.AUTO if we're not within a read-only transaction.
                        FlushMode flushMode = session.FlushMode;
                        if (FlushMode.Never == flushMode &&
                            !TransactionSynchronizationManager.CurrentTransactionReadOnly) 
                        {
                            session.FlushMode = FlushMode.Auto;
                            sessionHolder.PreviousFlushMode = flushMode;
                        }
                    }
                }
                else
                {
                   // No Spring transaction management active -> simply return default thread-bound Session, if any
                   // (possibly from OpenSessionInViewModule)
                    session = sessionHolder.ValidatedSession;
                }
                
                if (session != null) 
                {
                    return session;
                }
                
            }


            ISession sess = OpenSession(sessionFactory, entityInterceptor);
            // Set Session to FlushMode.Never if we're within a read-only transaction.
            // Use same Session for further Hibernate actions within the transaction.
            // Thread object will get removed by synchronization at transaction completion.
            if (TransactionSynchronizationManager.SynchronizationActive) 
            {
                log.Debug("Registering Spring transaction synchronization for new Hibernate Session");
                SessionHolder holderToUse = sessionHolder;
                if (holderToUse == null) 
                {
                    holderToUse = new SessionHolder(sess);
                }
                else 
                {
                    holderToUse.AddSession(sess);
                }
                if (TransactionSynchronizationManager.CurrentTransactionReadOnly) 
                {
                    sess.FlushMode = FlushMode.Never;
                }
                TransactionSynchronizationManager.RegisterSynchronization(
                    new SpringSessionSynchronization(holderToUse, sessionFactory, adoExceptionTranslator, true));
                holderToUse.SynchronizedWithTransaction = true;
                if (holderToUse != sessionHolder) 
                {
                    TransactionSynchronizationManager.BindResource(sessionFactory, holderToUse);
                }
            }

            

            // Check whether we are allowed to return the Session.
            if (!allowCreate && !IsSessionTransactional(sess, sessionFactory)) 
            {
                CloseSession(sess);
                throw new InvalidOperationException ("No Hibernate Session bound to thread, " +
                    "and configuration does not allow creation of non-transactional one here");
            }

            return sess;


        }
Beispiel #11
0
 private static ISession GetSession(
     ISessionFactory sessionFactory, IInterceptor entityInterceptor,
     IAdoExceptionTranslator adoExceptionTranslator, bool allowCreate)
 {
     try
     {
         return DoGetSession(sessionFactory, entityInterceptor, adoExceptionTranslator, allowCreate);
     }
     catch (HibernateException ex)
     {
         throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
     }
 }
 /// <summary>
 /// Convert the given ADOException to an appropriate exception from the
 /// the Spring.Dao hierarchy. Can be overridden in subclasses.
 /// </summary>
 /// <param name="ex">The ADOException that occured, wrapping the underlying
 /// ADO.NET thrown exception.</param>
 /// <param name="translator">The translator to convert hibernate ADOExceptions.</param>
 /// <returns>
 /// The corresponding DataAccessException instance
 /// </returns>
 protected virtual DataAccessException ConvertAdoAccessException(ADOException ex, IAdoExceptionTranslator translator)
 {
     return translator.Translate("Hibernate flusing: " + ex.Message, null, ex.InnerException);
 }
Beispiel #13
0
 protected virtual void InitExceptionTranslator()
 {
     if (exceptionTranslator == null)
     {
         IDbProvider provider = DbProvider;
         if (provider != null)
         {
             exceptionTranslator = new ErrorCodeExceptionTranslator(provider);
         }
         else
         {
             exceptionTranslator = new FallbackExceptionTranslator();
         }
     }
 }
        private static ISession DoGetSession(
            ISessionFactory sessionFactory, IInterceptor entityInterceptor,
            IAdoExceptionTranslator adoExceptionTranslator, bool allowCreate)
        {
            AssertUtils.ArgumentNotNull(sessionFactory, "sessionFactory", "SessionFactory can not be null");

            SessionHolder sessionHolder = (SessionHolder)TransactionSynchronizationManager.GetResource(sessionFactory);

            if (sessionHolder != null && !sessionHolder.IsEmpty)
            {
                // pre-bound Hibernate Session
                ISession session = null;
                if (TransactionSynchronizationManager.SynchronizationActive &&
                    sessionHolder.DoesNotHoldNonDefaultSession)
                {
                    // Spring transaction management is active ->
                    // register pre-bound Session with it for transactional flushing.
                    session = sessionHolder.ValidatedSession;
                    if (!sessionHolder.SynchronizedWithTransaction)
                    {
                        log.Debug("Registering Spring transaction synchronization for existing Hibernate Session");
                        TransactionSynchronizationManager.RegisterSynchronization(
                            new SpringSessionSynchronization(sessionHolder, sessionFactory, adoExceptionTranslator, false));
                        sessionHolder.SynchronizedWithTransaction = true;
                        // Switch to FlushMode.AUTO if we're not within a read-only transaction.
                        FlushMode flushMode = session.FlushMode;
                        if (FlushMode.Never == flushMode &&
                            !TransactionSynchronizationManager.CurrentTransactionReadOnly)
                        {
                            session.FlushMode = FlushMode.Auto;
                            sessionHolder.PreviousFlushMode = flushMode;
                        }
                    }
                }
                else
                {
                    // No Spring transaction management active -> simply return default thread-bound Session, if any
                    // (possibly from OpenSessionInViewModule)
                    session = sessionHolder.ValidatedSession;
                }

                if (session != null)
                {
                    return(session);
                }
            }


            ISession sess = OpenSession(sessionFactory, entityInterceptor);

            // Set Session to FlushMode.Never if we're within a read-only transaction.
            // Use same Session for further Hibernate actions within the transaction.
            // Thread object will get removed by synchronization at transaction completion.
            if (TransactionSynchronizationManager.SynchronizationActive)
            {
                log.Debug("Registering Spring transaction synchronization for new Hibernate Session");
                SessionHolder holderToUse = sessionHolder;
                if (holderToUse == null)
                {
                    holderToUse = new SessionHolder(sess);
                }
                else
                {
                    holderToUse.AddSession(sess);
                }
                if (TransactionSynchronizationManager.CurrentTransactionReadOnly)
                {
                    sess.FlushMode = FlushMode.Never;
                }
                TransactionSynchronizationManager.RegisterSynchronization(
                    new SpringSessionSynchronization(holderToUse, sessionFactory, adoExceptionTranslator, true));
                holderToUse.SynchronizedWithTransaction = true;
                if (holderToUse != sessionHolder)
                {
                    TransactionSynchronizationManager.BindResource(sessionFactory, holderToUse);
                }
            }



            // Check whether we are allowed to return the Session.
            if (!allowCreate && !IsSessionTransactional(sess, sessionFactory))
            {
                CloseSession(sess);
                throw new InvalidOperationException("No Hibernate Session bound to thread, " +
                                                    "and configuration does not allow creation of non-transactional one here");
            }

            return(sess);
        }
	    private void CheckTranslation(IAdoExceptionTranslator translator, string errorCode, Type exType)
	    {
            TestSqlException sex = new TestSqlException("", errorCode);
            DataAccessException ex = translator.Translate("", "", sex);
            Assert.IsTrue(exType.IsAssignableFrom(ex.GetType()));
            Assert.IsTrue(ex.InnerException == sex);
	    }