java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect

JIRA | Alan Williamson | 4 years ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    The Java driver is great when everything goes right. However, the weakness can be seen when things start to go wrong. Let me illustrate with an example. A very simple one ... detecting when the server is available. You can't! Say for example you wanted to make sure the server was available (maybe you start your client before the server, or the server goes away for maintenance for a few moments (backup)). Take the following piece of code: try{ mongo.getConnector().requestEnsureConnection(); }catch(Exception e){ // never thrown } Why is the following exception (see below) masked? It is logged out to the JUL but that is of no use to the calling application. The mongo.getConnector().isOpen() also suffers the same fate. Why on earth are these exceptions masked? This is Java 101. WARNING: Exception executing isMaster command on /127.0.0.1:27017 java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect at com.mongodb.DBPort._open(DBPort.java:214) at com.mongodb.DBPort.go(DBPort.java:107) at com.mongodb.DBPort.go(DBPort.java:88) at com.mongodb.DBPort.findOne(DBPort.java:143) at com.mongodb.DBPort.runCommand(DBPort.java:148) at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548) at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:527) at com.mongodb.DBTCPConnector.requestEnsureConnection(DBTCPConnector.java:129) at com.profiler.ProfilerExtension.run(ProfilerExtension.java:126) at java.lang.Thread.run(Thread.java:619) So the question is, how does one handle the fact a server has gone away? What is the magic here to determine what is what?

    JIRA | 4 years ago | Alan Williamson
    java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect

    Root Cause Analysis

    1. java.io.IOException

      couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect

      at com.mongodb.DBPort._open()
    2. MongoDB Java Driver
      DBTCPConnector.requestEnsureConnection
      1. com.mongodb.DBPort._open(DBPort.java:214)
      2. com.mongodb.DBPort.go(DBPort.java:107)
      3. com.mongodb.DBPort.go(DBPort.java:88)
      4. com.mongodb.DBPort.findOne(DBPort.java:143)
      5. com.mongodb.DBPort.runCommand(DBPort.java:148)
      6. com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
      7. com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:527)
      8. com.mongodb.DBTCPConnector.requestEnsureConnection(DBTCPConnector.java:129)
      8 frames
    3. com.profiler
      ProfilerExtension.run
      1. com.profiler.ProfilerExtension.run(ProfilerExtension.java:126)
      1 frame
    4. Java RT
      Thread.run
      1. java.lang.Thread.run(Thread.java:619)
      1 frame