I have a spring microservice application linked with mongodb atlas database, yesterday my project works fine but today I’m getting this error
2022-11-24 00:29:33.267 INFO 5588 --- [ngodb.net:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server ac-bl3jxj4-shard-00-02.wt2swod.mongodb.net:27017
com.mongodb.MongoSocketReadException: Exception receiving message
at com.mongodb.internal.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:707) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessageWithAdditionalTimeout(InternalStreamConnection.java:579) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:415) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:342) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:96) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:44) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:131) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.startHandshake(InternalStreamConnectionInitializer.java:73) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:182) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:152) ~[mongodb-driver-core-4.4.2.jar:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: java.net.SocketException: Socket is closed
at java.base/java.net.Socket.setSoTimeout(Socket.java:1154) ~[na:na]
at java.base/sun.security.ssl.BaseSSLSocketImpl.setSoTimeout(BaseSSLSocketImpl.java:637) ~[na:na]
at java.base/sun.security.ssl.SSLSocketImpl.setSoTimeout(SSLSocketImpl.java:75) ~[na:na]
at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:133) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:718) ~[mongodb-driver-core-4.4.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessageWithAdditionalTimeout(InternalStreamConnection.java:576) ~[mongodb-driver-core-4.4.2.jar:na]
... 10 common frames omitted
here is my application.properties file
spring.data.mongodb.uri=mongodb+srv://denver:*****@product-service.wt2swod.mongodb.net/?retryWrites=true&w=majority
spring.data.mongodb.database=product-service
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.application.name=product-service
server.port=0
I tried to add those properties but never worked
@SpringBootApplication(exclude = {
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
2
Answers
I would recommend you to – -> set socketKeepAlive to true (which is by default true in the latest versions of the native driver) -> set maxConnectionIdleTime < net.ipv4.tcp_keepalive_time in this way your JVM would reap the connection and not the other way around.
I had the same issue when I was connecting to the Atlas cluster through my local machine. I had to whitelist my IP address through the Atlas dashboard again.
Whitelist your current IP address in your mongo DB cluster
Check "Whitelist your current IP address in your mongo DB cluster" in this article for more information. https://baraksaidoff.medium.com/fixing-mongoserverselectionerror-while-connecting-mongodb-with-node-js-213aaf0bf596