From 1beef0a7d6441546d49706b185061439164afe04 Mon Sep 17 00:00:00 2001 From: John Zabroski Date: Fri, 14 Aug 2015 17:09:35 -0400 Subject: [PATCH] Respect transaction flag, rollback on error. Fixes issues #209 and cleans up fixes in #173. --- .../roundhouse/databases/AdoNetDatabase.cs | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/product/roundhouse/databases/AdoNetDatabase.cs b/product/roundhouse/databases/AdoNetDatabase.cs index d8177309..70ce40e7 100644 --- a/product/roundhouse/databases/AdoNetDatabase.cs +++ b/product/roundhouse/databases/AdoNetDatabase.cs @@ -130,16 +130,32 @@ protected override void run_sql(string sql_to_run, ConnectionType connection_typ } catch (SqlException ex) { - if (ex.Number == sql_connection_exception_number) + // If we are not running inside a transaction, then we can continue to the next command. + if (transaction == null) + { + // But only if it's a connection failure AND connection failure is the only error reported. + if (ex.Errors.Count == 1 && ex.Number == sql_connection_exception_number) { - Log.bound_to(this).log_a_debug_event_containing("Failure executing command, trying again. {0}{1}", Environment.NewLine, ex.ToString()); - run_command_with(sql_to_run, connection_type, parameters); + Log.bound_to(this).log_a_debug_event_containing("Failure executing command, trying again. {0}{1}", Environment.NewLine, ex.ToString()); + run_command_with(sql_to_run, connection_type, parameters); } else { - //Re-throw the original exception. - throw; + //Re-throw the original exception. + throw; } + } + else + { + // Re-throw the exception, which will delegate handling of the rollback to DatabaseMigrator calling class, + // e.g. DefaultDatabaseMigrator.run_sql(...) method catches exceptions from run_sql and rolls back the transaction. + throw; + } + } + catch (Exception ex) + { + // If the Exception is not due to a SqlException, which is the case for any non-SqlServer database, then also delegate handling of the rollback to DatabaseMigrator calling class. + throw; } }