@@ -61,7 +61,7 @@ public override void AddToBatch(IExpectation expectation)
61
61
{
62
62
Log . Debug ( "Adding to batch:" + lineWithParameters ) ;
63
63
}
64
- _currentBatch . Append ( ( System . Data . SqlClient . SqlCommand ) batchUpdate ) ;
64
+ _currentBatch . Append ( ( System . Data . SqlClient . SqlCommand ) batchUpdate ) ;
65
65
66
66
if ( _currentBatch . CountOfCommands >= _batchSize )
67
67
{
@@ -71,30 +71,31 @@ public override void AddToBatch(IExpectation expectation)
71
71
72
72
protected override void DoExecuteBatch ( DbCommand ps )
73
73
{
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
- _currentBatchCommandsLog = new StringBuilder ( ) . AppendLine ( "Batch commands:" ) ;
81
- }
82
-
83
- int rowsAffected ;
84
74
try
85
75
{
86
- 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 ) ;
87
94
}
88
- catch ( DbException e )
95
+ finally
89
96
{
90
- throw ADOExceptionHelper . Convert ( Factory . SQLExceptionConverter , e , "could not execute batch command." ) ;
97
+ ClearCurrentBatch ( ) ;
91
98
}
92
-
93
- Expectations . VerifyOutcomeBatched ( _totalExpectedRowsAffected , rowsAffected ) ;
94
-
95
- _currentBatch . Dispose ( ) ;
96
- _totalExpectedRowsAffected = 0 ;
97
- _currentBatch = CreateConfiguredBatch ( ) ;
98
99
}
99
100
100
101
private SqlClientSqlCommandSet CreateConfiguredBatch ( )
@@ -118,18 +119,45 @@ private SqlClientSqlCommandSet CreateConfiguredBatch()
118
119
return result ;
119
120
}
120
121
122
+ private void ClearCurrentBatch ( )
123
+ {
124
+ _currentBatch . Dispose ( ) ;
125
+ _totalExpectedRowsAffected = 0 ;
126
+ _currentBatch = CreateConfiguredBatch ( ) ;
127
+
128
+ if ( Factory . Settings . SqlStatementLogger . IsDebugEnabled )
129
+ {
130
+ _currentBatchCommandsLog = new StringBuilder ( ) . AppendLine ( "Batch commands:" ) ;
131
+ }
132
+ }
133
+
121
134
public override void CloseCommands ( )
122
135
{
123
136
base . CloseCommands ( ) ;
124
137
138
+ // Prevent exceptions when closing the batch from hiding any original exception
139
+ // (We do not know here if this batch closing occurs after a failure or not.)
140
+ try
141
+ {
142
+ ClearCurrentBatch ( ) ;
143
+ }
144
+ catch ( Exception e )
145
+ {
146
+ Log . Warn ( "Exception clearing batch" , e ) ;
147
+ }
148
+ }
149
+
150
+ protected override void Dispose ( bool isDisposing )
151
+ {
152
+ base . Dispose ( isDisposing ) ;
153
+ // Prevent exceptions when closing the batch from hiding any original exception
154
+ // (We do not know here if this batch closing occurs after a failure or not.)
125
155
try
126
156
{
127
157
_currentBatch . Dispose ( ) ;
128
158
}
129
159
catch ( Exception e )
130
160
{
131
- // Prevent exceptions when closing the batch from hiding any original exception
132
- // (We do not know here if this batch closing occurs after a failure or not.)
133
161
Log . Warn ( "Exception closing batcher" , e ) ;
134
162
}
135
163
}
0 commit comments