@@ -159,11 +159,11 @@ void ICoreSessionInternal.AbortTransaction(AbortTransactionOptions options, Canc
159159
160160 try
161161 {
162- var firstAttempt = CreateAbortTransactionOperation ( ) ;
162+ var firstAttempt = CreateAbortTransactionOperation ( operationContext ) ;
163163 ExecuteEndTransactionOnPrimary ( operationContext , firstAttempt ) ;
164164 return ;
165165 }
166- catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( exception ) )
166+ catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( operationContext , exception ) )
167167 {
168168 // unpin if retryable error
169169 _currentTransaction . UnpinAll ( ) ;
@@ -177,7 +177,7 @@ void ICoreSessionInternal.AbortTransaction(AbortTransactionOptions options, Canc
177177
178178 try
179179 {
180- var secondAttempt = CreateAbortTransactionOperation ( ) ;
180+ var secondAttempt = CreateAbortTransactionOperation ( operationContext ) ;
181181 ExecuteEndTransactionOnPrimary ( operationContext , secondAttempt ) ;
182182 }
183183 catch
@@ -213,11 +213,11 @@ async Task ICoreSessionInternal.AbortTransactionAsync(AbortTransactionOptions op
213213
214214 try
215215 {
216- var firstAttempt = CreateAbortTransactionOperation ( ) ;
216+ var firstAttempt = CreateAbortTransactionOperation ( operationContext ) ;
217217 await ExecuteEndTransactionOnPrimaryAsync ( operationContext , firstAttempt ) . ConfigureAwait ( false ) ;
218218 return ;
219219 }
220- catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( exception ) )
220+ catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( operationContext , exception ) )
221221 {
222222 // unpin if retryable error
223223 _currentTransaction . UnpinAll ( ) ;
@@ -231,7 +231,7 @@ async Task ICoreSessionInternal.AbortTransactionAsync(AbortTransactionOptions op
231231
232232 try
233233 {
234- var secondAttempt = CreateAbortTransactionOperation ( ) ;
234+ var secondAttempt = CreateAbortTransactionOperation ( operationContext ) ;
235235 await ExecuteEndTransactionOnPrimaryAsync ( operationContext , secondAttempt ) . ConfigureAwait ( false ) ;
236236 }
237237 catch
@@ -317,17 +317,17 @@ void ICoreSessionInternal.CommitTransaction(CommitTransactionOptions options, Ca
317317
318318 try
319319 {
320- var firstAttempt = CreateCommitTransactionOperation ( IsFirstCommitAttemptRetry ( ) ) ;
320+ var firstAttempt = CreateCommitTransactionOperation ( operationContext , IsFirstCommitAttemptRetry ( ) ) ;
321321 ExecuteEndTransactionOnPrimary ( operationContext , firstAttempt ) ;
322322 return ;
323323 }
324- catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( exception ) )
324+ catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( operationContext , exception ) )
325325 {
326326 // unpin server if needed, then ignore exception and retry
327327 TransactionHelper . UnpinServerIfNeededOnRetryableCommitException ( _currentTransaction , exception ) ;
328328 }
329329
330- var secondAttempt = CreateCommitTransactionOperation ( isCommitRetry : true ) ;
330+ var secondAttempt = CreateCommitTransactionOperation ( operationContext , isCommitRetry : true ) ;
331331 ExecuteEndTransactionOnPrimary ( operationContext , secondAttempt ) ;
332332 }
333333 finally
@@ -357,17 +357,17 @@ async Task ICoreSessionInternal.CommitTransactionAsync(CommitTransactionOptions
357357
358358 try
359359 {
360- var firstAttempt = CreateCommitTransactionOperation ( IsFirstCommitAttemptRetry ( ) ) ;
360+ var firstAttempt = CreateCommitTransactionOperation ( operationContext , IsFirstCommitAttemptRetry ( ) ) ;
361361 await ExecuteEndTransactionOnPrimaryAsync ( operationContext , firstAttempt ) . ConfigureAwait ( false ) ;
362362 return ;
363363 }
364- catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( exception ) )
364+ catch ( Exception exception ) when ( ShouldRetryEndTransactionException ( operationContext , exception ) )
365365 {
366366 // unpin server if needed, then ignore exception and retry
367367 TransactionHelper . UnpinServerIfNeededOnRetryableCommitException ( _currentTransaction , exception ) ;
368368 }
369369
370- var secondAttempt = CreateCommitTransactionOperation ( isCommitRetry : true ) ;
370+ var secondAttempt = CreateCommitTransactionOperation ( operationContext , isCommitRetry : true ) ;
371371 await ExecuteEndTransactionOnPrimaryAsync ( operationContext , secondAttempt ) . ConfigureAwait ( false ) ;
372372 }
373373 finally
@@ -444,14 +444,14 @@ public void WasUsed()
444444 }
445445
446446 // private methods
447- private IReadOperation < BsonDocument > CreateAbortTransactionOperation ( )
447+ private IReadOperation < BsonDocument > CreateAbortTransactionOperation ( OperationContext operationContext )
448448 {
449- return new AbortTransactionOperation ( _currentTransaction . RecoveryToken , GetTransactionWriteConcern ( ) ) ;
449+ return new AbortTransactionOperation ( _currentTransaction . RecoveryToken , GetTransactionWriteConcern ( operationContext ) ) ;
450450 }
451451
452- private IReadOperation < BsonDocument > CreateCommitTransactionOperation ( bool isCommitRetry )
452+ private IReadOperation < BsonDocument > CreateCommitTransactionOperation ( OperationContext operationContext , bool isCommitRetry )
453453 {
454- var writeConcern = GetCommitTransactionWriteConcern ( isCommitRetry ) ;
454+ var writeConcern = GetCommitTransactionWriteConcern ( operationContext , isCommitRetry ) ;
455455 var maxCommitTime = _currentTransaction . TransactionOptions . MaxCommitTime ;
456456 return new CommitTransactionOperation ( _currentTransaction . RecoveryToken , writeConcern ) { MaxCommitTime = maxCommitTime } ;
457457 }
@@ -587,21 +587,27 @@ private TransactionOptions GetEffectiveTransactionOptions(TransactionOptions tra
587587 return new TransactionOptions ( readConcern , readPreference , writeConcern , maxCommitTime ) ;
588588 }
589589
590- private WriteConcern GetTransactionWriteConcern ( )
590+ private WriteConcern GetTransactionWriteConcern ( OperationContext operationContext )
591591 {
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 ;
596602 }
597603
598- private WriteConcern GetCommitTransactionWriteConcern ( bool isCommitRetry )
604+ private WriteConcern GetCommitTransactionWriteConcern ( OperationContext operationContext , bool isCommitRetry )
599605 {
600- var writeConcern = GetTransactionWriteConcern ( ) ;
606+ var writeConcern = GetTransactionWriteConcern ( operationContext ) ;
601607 if ( isCommitRetry )
602608 {
603609 writeConcern = writeConcern . With ( mode : "majority" ) ;
604- if ( writeConcern . WTimeout == null )
610+ if ( writeConcern . WTimeout == null && ! operationContext . IsRootContextTimeoutConfigured ( ) )
605611 {
606612 writeConcern = writeConcern . With ( wTimeout : TimeSpan . FromMilliseconds ( 10000 ) ) ;
607613 }
@@ -616,9 +622,14 @@ private bool IsFirstCommitAttemptRetry()
616622 return _currentTransaction . State == CoreTransactionState . Committed ;
617623 }
618624
619- private bool ShouldRetryEndTransactionException ( Exception exception )
625+ private bool ShouldRetryEndTransactionException ( OperationContext operationContext , Exception exception )
620626 {
621- return RetryabilityHelper . IsRetryableWriteException ( exception ) ;
627+ if ( ! RetryabilityHelper . IsRetryableWriteException ( exception ) )
628+ {
629+ return false ;
630+ }
631+
632+ return operationContext . IsRootContextTimeoutConfigured ( ) ? ! operationContext . IsTimedOut ( ) : true ;
622633 }
623634 }
624635}
0 commit comments