2727import com .mongodb .WriteConcern ;
2828import com .mongodb .client .ClientSession ;
2929import com .mongodb .client .TransactionBody ;
30- import com .mongodb .internal .TimeoutContext ;
3130import com .mongodb .internal .ExponentialBackoff ;
31+ import com .mongodb .internal .TimeoutContext ;
32+ import com .mongodb .internal .observability .micrometer .TracingManager ;
33+ import com .mongodb .internal .observability .micrometer .TransactionSpan ;
3234import com .mongodb .internal .operation .AbortTransactionOperation ;
3335import com .mongodb .internal .operation .CommitTransactionOperation ;
3436import com .mongodb .internal .operation .OperationHelper ;
3739import com .mongodb .internal .operation .WriteOperation ;
3840import com .mongodb .internal .session .BaseClientSessionImpl ;
3941import com .mongodb .internal .session .ServerSessionPool ;
40- import com .mongodb .internal .observability .micrometer .TracingManager ;
41- import com .mongodb .internal .observability .micrometer .TransactionSpan ;
4242import com .mongodb .lang .Nullable ;
4343
4444import static com .mongodb .MongoException .TRANSIENT_TRANSACTION_ERROR_LABEL ;
@@ -252,12 +252,8 @@ public <T> T withTransaction(final TransactionBody<T> transactionBody, final Tra
252252 notNull ("transactionBody" , transactionBody );
253253 long startTime = ClientSessionClock .INSTANCE .now ();
254254 TimeoutContext withTransactionTimeoutContext = createTimeoutContext (options );
255- // Use CSOT timeout if set, otherwise default to MAX_RETRY_TIME_LIMIT_MS
256- Long timeoutMS = withTransactionTimeoutContext .getTimeoutSettings ().getTimeoutMS ();
257- long maxRetryTimeMS = timeoutMS != null ? timeoutMS : MAX_RETRY_TIME_LIMIT_MS ;
258255 ExponentialBackoff transactionBackoff = null ;
259256 boolean isRetry = false ;
260- MongoException lastError = null ;
261257
262258 try {
263259 outer :
@@ -267,19 +263,7 @@ public <T> T withTransaction(final TransactionBody<T> transactionBody, final Tra
267263 if (transactionBackoff == null ) {
268264 transactionBackoff = ExponentialBackoff .forTransactionRetry ();
269265 }
270- // Calculate backoff delay and check if it would exceed timeout
271266 long backoffMs = transactionBackoff .calculateDelayMs ();
272- if (ClientSessionClock .INSTANCE .now () - startTime + backoffMs >= maxRetryTimeMS ) {
273- // If CSOT is enabled (timeoutMS is set), throw MongoOperationTimeoutException
274- // Otherwise, throw the last error directly for backward compatibility
275- if (timeoutMS != null ) {
276- throw new MongoOperationTimeoutException (
277- "Transaction retry timeout exceeded after " + (ClientSessionClock .INSTANCE .now () - startTime ) + "ms" ,
278- lastError );
279- } else {
280- throw lastError ;
281- }
282- }
283267 try {
284268 if (backoffMs > 0 ) {
285269 Thread .sleep (backoffMs );
@@ -304,8 +288,7 @@ public <T> T withTransaction(final TransactionBody<T> transactionBody, final Tra
304288 if (e instanceof MongoException && !(e instanceof MongoOperationTimeoutException )) {
305289 MongoException exceptionToHandle = OperationHelper .unwrap ((MongoException ) e );
306290 if (exceptionToHandle .hasErrorLabel (TRANSIENT_TRANSACTION_ERROR_LABEL )
307- && ClientSessionClock .INSTANCE .now () - startTime < maxRetryTimeMS ) {
308- lastError = exceptionToHandle ; // Track the last error for timeout scenarios
291+ && ClientSessionClock .INSTANCE .now () - startTime < MAX_RETRY_TIME_LIMIT_MS ) {
309292 if (transactionSpan != null ) {
310293 transactionSpan .spanFinalizing (false );
311294 }
@@ -320,10 +303,9 @@ public <T> T withTransaction(final TransactionBody<T> transactionBody, final Tra
320303 commitTransaction (false );
321304 break ;
322305 } catch (MongoException e ) {
323- lastError = e ; // Track the last error for timeout scenarios
324306 clearTransactionContextOnError (e );
325307 if (!(e instanceof MongoOperationTimeoutException )
326- && ClientSessionClock .INSTANCE .now () - startTime < maxRetryTimeMS ) {
308+ && ClientSessionClock .INSTANCE .now () - startTime < MAX_RETRY_TIME_LIMIT_MS ) {
327309 applyMajorityWriteConcernToTransactionOptions ();
328310
329311 if (!(e instanceof MongoExecutionTimeoutException )
0 commit comments