Skip to content

Commit 2961ce5

Browse files
committed
JAVA-2815: Add TransientTransactionError error label
Add TransientTransactionError error label for MongoTimeoutException as required by the specification. Currently there's no strategy to test this, as the classes in which it's implemented are not easily subject to mocking.
1 parent 2247c7d commit 2961ce5

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

driver-async/src/main/com/mongodb/async/client/OperationExecutorImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.mongodb.MongoException;
2121
import com.mongodb.MongoInternalException;
2222
import com.mongodb.MongoSocketException;
23+
import com.mongodb.MongoTimeoutException;
2324
import com.mongodb.ReadConcern;
2425
import com.mongodb.ReadPreference;
2526
import com.mongodb.async.SingleResultCallback;
@@ -126,7 +127,8 @@ public void onResult(final T result, final Throwable t) {
126127

127128

128129
private void labelException(final Throwable t, final ClientSession session) {
129-
if (t instanceof MongoSocketException && session != null && session.hasActiveTransaction()
130+
if ((t instanceof MongoSocketException || t instanceof MongoTimeoutException)
131+
&& session != null && session.hasActiveTransaction()
130132
&& !((MongoException) t).hasErrorLabel(UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)) {
131133
((MongoException) t).addLabel(TRANSIENT_TRANSACTION_ERROR_LABEL);
132134
}

driver-sync/src/main/com/mongodb/client/internal/MongoClientDelegate.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.mongodb.MongoException;
2323
import com.mongodb.MongoInternalException;
2424
import com.mongodb.MongoSocketException;
25+
import com.mongodb.MongoTimeoutException;
2526
import com.mongodb.ReadConcern;
2627
import com.mongodb.ReadPreference;
2728
import com.mongodb.ServerAddress;
@@ -46,6 +47,7 @@
4647
import java.util.ArrayList;
4748
import java.util.List;
4849

50+
import static com.mongodb.MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL;
4951
import static com.mongodb.MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL;
5052
import static com.mongodb.ReadPreference.primary;
5153
import static com.mongodb.assertions.Assertions.isTrue;
@@ -175,7 +177,7 @@ public <T> T execute(final ReadOperation<T> operation, final ReadPreference read
175177
throw new MongoClientException("Read preference in a transaction must be primary");
176178
}
177179
return operation.execute(binding);
178-
} catch (MongoSocketException e) {
180+
} catch (MongoException e) {
179181
labelException(session, e);
180182
throw e;
181183
} finally {
@@ -189,7 +191,7 @@ public <T> T execute(final WriteOperation<T> operation, final ReadConcern readCo
189191
WriteBinding binding = getWriteBinding(readConcern, actualClientSession, session == null && actualClientSession != null);
190192
try {
191193
return operation.execute(binding);
192-
} catch (MongoSocketException e) {
194+
} catch (MongoException e) {
193195
labelException(session, e);
194196
throw e;
195197
} finally {
@@ -217,8 +219,9 @@ ReadWriteBinding getReadWriteBinding(final ReadPreference readPreference, final
217219
}
218220

219221
private void labelException(final @Nullable ClientSession session, final MongoException e) {
220-
if (session != null && session.hasActiveTransaction() && !e.hasErrorLabel(UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)) {
221-
e.addLabel(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL);
222+
if ((e instanceof MongoSocketException || e instanceof MongoTimeoutException)
223+
&& session != null && session.hasActiveTransaction() && !e.hasErrorLabel(UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)) {
224+
e.addLabel(TRANSIENT_TRANSACTION_ERROR_LABEL);
222225
}
223226
}
224227

0 commit comments

Comments
 (0)