@@ -525,28 +525,30 @@ private async Task RenewLeasesAsync(SqlConnection connection, CancellationToken
525525 {
526526 try
527527 {
528- using ( SqlCommand renewLeasesCommand = this . BuildRenewLeasesCommand ( connection , transaction ) )
528+ SqlCommand renewLeasesCommand = this . BuildRenewLeasesCommand ( connection , transaction ) ;
529+ if ( renewLeasesCommand != null )
529530 {
530- var stopwatch = Stopwatch . StartNew ( ) ;
531+ using ( renewLeasesCommand )
532+ {
533+ var stopwatch = Stopwatch . StartNew ( ) ;
531534
532- int rowsAffected = await renewLeasesCommand . ExecuteNonQueryAsyncWithLogging ( this . _logger , token , true ) ;
535+ int rowsAffected = await renewLeasesCommand . ExecuteNonQueryAsyncWithLogging ( this . _logger , token , true ) ;
533536
534- long durationMs = stopwatch . ElapsedMilliseconds ;
537+ long durationMs = stopwatch . ElapsedMilliseconds ;
535538
536- if ( rowsAffected > 0 )
537- {
538- this . _logger . LogDebug ( $ "Renewed leases for { rowsAffected } rows") ;
539- // Only send an event if we actually updated rows to reduce the overall number of events we send
540- var measures = new Dictionary < TelemetryMeasureName , double >
539+ if ( rowsAffected > 0 )
541540 {
542- [ TelemetryMeasureName . DurationMs ] = durationMs ,
543- } ;
544-
545- TelemetryInstance . TrackEvent ( TelemetryEventName . RenewLeases , this . _telemetryProps , measures ) ;
541+ this . _logger . LogDebug ( $ "Renewed leases for { rowsAffected } rows") ;
542+ // Only send an event if we actually updated rows to reduce the overall number of events we send
543+ var measures = new Dictionary < TelemetryMeasureName , double >
544+ {
545+ [ TelemetryMeasureName . DurationMs ] = durationMs ,
546+ } ;
547+
548+ TelemetryInstance . TrackEvent ( TelemetryEventName . RenewLeases , this . _telemetryProps , measures ) ;
549+ }
550+ transaction . Commit ( ) ;
546551 }
547-
548-
549- transaction . Commit ( ) ;
550552 }
551553 }
552554 catch ( Exception e )
@@ -978,9 +980,12 @@ WHEN NOT MATCHED THEN
978980 private SqlCommand BuildRenewLeasesCommand ( SqlConnection connection , SqlTransaction transaction )
979981 {
980982 string matchCondition = string . Join ( " OR " , this . _rowMatchConditions . Take ( this . _rowsToProcess . Count ) ) ;
983+ // If the matchCondition is empty return null to avoid empty where clause query failure.
981984 if ( string . IsNullOrEmpty ( matchCondition ) )
982985 {
983986 this . _logger . LogError ( $ "MatchCondition resolved to empty with '{ this . _rowsToProcess . Count } ' rowsToProcess.") ;
987+ TelemetryInstance . TrackEvent ( TelemetryEventName . BuildRenewLeasesWithEmptyMatchCondtion ) ;
988+ return null ;
984989 }
985990 string renewLeasesQuery = $@ "
986991 { AppLockStatements }
0 commit comments