Skip to content

Commit 747dc7a

Browse files
NH-4013 - Guarantee cleanup on failed sql execution.
1 parent 325453e commit 747dc7a

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

src/NHibernate/AdoNet/MySqlClientBatchingBatcher.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,31 @@ public override void AddToBatch(IExpectation expectation)
6969

7070
protected override void DoExecuteBatch(DbCommand ps)
7171
{
72-
Log.DebugFormat("Executing batch");
73-
CheckReaders();
74-
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
75-
{
76-
Factory.Settings.SqlStatementLogger.LogBatchCommand(currentBatchCommandsLog.ToString());
77-
}
78-
79-
int rowsAffected;
8072
try
8173
{
82-
rowsAffected = currentBatch.ExecuteNonQuery();
74+
Log.DebugFormat("Executing batch");
75+
CheckReaders();
76+
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
77+
{
78+
Factory.Settings.SqlStatementLogger.LogBatchCommand(currentBatchCommandsLog.ToString());
79+
}
80+
81+
int rowsAffected;
82+
try
83+
{
84+
rowsAffected = currentBatch.ExecuteNonQuery();
85+
}
86+
catch (DbException e)
87+
{
88+
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
89+
}
90+
91+
Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected);
8392
}
84-
catch (DbException e)
93+
finally
8594
{
86-
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
95+
ClearCurrentBatch();
8796
}
88-
89-
Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected);
90-
91-
ClearCurrentBatch();
9297
}
9398

9499
private MySqlClientSqlCommandSet CreateConfiguredBatch()

src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public override void AddToBatch(IExpectation expectation)
6161
{
6262
Log.Debug("Adding to batch:" + lineWithParameters);
6363
}
64-
_currentBatch.Append((System.Data.SqlClient.SqlCommand) batchUpdate);
64+
_currentBatch.Append((System.Data.SqlClient.SqlCommand)batchUpdate);
6565

6666
if (_currentBatch.CountOfCommands >= _batchSize)
6767
{
@@ -71,27 +71,31 @@ public override void AddToBatch(IExpectation expectation)
7171

7272
protected override void DoExecuteBatch(DbCommand ps)
7373
{
74-
Log.DebugFormat("Executing batch");
75-
CheckReaders();
76-
Prepare(_currentBatch.BatchCommand);
77-
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
78-
{
79-
Factory.Settings.SqlStatementLogger.LogBatchCommand(_currentBatchCommandsLog.ToString());
80-
}
81-
82-
int rowsAffected;
8374
try
8475
{
85-
rowsAffected = _currentBatch.ExecuteNonQuery();
76+
Log.DebugFormat("Executing batch");
77+
CheckReaders();
78+
Prepare(_currentBatch.BatchCommand);
79+
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
80+
{
81+
Factory.Settings.SqlStatementLogger.LogBatchCommand(_currentBatchCommandsLog.ToString());
82+
}
83+
int rowsAffected;
84+
try
85+
{
86+
rowsAffected = _currentBatch.ExecuteNonQuery();
87+
}
88+
catch (DbException e)
89+
{
90+
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
91+
}
92+
93+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected);
8694
}
87-
catch (DbException e)
95+
finally
8896
{
89-
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
97+
ClearCurrentBatch();
9098
}
91-
92-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected);
93-
94-
ClearCurrentBatch();
9599
}
96100

97101
private SqlClientSqlCommandSet CreateConfiguredBatch()

0 commit comments

Comments
 (0)