3

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
1
  • Its better to edit your question with exception trace Commented Mar 6, 2018 at 21:08

2 Answers 2

10
  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.

Sign up to request clarification or add additional context in comments.

2 Comments

Firstly thanks for answer. Actually I connect to remote mongo database not at localhost, I have only three lines mongo configuration these are spring.data.mongodb.host=** spring.data.mongodb.port=42015 spring.data.mongodb.database=**
For the configuration file approach, see here: stackoverflow.com/a/49980868/3795043
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.