Skip to content

Commit 0da7483

Browse files
committed
JAVA-2616: Wait for at least one connected server to be available when determining whether to apply an implicit session
1 parent 9f757fb commit 0da7483

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

driver/src/main/com/mongodb/Mongo.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.mongodb.operation.ReadOperation;
4444
import com.mongodb.operation.WriteOperation;
4545
import com.mongodb.selector.LatencyMinimizingServerSelector;
46+
import com.mongodb.selector.ServerSelector;
4647
import org.bson.BsonBoolean;
4748
import org.bson.BsonDocument;
4849
import org.bson.BsonTimestamp;
@@ -853,13 +854,31 @@ ClientSession getClientSession(final ClientSession clientSessionFromOperation) {
853854
}
854855

855856
ClientSession createClientSession(final ClientSessionOptions options) {
856-
if (cluster.getDescription().getLogicalSessionTimeoutMinutes() != null && credentialsList.size() < 2) {
857+
if (credentialsList.size() > 1) {
858+
return null;
859+
}
860+
if (getConnectedClusterDescription().getLogicalSessionTimeoutMinutes() != null) {
857861
return new ClientSessionImpl(this, options);
858862
} else {
859863
return null;
860864
}
861865
}
862866

867+
@SuppressWarnings("deprecation")
868+
private ClusterDescription getConnectedClusterDescription() {
869+
ClusterDescription clusterDescription = cluster.getDescription();
870+
if (clusterDescription.getAny().isEmpty()) {
871+
cluster.selectServer(new ServerSelector() {
872+
@Override
873+
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
874+
return clusterDescription.getAny();
875+
}
876+
});
877+
clusterDescription = cluster.getDescription();
878+
}
879+
return clusterDescription;
880+
}
881+
863882
static class ClientSessionImpl implements ClientSession {
864883
private static final String CLUSTER_TIME_KEY = "clusterTime";
865884

driver/src/test/functional/com/mongodb/MongoClientSessionSpecification.groovy

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,14 @@ class MongoClientSessionSpecification extends FunctionalSpecification {
230230
def optionsBuilder = MongoClientOptions.builder()
231231
.addCommandListener(commandListener)
232232
def client = new MongoClient(Fixture.getMongoClientURI(optionsBuilder))
233-
// TODO: Remove this once SPEC-944 is resolved
234-
client.getDatabase('admin').runCommand(new BsonDocument('ping', new BsonInt32(1)))
235233

236234
when:
237235
client.getDatabase('admin').runCommand(new BsonDocument('ping', new BsonInt32(1)))
238236

239237
then:
240-
def pingCommandStartedEvent = commandListener.events.get(2)
241-
(pingCommandStartedEvent as CommandStartedEvent).command.containsKey('lsid')
238+
commandListener.events.size() == 2
239+
def pingCommandStartedEvent = commandListener.events.get(0) as CommandStartedEvent
240+
pingCommandStartedEvent.command.containsKey('lsid')
242241

243242
cleanup:
244243
client?.close()

0 commit comments

Comments
 (0)