I have a Mongo Atlas free account that contains a db with a single collection. The client only reads the collections and retrieves rows, therefore I have created an account with read only access. This account is tested via compass GUI and works fine.
However I am facing problems when I am trying to connect via the mongo java API.
First code:
MongoClient mongoClient = MongoClients.create(this.mongoURI);
MongoDatabase database = mongoClient.getDatabase(this.database);
MongoCollection<Document> collection = database.getCollection(this.collectionName);
The snippet above worked fine while I was working in my localhost (a dockerized mongodb) setup.
Mongo Atlas suggested to use the following code in order to connect:
mongodb+srv://dev2:<password>@cluster0-ldxfb.mongodb.net/test?retryWrites=true
(For your reference please find the logs of the first code at the end of the question.)
I was never able to work properly with this string as the this.mongoURI value. The server never responded with data, each call raised an exception.
After hours struggling with this issue I was able to get some data back using this approach:
Second code:
MongoCredential credential = MongoCredential.createScramSha1Credential("dev2", "admin", "apass".toCharArray());
MongoClientSettings settings = MongoClientSettings.builder()
.credential(credential)
.retryWrites(false)
.applyToConnectionPoolSettings(builder ->
builder.maxConnectionIdleTime(60000, TimeUnit.MILLISECONDS))
.applyToSslSettings(builder -> builder.enabled(true))
.applyToClusterSettings(builder -> {
builder.hosts(Arrays.asList(
new ServerAddress("cluster0-shard-00-00-ldxfb.mongodb.net", 27017),
new ServerAddress("cluster0-shard-00-01-ldxfb.mongodb.net", 27017),
new ServerAddress("cluster0-shard-00-02-ldxfb.mongodb.net", 27017)
));
builder.requiredReplicaSetName("Cluster0-shard-0");
})
.build();
MongoClient mongoClient = MongoClients.create(settings);
MongoDatabase database = mongoClient.getDatabase(this.database);
MongoCollection<Document> collection = database.getCollection(this.collectionName);
Using this code, I am able to get data, even if the response time is >2seconds.
I wonder if someone else faced this issue and managed to connect and get data with less code.
Why the first code does not work? I know that there workarounds, however I want to understand why the code that mongo suggests, failed. LOGS
2019.04.02 10:17:01 INFO org.mongodb.driver.cluster Thread[nioEventLoopGroup-3-2,10,main]: Cluster created with settings {hosts=[127.0.0.1:27017], srvHost=cluster0-ldxfb.mongodb.net, mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500, requiredReplicaSetName='Cluster0-shard-0'}
2019.04.02 10:17:01 WARNING io.helidon.webserver.RequestRouting Thread[nioEventLoopGroup-3-2,10,main]: Default error handler: Unhandled exception encountered.
java.util.concurrent.ExecutionException: Unhandled 'cause' of this exception encountered.
at io.helidon.webserver.RequestRouting$RoutedRequest.defaultHandler(RequestRouting.java:408)
REMOVE SOME LINES
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:644)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:579)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:496)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:458)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:176)
at com.mongodb.internal.connection.AbstractMultiServerCluster.getDescription(AbstractMultiServerCluster.java:53)
at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:136)
at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:94)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:249)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:172)
at com.mongodb.client.internal.MongoCollectionImpl.executeCount(MongoCollectionImpl.java:249)
at com.mongodb.client.internal.MongoCollectionImpl.countDocuments(MongoCollectionImpl.java:218)
at com.mongodb.client.internal.MongoCollectionImpl.countDocuments(MongoCollectionImpl.java:213)
at io.lef.guides.se.rawina.Names.getMongoNameLastOrMiddle(Names.java:165)
at io.lef.guides.se.rawina.Names.getNameLastOrMiddle(Names.java:221)
at io.helidon.webserver.RequestRouting$RoutedRequest.next(RequestRouting.java:352)
... 29 more