Example #1
0
        private Exception createCommandException(DbCommand command, Exception innerException)
        {
            if (databaseInfo is SqlServerInfo && innerException is SqlException)
            {
                var errorNumber = ((SqlException)innerException).Number;

                // 1205 is the code for deadlock; 3960 is the code for a snapshot optimistic concurrency error; 3961 is the code for a snapshot concurrency error due to
                // a DDL statement in another transaction.
                if (errorNumber == 1205 || errorNumber == 3960 || errorNumber == 3961)
                {
                    return(new DbConcurrencyException(getCommandExceptionMessage(command, "A concurrency error occurred."), innerException));
                }

                // Failed to update database * because the database is read-only. This happens when you try to make a change to a live installation on a standby server.
                if (errorNumber == 3906 && Configuration.ConfigurationStatics.MachineIsStandbyServer)
                {
                    return(DataAccessMethods.CreateStandbyServerModificationException());
                }

                // -2 is the code for a timeout.
                if (errorNumber == -2)
                {
                    return(new DbCommandTimeoutException(getCommandExceptionMessage(command, "A command timeout occurred."), innerException));
                }

                // We also handle this error at the connection level.
                if (errorNumber == 233)
                {
                    const string m =
                        "The connection with the server has probably been severed. This likely happened because we did not disable connection pooling and a connection was taken from the pool that was no longer valid.";
                    return(new ApplicationException(getCommandExceptionMessage(command, m), innerException));
                }

                return(new ApplicationException(getCommandExceptionMessage(command, "Error number: " + errorNumber + "."), innerException));
            }

            if (databaseInfo is OracleInfo)
            {
                // ORA-00060 is the code for deadlock. ORA-08177 happens when we attempt to update a row that has changed since the transaction began.
                if (new[] { "ORA-00060", "ORA-08177" }.Any(i => innerException.Message.Contains(i)))
                {
                    return(new DbConcurrencyException(getCommandExceptionMessage(command, "A concurrency error occurred."), innerException));
                }

                // This has happened on RLE servers when Dave Foss has manually shut down Oracle.
                if (innerException.Message.Contains("ORA-01109"))
                {
                    return(new DbConnectionFailureException(getCommandExceptionMessage(command, "Failed to connect to Oracle."), innerException));
                }
            }

            return(new ApplicationException(getCommandExceptionMessage(command, ""), innerException));
        }
Example #2
0
 private string getCommandExceptionMessage(DbCommand command, string customMessage)
 {
     using (var sw = new StringWriter()) {
         sw.WriteLine(
             "Failed to execute a command against the " + DataAccessMethods.GetDbName(databaseInfo) + " database. " + customMessage + " Command details:");
         sw.WriteLine();
         sw.WriteLine("Type: " + command.CommandType);
         sw.WriteLine("Text: " + command.CommandText);
         sw.WriteLine();
         sw.WriteLine("Parameters:");
         foreach (DbParameter p in command.Parameters)
         {
             sw.WriteLine(p.ParameterName + ": " + p.Value);
         }
         return(sw.ToString());
     }
 }
Example #3
0
 private Exception createConnectionException(string action, Exception innerException)
 {
     return(DataAccessMethods.CreateDbConnectionException(databaseInfo, action, innerException));
 }