Skip to content

Commit 57a7b4b

Browse files
committed
Allow writes to any server when in direct connection mode
JAVA-3309
1 parent e24a003 commit 57a7b4b

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

driver-core/src/main/com/mongodb/selector/WritableServerSelector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.selector;
1818

19+
import com.mongodb.connection.ClusterConnectionMode;
1920
import com.mongodb.connection.ClusterDescription;
2021
import com.mongodb.connection.ServerDescription;
2122

@@ -32,6 +33,9 @@ public final class WritableServerSelector implements ServerSelector {
3233
@Override
3334
@SuppressWarnings("deprecation")
3435
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
36+
if (clusterDescription.getConnectionMode() == ClusterConnectionMode.SINGLE) {
37+
return clusterDescription.getAny();
38+
}
3539
return clusterDescription.getPrimaries();
3640
}
3741

driver-core/src/test/unit/com/mongodb/selector/WritableServerSelectorSpecification.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import spock.lang.Specification
2424
import spock.lang.Unroll
2525

2626
import static com.mongodb.connection.ClusterConnectionMode.MULTIPLE
27+
import static com.mongodb.connection.ClusterConnectionMode.SINGLE
2728
import static com.mongodb.connection.ServerConnectionState.CONNECTED
2829
import static com.mongodb.connection.ServerType.REPLICA_SET_PRIMARY
2930
import static com.mongodb.connection.ServerType.REPLICA_SET_SECONDARY
@@ -49,4 +50,15 @@ class WritableServerSelectorSpecification extends Specification {
4950
[] | new ClusterDescription(MULTIPLE, ClusterType.REPLICA_SET, [SECONDARY_SERVER])
5051
}
5152

53+
@Unroll
54+
def 'WritableServerSelector will choose secondary server in single mode for #clusterDescription'() throws UnknownHostException {
55+
expect:
56+
WritableServerSelector selector = new WritableServerSelector()
57+
expectedServerList == selector.select(clusterDescription)
58+
59+
where:
60+
expectedServerList | clusterDescription
61+
[SECONDARY_SERVER] | new ClusterDescription(SINGLE, ClusterType.REPLICA_SET, [SECONDARY_SERVER])
62+
}
63+
5264
}

0 commit comments

Comments
 (0)