okisad
okisad

Reputation: 41

Spring boot mongo mongoSocketException

I am using spring boot for my rest api but I have a problem about mongo database. Application sometimes throws mongo socket exception and does not execute following codes, when endpoint which needs mongo operation is triggred. I assign true to value of socketkeepalive but it did bot solve my problem. How can I get rid of this problem and can you offer me spring boot mongo db configuration values that are suitable?

By the way, program is working properly. But sometimes it throws this exception.

Thanks

INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server **.***.***.***:42015
com.mongodb.MongoSocketOpenException: Exception opening socket
        at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
        at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
        at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57)
        at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
        ... 3 common frames omitted

Upvotes: 3

Views: 14011

Answers (2)

skrymir1
skrymir1

Reputation: 617

Got the same issue while running mongo inside a docker stack and i just missed the port mapping from 27017 (external) to 27017 (internal), so applying

    ports:
      - 27017:27017

to my docker-compose.yml file worked for me.

Upvotes: 1

sssanjaya
sssanjaya

Reputation: 589

  1. Spring Boot has a feature called "auto configuration". In this case, as soon as the Mongo driver is detected on the classpath, the MongoAutoConfiguration is activated with default values, which point to localhost:27017. If you don't want that behaviour, you can now either configure the properties for MongoDB (see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-mongodb for valid property keys) or disable the MongoAutoConfiguration:

    @SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

  2. Spring boot throws this exception when Mongo DB is not running. Please make sure that Mongodb is running. It got resolved for me after starting Mongo DB.

  3. you can check if mongoDB is running on 27017 is running or not. use this code in your terminal

    netstat -plntu

And Please show me your configurations file or properties file.

Upvotes: 10

Related Questions