@@ -159,11 +159,11 @@ void ICoreSessionInternal.AbortTransaction(AbortTransactionOptions options, Canc
159
159
160
160
try
161
161
{
162
- var firstAttempt = CreateAbortTransactionOperation ( ) ;
162
+ var firstAttempt = CreateAbortTransactionOperation ( operationContext ) ;
163
163
ExecuteEndTransactionOnPrimary ( operationContext , firstAttempt ) ;
164
164
return ;
165
165
}
166
- catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( exception ) )
166
+ catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( operationContext , exception ) )
167
167
{
168
168
// unpin if retryable error
169
169
_currentTransaction . UnpinAll ( ) ;
@@ -177,7 +177,7 @@ void ICoreSessionInternal.AbortTransaction(AbortTransactionOptions options, Canc
177
177
178
178
try
179
179
{
180
- var secondAttempt = CreateAbortTransactionOperation ( ) ;
180
+ var secondAttempt = CreateAbortTransactionOperation ( operationContext ) ;
181
181
ExecuteEndTransactionOnPrimary ( operationContext , secondAttempt ) ;
182
182
}
183
183
catch
@@ -213,11 +213,11 @@ async Task ICoreSessionInternal.AbortTransactionAsync(AbortTransactionOptions op
213
213
214
214
try
215
215
{
216
- var firstAttempt = CreateAbortTransactionOperation ( ) ;
216
+ var firstAttempt = CreateAbortTransactionOperation ( operationContext ) ;
217
217
await ExecuteEndTransactionOnPrimaryAsync ( operationContext , firstAttempt ) . ConfigureAwait ( false ) ;
218
218
return ;
219
219
}
220
- catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( exception ) )
220
+ catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( operationContext , exception ) )
221
221
{
222
222
// unpin if retryable error
223
223
_currentTransaction . UnpinAll ( ) ;
@@ -231,7 +231,7 @@ async Task ICoreSessionInternal.AbortTransactionAsync(AbortTransactionOptions op
231
231
232
232
try
233
233
{
234
- var secondAttempt = CreateAbortTransactionOperation ( ) ;
234
+ var secondAttempt = CreateAbortTransactionOperation ( operationContext ) ;
235
235
await ExecuteEndTransactionOnPrimaryAsync ( operationContext , secondAttempt ) . ConfigureAwait ( false ) ;
236
236
}
237
237
catch
@@ -317,17 +317,17 @@ void ICoreSessionInternal.CommitTransaction(CommitTransactionOptions options, Ca
317
317
318
318
try
319
319
{
320
- var firstAttempt = CreateCommitTransactionOperation ( IsFirstCommitAttemptRetry ( ) ) ;
320
+ var firstAttempt = CreateCommitTransactionOperation ( operationContext , IsFirstCommitAttemptRetry ( ) ) ;
321
321
ExecuteEndTransactionOnPrimary ( operationContext , firstAttempt ) ;
322
322
return ;
323
323
}
324
- catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( exception ) )
324
+ catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( operationContext , exception ) )
325
325
{
326
326
// unpin server if needed, then ignore exception and retry
327
327
TransactionHelper . UnpinServerIfNeededOnRetryableCommitException ( _currentTransaction , exception ) ;
328
328
}
329
329
330
- var secondAttempt = CreateCommitTransactionOperation ( isCommitRetry : true ) ;
330
+ var secondAttempt = CreateCommitTransactionOperation ( operationContext , isCommitRetry : true ) ;
331
331
ExecuteEndTransactionOnPrimary ( operationContext , secondAttempt ) ;
332
332
}
333
333
finally
@@ -357,17 +357,17 @@ async Task ICoreSessionInternal.CommitTransactionAsync(CommitTransactionOptions
357
357
358
358
try
359
359
{
360
- var firstAttempt = CreateCommitTransactionOperation ( IsFirstCommitAttemptRetry ( ) ) ;
360
+ var firstAttempt = CreateCommitTransactionOperation ( operationContext , IsFirstCommitAttemptRetry ( ) ) ;
361
361
await ExecuteEndTransactionOnPrimaryAsync ( operationContext , firstAttempt ) . ConfigureAwait ( false ) ;
362
362
return ;
363
363
}
364
- catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( exception ) )
364
+ catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( operationContext , exception ) )
365
365
{
366
366
// unpin server if needed, then ignore exception and retry
367
367
TransactionHelper . UnpinServerIfNeededOnRetryableCommitException ( _currentTransaction , exception ) ;
368
368
}
369
369
370
- var secondAttempt = CreateCommitTransactionOperation ( isCommitRetry : true ) ;
370
+ var secondAttempt = CreateCommitTransactionOperation ( operationContext , isCommitRetry : true ) ;
371
371
await ExecuteEndTransactionOnPrimaryAsync ( operationContext , secondAttempt ) . ConfigureAwait ( false ) ;
372
372
}
373
373
finally
@@ -444,14 +444,14 @@ public void WasUsed()
444
444
}
445
445
446
446
// private methods
447
- private IReadOperation < BsonDocument > CreateAbortTransactionOperation ( )
447
+ private IReadOperation < BsonDocument > CreateAbortTransactionOperation ( OperationContext operationContext )
448
448
{
449
- return new AbortTransactionOperation ( _currentTransaction . RecoveryToken , GetTransactionWriteConcern ( ) ) ;
449
+ return new AbortTransactionOperation ( _currentTransaction . RecoveryToken , GetTransactionWriteConcern ( operationContext ) ) ;
450
450
}
451
451
452
- private IReadOperation < BsonDocument > CreateCommitTransactionOperation ( bool isCommitRetry )
452
+ private IReadOperation < BsonDocument > CreateCommitTransactionOperation ( OperationContext operationContext , bool isCommitRetry )
453
453
{
454
- var writeConcern = GetCommitTransactionWriteConcern ( isCommitRetry ) ;
454
+ var writeConcern = GetCommitTransactionWriteConcern ( operationContext , isCommitRetry ) ;
455
455
var maxCommitTime = _currentTransaction . TransactionOptions . MaxCommitTime ;
456
456
return new CommitTransactionOperation ( _currentTransaction . RecoveryToken , writeConcern ) { MaxCommitTime = maxCommitTime } ;
457
457
}
@@ -587,21 +587,27 @@ private TransactionOptions GetEffectiveTransactionOptions(TransactionOptions tra
587
587
return new TransactionOptions ( readConcern , readPreference , writeConcern , maxCommitTime ) ;
588
588
}
589
589
590
- private WriteConcern GetTransactionWriteConcern ( )
590
+ private WriteConcern GetTransactionWriteConcern ( OperationContext operationContext )
591
591
{
592
- return
593
- _currentTransaction . TransactionOptions ? . WriteConcern ??
594
- _options . DefaultTransactionOptions ? . WriteConcern ??
595
- WriteConcern . WMajority ;
592
+ var writeConcern = _currentTransaction . TransactionOptions ? . WriteConcern ??
593
+ _options . DefaultTransactionOptions ? . WriteConcern ??
594
+ WriteConcern . WMajority ;
595
+
596
+ if ( operationContext . IsRootContextTimeoutConfigured ( ) )
597
+ {
598
+ writeConcern = writeConcern . With ( wTimeout : null ) ;
599
+ }
600
+
601
+ return writeConcern ;
596
602
}
597
603
598
- private WriteConcern GetCommitTransactionWriteConcern ( bool isCommitRetry )
604
+ private WriteConcern GetCommitTransactionWriteConcern ( OperationContext operationContext , bool isCommitRetry )
599
605
{
600
- var writeConcern = GetTransactionWriteConcern ( ) ;
606
+ var writeConcern = GetTransactionWriteConcern ( operationContext ) ;
601
607
if ( isCommitRetry )
602
608
{
603
609
writeConcern = writeConcern . With ( mode : "majority" ) ;
604
- if ( writeConcern . WTimeout == null )
610
+ if ( writeConcern . WTimeout == null && ! operationContext . IsRootContextTimeoutConfigured ( ) )
605
611
{
606
612
writeConcern = writeConcern . With ( wTimeout : TimeSpan . FromMilliseconds ( 10000 ) ) ;
607
613
}
@@ -616,9 +622,14 @@ private bool IsFirstCommitAttemptRetry()
616
622
return _currentTransaction . State == CoreTransactionState . Committed ;
617
623
}
618
624
619
- private bool ShouldRetryEndTransactionException ( Exception exception )
625
+ private bool ShouldRetryEndTransactionException ( OperationContext operationContext , Exception exception )
620
626
{
621
- return RetryabilityHelper . IsRetryableWriteException ( exception ) ;
627
+ if ( ! RetryabilityHelper . IsRetryableWriteException ( exception ) )
628
+ {
629
+ return false ;
630
+ }
631
+
632
+ return operationContext . IsRootContextTimeoutConfigured ( ) ? ! operationContext . IsTimedOut ( ) : true ;
622
633
}
623
634
}
624
635
}
0 commit comments