Skip to content

Commit 89ffdb5

Browse files
committed
Throw MongoClientException for sharded cluster transactions
This version of the driver does not support sharded cluster transactions, so should throw a MongoClientException it an application attempts it. JAVA-3240
1 parent afe1692 commit 89ffdb5

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

driver-async/src/test/functional/com/mongodb/async/client/TransactionFailureTest.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,23 @@ public TransactionFailureTest() {
3333

3434
@Before
3535
public void setUp() {
36-
assumeTrue(canRunTests());
3736
super.setUp();
3837
}
3938

4039
@Test(expected = MongoClientException.class)
41-
public void testTransactionFails() throws InterruptedException {
40+
public void testTransactionFailsOn40Cluster() throws InterruptedException {
41+
assumeTrue(serverVersionLessThan("4.0"));
42+
doTransaction();
43+
}
44+
45+
@Test(expected = MongoClientException.class)
46+
public void testTransactionFailsOnShardedCluster() throws InterruptedException {
47+
assumeTrue(isSharded());
48+
doTransaction();
49+
}
50+
51+
52+
private void doTransaction() throws InterruptedException {
4253
final ClientSession clientSession = createSession();
4354

4455
try {
@@ -61,10 +72,4 @@ public void execute() {
6172
}
6273
}.get();
6374
}
64-
65-
66-
private boolean canRunTests() {
67-
return serverVersionLessThan("4.0")
68-
|| (serverVersionLessThan("4.1.0") && isSharded());
69-
}
7075
}

driver-core/src/main/com/mongodb/internal/connection/CommandMessage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import static com.mongodb.connection.ServerType.SHARD_ROUTER;
5151
import static com.mongodb.internal.connection.BsonWriterHelper.writePayload;
5252
import static com.mongodb.internal.connection.ReadConcernHelper.getReadConcernDocument;
53-
import static com.mongodb.internal.operation.ServerVersionHelper.FOUR_DOT_TWO_WIRE_VERSION;
5453
import static com.mongodb.internal.operation.ServerVersionHelper.FOUR_DOT_ZERO_WIRE_VERSION;
5554
import static com.mongodb.internal.operation.ServerVersionHelper.THREE_DOT_SIX_WIRE_VERSION;
5655

@@ -265,9 +264,10 @@ private List<BsonElement> getExtraElements(final SessionContext sessionContext)
265264

266265
private void checkServerVersionForTransactionSupport() {
267266
int wireVersion = getSettings().getMaxWireVersion();
268-
if (wireVersion < FOUR_DOT_ZERO_WIRE_VERSION
269-
|| (wireVersion < FOUR_DOT_TWO_WIRE_VERSION && getSettings().getServerType() == SHARD_ROUTER)) {
267+
if (wireVersion < FOUR_DOT_ZERO_WIRE_VERSION) {
270268
throw new MongoClientException("Transactions are not supported by the MongoDB cluster to which this client is connected.");
269+
} else if (getSettings().getServerType() == SHARD_ROUTER) {
270+
throw new MongoClientException("This version of the driver does not support transactions on a sharded cluster.");
271271
}
272272
}
273273

driver-sync/src/test/functional/com/mongodb/client/TransactionFailureTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,22 @@ public TransactionFailureTest() {
3131

3232
@Before
3333
public void setUp() {
34-
assumeTrue(canRunTests());
3534
super.setUp();
3635
}
3736

3837
@Test(expected = MongoClientException.class)
39-
public void testTransactionFails() {
38+
public void testTransactionFailsOn40Cluster() {
39+
assumeTrue(serverVersionLessThan("4.0"));
40+
doTransaction();
41+
}
42+
43+
@Test(expected = MongoClientException.class)
44+
public void testTransactionFailsOnShardedCluster() {
45+
assumeTrue(isSharded());
46+
doTransaction();
47+
}
48+
49+
private void doTransaction() {
4050
ClientSession clientSession = client.startSession();
4151
try {
4252
clientSession.startTransaction();
@@ -45,9 +55,4 @@ public void testTransactionFails() {
4555
clientSession.close();
4656
}
4757
}
48-
49-
private boolean canRunTests() {
50-
return serverVersionLessThan("4.0")
51-
|| (serverVersionLessThan("4.1.0") && isSharded());
52-
}
5358
}

0 commit comments

Comments
 (0)