@@ -36,7 +36,7 @@ internal class FbConnectionInternal : IDisposable
3636
3737 private IDatabase _db ;
3838 private FbTransaction _activeTransaction ;
39- private List < WeakReference < FbCommand > > _preparedCommands ;
39+ private HashSet < FbCommand > _preparedCommands ;
4040 private FbConnectionString _options ;
4141 private FbConnection _owningConnection ;
4242 private bool _disposed ;
@@ -96,7 +96,7 @@ public FbConnectionString Options
9696
9797 public FbConnectionInternal ( FbConnectionString options )
9898 {
99- _preparedCommands = new List < WeakReference < FbCommand > > ( ) ;
99+ _preparedCommands = new HashSet < FbCommand > ( ) ;
100100
101101 _options = options ;
102102 }
@@ -246,11 +246,8 @@ public void DisposeTransaction()
246246
247247 public void TransactionCompleted ( )
248248 {
249- for ( var i = 0 ; i < _preparedCommands . Count ; i ++ )
249+ foreach ( var command in _preparedCommands )
250250 {
251- if ( ! _preparedCommands [ i ] . TryGetTarget ( out var command ) )
252- continue ;
253-
254251 if ( command . Transaction != null )
255252 {
256253 command . DisposeReader ( ) ;
@@ -336,54 +333,26 @@ public DataTable GetSchema(string collectionName, string[] restrictions)
336333
337334 public void AddPreparedCommand ( FbCommand command )
338335 {
339- var position = - 1 ;
340- for ( var i = 0 ; i < _preparedCommands . Count ; i ++ )
341- {
342- if ( ! _preparedCommands [ i ] . TryGetTarget ( out var current ) )
343- {
344- position = i ;
345- }
346- else
347- {
348- if ( current == command )
349- {
350- return ;
351- }
352- }
353- }
354- if ( position != - 1 )
355- {
356- _preparedCommands [ position ] . SetTarget ( command ) ;
357- }
358- else
359- {
360- _preparedCommands . Add ( new WeakReference < FbCommand > ( command ) ) ;
361- }
336+ if ( _preparedCommands . Contains ( command ) )
337+ return ;
338+ _preparedCommands . Add ( command ) ;
362339 }
363340
364341 public void RemovePreparedCommand ( FbCommand command )
365342 {
366- for ( var i = _preparedCommands . Count - 1 ; i >= 0 ; i -- )
367- {
368- var item = _preparedCommands [ i ] ;
369- if ( item . TryGetTarget ( out var current ) && current == command )
370- {
371- _preparedCommands . RemoveAt ( i ) ;
372- return ;
373- }
374- }
343+ var removed = _preparedCommands . Remove ( command ) ;
344+ Debug . Assert ( removed ) ;
375345 }
376346
377347 public void ReleasePreparedCommands ( )
378348 {
379- for ( var i = 0 ; i < _preparedCommands . Count ; i ++ )
349+ // copy the data because the collection will be modified via RemovePreparedCommand from Release
350+ var data = _preparedCommands . ToList ( ) ;
351+ foreach ( var item in data )
380352 {
381- if ( ! _preparedCommands [ i ] . TryGetTarget ( out var current ) )
382- continue ;
383-
384353 try
385354 {
386- current . Release ( ) ;
355+ item . Release ( ) ;
387356 }
388357 catch ( IOException )
389358 {
@@ -401,7 +370,6 @@ public void ReleasePreparedCommands()
401370 }
402371 }
403372 }
404- _preparedCommands . Clear ( ) ;
405373 }
406374
407375 #endregion
0 commit comments