11using System . Data ;
22using System . Data . Common ;
3- using System . Diagnostics ;
43
54using FluentCommand . Extensions ;
5+ using FluentCommand . Internal ;
66
77using HashCode = FluentCommand . Internal . HashCode ;
88
@@ -162,7 +162,7 @@ public IDataCommand UseCache(DateTimeOffset absoluteExpiration)
162162 /// A fluent <see langword="interface" /> to the data command.
163163 /// </returns>
164164 /// <remarks>
165- /// Cached keys are created using the current DataCommand state. When any Query opertion is
165+ /// Cached keys are created using the current DataCommand state. When any Query operation is
166166 /// executed with a cache policy, the results are cached. Use this method with the same parameters
167167 /// to expire the cached item.
168168 /// </remarks>
@@ -525,7 +525,7 @@ protected override void DisposeManagedResources()
525525 Command ? . Dispose ( ) ;
526526 }
527527
528- #if ! NETSTANDARD2_0
528+ #if NETCOREAPP3_0_OR_GREATER
529529 /// <summary>
530530 /// Disposes the managed resources.
531531 /// </summary>
@@ -556,12 +556,14 @@ private TResult QueryFactory<TResult>(Func<TResult> query, bool supportCache)
556556
557557 AssertDisposed ( ) ;
558558
559- var watch = Stopwatch . StartNew ( ) ;
559+ var watch = SharedStopwatch . StartNew ( ) ;
560+ var logged = false ;
561+
560562 try
561563 {
562564 var cacheKey = CacheKey < TResult > ( supportCache ) ;
563565
564- ( bool cacheSuccess , TResult cacheValue ) = GetCache < TResult > ( cacheKey ) ;
566+ var ( cacheSuccess , cacheValue ) = GetCache < TResult > ( cacheKey ) ;
565567 if ( cacheSuccess )
566568 return cacheValue ;
567569
@@ -577,19 +579,16 @@ private TResult QueryFactory<TResult>(Func<TResult> query, bool supportCache)
577579 }
578580 catch ( Exception ex )
579581 {
580- watch . Stop ( ) ;
581582 LogCommand ( watch . Elapsed , ex ) ;
583+ logged = true ;
582584
583585 throw ;
584586 }
585587 finally
586588 {
587589 // if catch block didn't already log
588- if ( watch . IsRunning )
589- {
590- watch . Stop ( ) ;
590+ if ( ! logged )
591591 LogCommand ( watch . Elapsed ) ;
592- }
593592
594593 _dataSession . ReleaseConnection ( ) ;
595594 Dispose ( ) ;
@@ -606,12 +605,14 @@ private async Task<TResult> QueryFactoryAsync<TResult>(
606605
607606 AssertDisposed ( ) ;
608607
609- var watch = Stopwatch . StartNew ( ) ;
608+ var watch = SharedStopwatch . StartNew ( ) ;
609+ var logged = false ;
610+
610611 try
611612 {
612613 var cacheKey = CacheKey < TResult > ( supportCache ) ;
613614
614- ( bool cacheSuccess , TResult cacheValue ) = await GetCacheAsync < TResult > ( cacheKey , cancellationToken ) . ConfigureAwait ( false ) ;
615+ var ( cacheSuccess , cacheValue ) = await GetCacheAsync < TResult > ( cacheKey , cancellationToken ) . ConfigureAwait ( false ) ;
615616 if ( cacheSuccess )
616617 return cacheValue ;
617618
@@ -627,22 +628,25 @@ private async Task<TResult> QueryFactoryAsync<TResult>(
627628 }
628629 catch ( Exception ex )
629630 {
630- watch . Stop ( ) ;
631631 LogCommand ( watch . Elapsed , ex ) ;
632+ logged = true ;
632633
633634 throw ;
634635 }
635636 finally
636637 {
637638 // if catch block didn't already log
638- if ( watch . IsRunning )
639- {
640- watch . Stop ( ) ;
639+ if ( ! logged )
641640 LogCommand ( watch . Elapsed ) ;
642- }
643641
642+ #if NETCOREAPP3_0_OR_GREATER
643+
644+ await _dataSession . ReleaseConnectionAsync ( ) ;
645+ await DisposeAsync ( ) ;
646+ #else
644647 _dataSession . ReleaseConnection ( ) ;
645648 Dispose ( ) ;
649+ #endif
646650 }
647651 }
648652
0 commit comments