Exemple #1
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;

            if (sqle != null)
            {
                switch (sqle.Number)
                {
                case 2601:     // Violation in unique index
                case 2627:     // Violation in unique constraint
                    return(new ConstraintViolationException(exInfo.Message, sqle, exInfo.Sql, null));

                case 547:     // constraint violation (referential integrity)
                {
                    if (exInfo.Message.StartsWith("could not delete"))
                    {
                        return(new CannotDeleteException(exInfo.Message, sqle));
                    }

                    return(new ConstraintViolationException(exInfo.Message, sqle, exInfo.Sql, null));
                }
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException,
                                                                 exInfo.Message, exInfo.Sql));
        }
Exemple #2
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var dbException = ADOExceptionHelper.ExtractDbException(exInfo.SqlException);

            var ns = dbException.GetType().Namespace ?? string.Empty;

            if (ns.ToLowerInvariant().StartsWith("system.data.sqlite"))
            {
                // SQLite exception
                switch (dbException.ErrorCode)
                {
                case -2147467259: // Abort due to constraint violation
                    throw new ConcurrencyException();
                }
            }

            if (ns.ToLowerInvariant().StartsWith("system.data.sqlclient"))
            {
                // MS SQL Server
                switch (dbException.ErrorCode)
                {
                case -2146232060:
                    throw new ConcurrencyException();
                }
            }

            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException,
                                                                 exInfo.Message, exInfo.Sql));
        }
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var dbEx = ADOExceptionHelper.ExtractDbException(exInfo.SqlException);

            if (dbEx is SqlException sqle)
            {
                if (sqle.Number == 547)
                {
                    return(new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null));
                }
                if (sqle.Number == 208)
                {
                    return(new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql));
                }
            }

            if (dbEx is Microsoft.Data.SqlClient.SqlException msSqle)
            {
                if (msSqle.Number == 547)
                {
                    return(new ConstraintViolationException(exInfo.Message, msSqle.InnerException, exInfo.Sql, null));
                }
                if (msSqle.Number == 208)
                {
                    return(new SQLGrammarException(exInfo.Message, msSqle.InnerException, exInfo.Sql));
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
Exemple #4
0
        /// <summary>
        /// Iterates through the provided mappings looking for a matching rule.
        /// If found, the converter function for that mapping is called.
        /// </summary>
        /// <param name="exceptionContext">Available info about the exception
        ///     being thrown.</param>
        public Exception Convert(AdoExceptionContextInfo exceptionContext)
        {
            var mapping = _mappings.FirstOrDefault(m => m.Matcher.Invoke(exceptionContext));

            return(mapping != null
                ? mapping.Converter.Invoke(exceptionContext)
                : SQLStateConverter.HandledNonSpecificException(exceptionContext.SqlException, exceptionContext.Message, exceptionContext.Sql));
        }
Exemple #5
0
        public async Task LockAsync(object id, object version, object obj, ISessionImplementor session, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ISessionFactoryImplementor factory = session.Factory;

            try
            {
                var          st = await(session.Batcher.PrepareCommandAsync(CommandType.Text, sql, lockable.IdAndVersionSqlTypes, cancellationToken)).ConfigureAwait(false);
                DbDataReader rs = null;
                try
                {
                    await(lockable.IdentifierType.NullSafeSetAsync(st, id, 0, session, cancellationToken)).ConfigureAwait(false);
                    if (lockable.IsVersioned)
                    {
                        await(lockable.VersionType.NullSafeSetAsync(st, version, lockable.IdentifierType.GetColumnSpan(factory), session, cancellationToken)).ConfigureAwait(false);
                    }

                    rs = await(session.Batcher.ExecuteReaderAsync(st, cancellationToken)).ConfigureAwait(false);
                    try
                    {
                        if (!await(rs.ReadAsync(cancellationToken)).ConfigureAwait(false))
                        {
                            if (factory.Statistics.IsStatisticsEnabled)
                            {
                                factory.StatisticsImplementor.OptimisticFailure(lockable.EntityName);
                            }
                            throw new StaleObjectStateException(lockable.EntityName, id);
                        }
                    }
                    finally
                    {
                        rs.Close();
                    }
                }
                finally
                {
                    session.Batcher.CloseCommand(st, rs);
                }
            }
            catch (OperationCanceledException) { throw; }
            catch (HibernateException)
            {
                // Do not call Convert on HibernateExceptions
                throw;
            }
            catch (Exception sqle)
            {
                var exceptionContext = new AdoExceptionContextInfo
                {
                    SqlException = sqle,
                    Message      = "could not lock: " + MessageHelper.InfoString(lockable, id, factory),
                    Sql          = sql.ToString(),
                    EntityName   = lockable.EntityName,
                    EntityId     = id
                };
                throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, exceptionContext);
            }
        }
        public void Lock(object id, object version, object obj, ISessionImplementor session)
        {
            ISessionFactoryImplementor factory = session.Factory;

            try
            {
                IDbCommand  st = session.Batcher.PrepareCommand(CommandType.Text, sql, lockable.IdAndVersionSqlTypes);
                IDataReader rs = null;
                try
                {
                    lockable.IdentifierType.NullSafeSet(st, id, 0, session);
                    if (lockable.IsVersioned)
                    {
                        lockable.VersionType.NullSafeSet(st, version, lockable.IdentifierType.GetColumnSpan(factory), session);
                    }

                    rs = session.Batcher.ExecuteReader(st);
                    try
                    {
                        if (!rs.Read())
                        {
                            if (factory.Statistics.IsStatisticsEnabled)
                            {
                                factory.StatisticsImplementor.OptimisticFailure(lockable.EntityName);
                            }
                            throw new StaleObjectStateException(lockable.EntityName, id);
                        }
                    }
                    finally
                    {
                        rs.Close();
                    }
                }
                finally
                {
                    session.Batcher.CloseCommand(st, rs);
                }
            }
            catch (HibernateException)
            {
                // Do not call Convert on HibernateExceptions
                throw;
            }
            catch (Exception sqle)
            {
                var exceptionContext = new AdoExceptionContextInfo
                {
                    SqlException = sqle,
                    Message      = "could not lock: " + MessageHelper.InfoString(lockable, id, factory),
                    Sql          = sql.ToString(),
                    EntityName   = lockable.EntityName,
                    EntityId     = id
                };
                throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, exceptionContext);
            }
        }
Exemple #7
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;

            if ((sqle != null) && (sqle.Number == 3960))
            {
                return(new StaleObjectStateException(exInfo.EntityName, exInfo.EntityId));
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
Exemple #8
0
        public void Lock(object id, object version, object obj, ISessionImplementor session)
        {
            if (!lockable.IsVersioned)
            {
                throw new HibernateException("write locks via update not supported for non-versioned entities [" + lockable.EntityName + "]");
            }
            // todo : should we additionally check the current isolation mode explicitly?
            ISessionFactoryImplementor factory = session.Factory;

            try
            {
                IDbCommand st = session.Batcher.PrepareCommand(CommandType.Text, sql, lockable.IdAndVersionSqlTypes);
                try
                {
                    lockable.VersionType.NullSafeSet(st, version, 1, session);
                    int offset = 2;

                    lockable.IdentifierType.NullSafeSet(st, id, offset, session);
                    offset += lockable.IdentifierType.GetColumnSpan(factory);

                    if (lockable.IsVersioned)
                    {
                        lockable.VersionType.NullSafeSet(st, version, offset, session);
                    }

                    int affected = session.Batcher.ExecuteNonQuery(st);
                    if (affected < 0)
                    {
                        factory.StatisticsImplementor.OptimisticFailure(lockable.EntityName);
                        throw new StaleObjectStateException(lockable.EntityName, id);
                    }
                }
                finally
                {
                    session.Batcher.CloseCommand(st, null);
                }
            }
            catch (HibernateException)
            {
                // Do not call Convert on HibernateExceptions
                throw;
            }
            catch (Exception sqle)
            {
                var exceptionContext = new AdoExceptionContextInfo
                {
                    SqlException = sqle,
                    Message      = "could not lock: " + MessageHelper.InfoString(lockable, id, factory),
                    Sql          = sql.ToString(),
                    EntityName   = lockable.EntityName,
                    EntityId     = id
                };
                throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, exceptionContext);
            }
        }
		public Exception Convert(AdoExceptionContextInfo exInfo)
		{
			SqlException sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;
			if(sqle != null)
			{
				if (sqle.Number == 547)
					return new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null);
				if (sqle.Number == 208)
					return new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql);
			}
			return SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql);
		}
Exemple #10
0
        public Exception Convert(AdoExceptionContextInfo adoExceptionContextInfo)
        {
            var sqlException = adoExceptionContextInfo.SqlException as MySqlException;

            if (sqlException != null)
            {
                if (sqlException.Number == 1062)
                {
                    return(new UniqueConstraintViolationException(sqlException.Message, sqlException));
                }
            }

            return(adoExceptionContextInfo.SqlException);
        }
Exemple #11
0
    public Exception Convert(AdoExceptionContextInfo adoExceptionContextInfo)
    {
        var npgsqlException = (NpgsqlException)adoExceptionContextInfo.SqlException;

        switch (npgsqlException.Code)
        {
        case "<errorcode for duplicate key>":
            return(new WhateverException(...));

            break;

        default:
            break;
        }
    }
Exemple #12
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqlException = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as MySqlException;

            if (sqlException != null)
            {
                switch ((MySqlErrorCode)sqlException.Number)
                {
                case MySqlErrorCode.DuplicateKeyEntry:
                    return(new ConstraintViolationException(exInfo.Message, sqlException.InnerException, exInfo.Sql, null));
                }
            }

            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
Exemple #13
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqlException = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SQLiteException;

            if (sqlException != null)
            {
                switch (sqlException.ErrorCode)
                {
                case 19:
                    return(new ConstraintViolationException(exInfo.Message, sqlException.InnerException, exInfo.Sql, null));
                }
            }

            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
Exemple #14
0
    public Exception Convert(AdoExceptionContextInfo adoExceptionContextInfo)
    {
        var sqlException = adoExceptionContextInfo.SqlException as SqlException;

        if (sqlException != null)
        {
            // 2601 is unique key, 2627 is unique index; same thing:
            // http://blog.sqlauthority.com/2007/04/26/sql-server-difference-between-unique-index-vs-unique-constraint/
            if (sqlException.Number == 2601 || sqlException.Number == 2627)
            {
                return(new UniqueKeyException(sqlException.Message, sqlException));
            }
        }
        return(adoExceptionContextInfo.SqlException);
    }
        public Exception Convert(AdoExceptionContextInfo adoExceptionContextInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(adoExceptionContextInfo.SqlException) as DbException;

            if (sqle != null)
            {
                if (sqle.ErrorCode == 335544466)
                {
                    return(new ConstraintViolationException(adoExceptionContextInfo.Message, sqle.InnerException, adoExceptionContextInfo.Sql, null));
                }
                if (sqle.ErrorCode == 335544569)
                {
                    return(new SQLGrammarException(adoExceptionContextInfo.Message, sqle.InnerException, adoExceptionContextInfo.Sql));
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(adoExceptionContextInfo.SqlException, adoExceptionContextInfo.Message, adoExceptionContextInfo.Sql));
        }
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as OracleException;

            if (sqle != null)
            {
                if (sqle.Code == 1036)
                {
                    return(new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null));
                }
                if (sqle.Code == 942)
                {
                    return(new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql));
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as DbException;

            if (sqle != null)
            {
                string code = (string)sqle.GetType().GetProperty("Code").GetValue(sqle, null);

                if (code == "23503")
                {
                    return(new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null));
                }
                if (code == "42P01")
                {
                    return(new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql));
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
Exemple #18
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;

            if (sqle != null)
            {
                switch (sqle.Number)
                {
                case 547:
                    return(new ConstraintViolationException(exInfo.Message, sqle));

                case 2627:
                    return(new UniquenessViolationException(exInfo.Message, sqle));
                    //case 208:
                    //    return new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql);
                    //case 3960:
                    //    return new StaleObjectStateException(exInfo.EntityName, exInfo.EntityId);
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
Exemple #19
0
 public Exception Convert(AdoExceptionContextInfo exInfo)
 {
     Utilities.Logger.Log("Inside our SqlExceptionConverter: EntityId: {0}. EntityName: {1}\nMessage: {2}.", exInfo.EntityId, exInfo.EntityName, exInfo.Message);
     //var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as MySqlException;
     //if (sqle != null)
     //{
     //    switch (sqle.Number)
     //    {
     //        case 547:
     //            return new ConstraintViolationException(exInfo.Message,
     //                sqle.InnerException, exInfo.Sql, null);
     //        case 208:
     //            return new SQLGrammarException(exInfo.Message,
     //                sqle.InnerException, exInfo.Sql);
     //        case 3960:
     //            return new StaleObjectStateException(exInfo.EntityName, exInfo.EntityId);
     //    }
     //}
     return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException,
                                                          exInfo.Message, exInfo.Sql));
 }
Exemple #20
0
        public Exception Convert(AdoExceptionContextInfo exceptionContext)
        {
            var sqlException = ADOExceptionHelper.ExtractDbException(exceptionContext.SqlException) as SqlException;

            if (sqlException != null)
            {
                switch (sqlException.Number)
                {
                case 2627:
                case 2601:
                case 547:
                    return(new ConstraintViolationException(
                               exceptionContext.SqlException.Message,
                               sqlException.InnerException,
                               exceptionContext.Sql,
                               null
                               ));

                case 208:
                    return(new SQLGrammarException(
                               exceptionContext.SqlException.Message,
                               sqlException.InnerException,
                               exceptionContext.Sql
                               ));

                case 3960:
                    return(new StaleObjectStateException(
                               exceptionContext.EntityName,
                               exceptionContext.EntityId
                               ));
                }
            }

            return(SQLStateConverter.HandledNonSpecificException(
                       exceptionContext.SqlException,
                       exceptionContext.Message,
                       exceptionContext.Sql
                       ));
        }
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;

            if (sqle != null)
            {
                switch (sqle.Number)
                {
                case 17:
                // SQL Server does not exist or access denied.
                case 4060:
                // Invalid Database
                case 18456:
                    // Login Failed
                    return(new DatabaseException(sqle.Message, sqle));

                case 547:
                    // ForeignKey Violation
                    return(new ConstraintException(_ParseConstraintName(sqle.Message), sqle));

                case 1205:
                    // DeadLock Victim
                    return(new DatabaseException(sqle.Message, sqle));

                case 2627:
                case 2601:
                    // Unique Index/Constriant Violation
                    return(new ConstraintException(_ParseConstraintName(sqle.Message), sqle));

                default:
                    // throw a general DAL Exception
                    return(new DatabaseException(sqle.Message, sqle));
                }
            }

            return(SQLStateConverter.HandledNonSpecificException(
                       exInfo.SqlException,
                       exInfo.Message, exInfo.Sql));
        }
            public Exception Convert(AdoExceptionContextInfo contextInfo)
            {
                Exception result = null;
                var       sqle   = ADOExceptionHelper.ExtractDbException(contextInfo.SqlException) as SqlException;

                if (sqle != null)
                {
                    switch (sqle.Number)
                    {
                    case 547:
                        result = new ConstraintViolationException(
                            sqle.Message,
                            sqle,
                            contextInfo.Sql,
                            null);
                        break;

                    case 208:
                        result = new SQLGrammarException(
                            contextInfo.Message,
                            sqle,
                            contextInfo.Sql);
                        break;

                    case 3960:
                        result = new StaleObjectStateException(
                            contextInfo.EntityName,
                            contextInfo.EntityId);
                        break;
                    }
                }

                return(result ?? SQLStateConverter.HandledNonSpecificException(
                           contextInfo.SqlException,
                           contextInfo.Message,
                           contextInfo.Sql));
            }
Exemple #23
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;

            if (sqle != null)
            {
                switch (sqle.Number)
                {
                case -2:          // timeout
                case -2147217871: // timeout
                case 11:          // network error
                case 1205:        // deadlock
                    return(new TransientErrorException("Temporary db error occured. Try again later.", sqle));

                // case 208: // sql grammar
                // case 547: // constraint violation
                // case 3960: // stale object state (does not occur with ReadCommitted isolation level). Should trigger reload on client side
                default:
                    return(new ApplicationException("DB error", sqle));
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException,
                                                                 exInfo.Message, exInfo.Sql));
        }
Exemple #24
0
        public Task LockAsync(object id, object version, object obj, ISessionImplementor session, CancellationToken cancellationToken)
        {
            if (!lockable.IsVersioned)
            {
                throw new HibernateException("write locks via update not supported for non-versioned entities [" + lockable.EntityName + "]");
            }
            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCanceled <object>(cancellationToken));
            }
            return(InternalLockAsync());

            async Task InternalLockAsync()
            {
                // todo : should we additionally check the current isolation mode explicitly?
                ISessionFactoryImplementor factory = session.Factory;

                try
                {
                    var st = await(session.Batcher.PrepareCommandAsync(CommandType.Text, sql, lockable.IdAndVersionSqlTypes, cancellationToken)).ConfigureAwait(false);
                    try
                    {
                        await(lockable.VersionType.NullSafeSetAsync(st, version, 1, session, cancellationToken)).ConfigureAwait(false);
                        int offset = 2;

                        await(lockable.IdentifierType.NullSafeSetAsync(st, id, offset, session, cancellationToken)).ConfigureAwait(false);
                        offset += lockable.IdentifierType.GetColumnSpan(factory);

                        if (lockable.IsVersioned)
                        {
                            await(lockable.VersionType.NullSafeSetAsync(st, version, offset, session, cancellationToken)).ConfigureAwait(false);
                        }

                        int affected = await(session.Batcher.ExecuteNonQueryAsync(st, cancellationToken)).ConfigureAwait(false);
                        if (affected < 0)
                        {
                            factory.StatisticsImplementor.OptimisticFailure(lockable.EntityName);
                            throw new StaleObjectStateException(lockable.EntityName, id);
                        }
                    }
                    finally
                    {
                        session.Batcher.CloseCommand(st, null);
                    }
                }
                catch (HibernateException)
                {
                    // Do not call Convert on HibernateExceptions
                    throw;
                }
                catch (Exception sqle)
                {
                    var exceptionContext = new AdoExceptionContextInfo
                    {
                        SqlException = sqle,
                        Message      = "could not lock: " + MessageHelper.InfoString(lockable, id, factory),
                        Sql          = sql.ToString(),
                        EntityName   = lockable.EntityName,
                        EntityId     = id
                    };
                    throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, exceptionContext);
                }
            }
        }
Exemple #25
0
 /// <summary>
 /// Called by NHibernate when an exception occurs. Uses the
 /// SqlExceptionMapper to determine if and how the exception is
 /// converted.
 /// </summary>
 /// <param name="exceptionContext">Available info about the exception being thrown.</param>
 public Exception Convert(AdoExceptionContextInfo exceptionContext)
 {
     return(_mapper.Convert(exceptionContext));
 }
Exemple #26
0
 public Exception Convert(AdoExceptionContextInfo adoExceptionContextInfo)
 {
     return(new UnitTestException());
 }