diff --git a/pymongo/asynchronous/mongo_client.py b/pymongo/asynchronous/mongo_client.py index 7c824d25b..509f2742f 100644 --- a/pymongo/asynchronous/mongo_client.py +++ b/pymongo/asynchronous/mongo_client.py @@ -2868,10 +2868,11 @@ class _ClientConnectionRetryable(Generic[T]): retryable_write_label = exc_to_check.has_error_label("RetryableWriteError") overloaded = exc_to_check.has_error_label("SystemOverloadedError") always_retryable = exc_to_check.has_error_label("RetryableError") and overloaded - if ( - not self._client.options.retry_writes - or not self._retryable - and not always_retryable + + # Always retry abortTransaction and commitTransaction up to once + if not (self._client.options.retry_writes and self._retryable) and ( + not always_retryable + and self._operation not in ["abortTransaction", "commitTransaction"] ): raise if retryable_write_label or always_retryable: diff --git a/pymongo/synchronous/mongo_client.py b/pymongo/synchronous/mongo_client.py index 44bd91ad9..f1e6024ce 100644 --- a/pymongo/synchronous/mongo_client.py +++ b/pymongo/synchronous/mongo_client.py @@ -2858,10 +2858,11 @@ class _ClientConnectionRetryable(Generic[T]): retryable_write_label = exc_to_check.has_error_label("RetryableWriteError") overloaded = exc_to_check.has_error_label("SystemOverloadedError") always_retryable = exc_to_check.has_error_label("RetryableError") and overloaded - if ( - not self._client.options.retry_writes - or not self._retryable - and not always_retryable + + # Always retry abortTransaction and commitTransaction up to once + if not (self._client.options.retry_writes and self._retryable) and ( + not always_retryable + and self._operation not in ["abortTransaction", "commitTransaction"] ): raise if retryable_write_label or always_retryable: