I have an annoying problem with the java driver connecting to a replica set in the remote environment.
I am using the below code to connect to the server through Mongo DB Java Async API 3.2.2.
MongoClient mongoClusterClient = MongoClients.create(MongoClientSettings.builder()
.clusterSettings(ClusterSettings.builder()
.applyConnectionString(hrvuConnectionString)
.requiredClusterType(ClusterType.REPLICA_SET)
.mode(ClusterConnectionMode.MULTIPLE)
.serverSelectionTimeout(120, TimeUnit.SECONDS)
.serverSelector(new WritableServerSelector())
.build())
.connectionPoolSettings(ConnectionPoolSettings.builder()
.applyConnectionString(hrvuConnectionString)
.build())
.serverSettings(ServerSettings.builder().build())
.credentialList(getMongoConnectionString().getCredentialList())
.sslSettings(SslSettings.builder()
.applyConnectionString(hrvuConnectionString)
.build())
.socketSettings(SocketSettings.builder()
.applyConnectionString(hrvuConnectionString)
.build())
.build());
return mongoClusterClient;
My connection string takes the below format.
I tried with different options in the client creation (supplying hosts instead of the connection string for the ClusterSettings) but always ended up with the below problem:
What is more interesting is after reporting this, the application is obtaining the connections independently. And there is no problem with the single DB server that is installed in my local environment. Application is functioning as expected in the local setup.
I see that it needs to get the primary node for db writes (
node-a.singlenode-mongodb.qa.shared.eu.qa.aws.app.io in this case). My understanding is supplying a new instance of WritableServerSelector in the ClusterSettings while creating the client will be enough to select primaries using the selector's select method and it is proving wrong with this problem.
Chandra.