@@ -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,30 +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- _currentBatchCommandsLog = new StringBuilder ( ) . AppendLine ( "Batch commands:" ) ;
81- }
82-
83- int rowsAffected ;
8474 try
8575 {
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 ) ;
8794 }
88- catch ( DbException e )
95+ finally
8996 {
90- throw ADOExceptionHelper . Convert ( Factory . SQLExceptionConverter , e , "could not execute batch command." ) ;
97+ ClearCurrentBatch ( ) ;
9198 }
92-
93- Expectations . VerifyOutcomeBatched ( _totalExpectedRowsAffected , rowsAffected ) ;
94-
95- _currentBatch . Dispose ( ) ;
96- _totalExpectedRowsAffected = 0 ;
97- _currentBatch = CreateConfiguredBatch ( ) ;
9899 }
99100
100101 private SqlClientSqlCommandSet CreateConfiguredBatch ( )
@@ -118,18 +119,45 @@ private SqlClientSqlCommandSet CreateConfiguredBatch()
118119 return result ;
119120 }
120121
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+
121134 public override void CloseCommands ( )
122135 {
123136 base . CloseCommands ( ) ;
124137
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.)
125155 try
126156 {
127157 _currentBatch . Dispose ( ) ;
128158 }
129159 catch ( Exception e )
130160 {
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.)
133161 Log . Warn ( "Exception closing batcher" , e ) ;
134162 }
135163 }
0 commit comments