2

When I run the following simple code, I typically get a connection refused error... but 1 time out of 20 it will work randomly. Then continue working repeatedly for 2-3 minutes, then refuse connections again. I can't detect a pattern. I have looked at the other connection refused threads but they are using differing technologies that may or may not be complicating the situation (and unfortunately not every thread is even resolved).

I am completely new to Mongo and am following this guide: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/ . My goal is to have a stable connection so I can experiment/learn the DB. Any help with this matter is greatly appreciated! Keep in mind I'm completely new to this technology and don't know my way around it yet.

I am using JDK 1.7.0_25 and Eclipse. I have added mongo-driver-2.11.3.jar to my project's build path already. The following is my simple code, directly from the example on the site I listed.

package database;

import java.util.Set;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;

public class MongoPortal {

    /*static final String domain = "localhost";  
    static final int port = 27107;
    static final String database = "test";*/

    public boolean insert(){

        try {
            MongoClient mongoClient = new MongoClient( "localhost" , 27107 );

            DB db = mongoClient.getDB("test");

            // Get and print all the collections
            Set<String> colls = db.getCollectionNames();
            for (String s : colls)
                System.out.println(s);

            mongoClient.close();
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }
}

And the error I get:

Oct 13, 2013 9:12:50 AM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on localhost/127.0.0.1:27107
java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at com.mongodb.DBPort._open(DBPort.java:223)
    at com.mongodb.DBPort.go(DBPort.java:125)
    at com.mongodb.DBPort.go(DBPort.java:106)
    at com.mongodb.DBPort.findOne(DBPort.java:162)
    at com.mongodb.DBPort.runCommand(DBPort.java:170)
    at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:547)
    at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:526)
    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:236)
    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
    at com.mongodb.DB.getCollectionNames(DB.java:400)
    at database.MongoPortal.insert(MongoPortal.java:29)
    at driver.Driver.main(Driver.java:22)

Oct 13, 2013 9:12:50 AM com.mongodb.DBPortPool gotError
WARNING: emptying DBPortPool to localhost/127.0.0.1:27107 b/c of error
java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at com.mongodb.DBPort._open(DBPort.java:223)
    at com.mongodb.DBPort.go(DBPort.java:125)
    at com.mongodb.DBPort.call(DBPort.java:92)
    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
    at com.mongodb.DB.getCollectionNames(DB.java:400)
    at database.MongoPortal.insert(MongoPortal.java:29)
    at driver.Driver.main(Driver.java:22)

com.mongodb.MongoException$Network: Read operation to server localhost/127.0.0.1:27107 failed on database test
    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:253)
    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
    at com.mongodb.DB.getCollectionNames(DB.java:400)
    at database.MongoPortal.insert(MongoPortal.java:29)
    at driver.Driver.main(Driver.java:22)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at com.mongodb.DBPort._open(DBPort.java:223)
    at com.mongodb.DBPort.go(DBPort.java:125)
    at com.mongodb.DBPort.call(DBPort.java:92)
    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
    ... 6 more

When I type mongo in bash, the display I get is:

@debian:~$ mongo
MongoDB shell version: 2.4.6
connecting to: test
> 
donsiuch
  • 343
  • 3
  • 8
  • 18
  • Have you tried to connect with the mongo shell too? If yes did that had the same behaviour? – attish Oct 13 '13 at 15:04
  • Are you 100% sure that the server is running in the background, while it is somehow odd but Java driver through connection refuse in some cases when there is no server listening. Check this question:http://stackoverflow.com/questions/5939457/mongodb-java-connection-issues – attish Oct 13 '13 at 15:12
  • I seem to be able to connect. I type mongo and appear to be logged into the shell. I can issue commands and navigate around. Following is the initial shell display, followed by a field to enter text: MongoDB shell version: 2.4.6 connecting to: test – donsiuch Oct 13 '13 at 15:16
  • Then the problem is i assume the port number. While by default it listens on the 27017 but in the java code you try to connect on 27107. So is the port number correct? – attish Oct 13 '13 at 15:30

3 Answers3

3

while you can connect with the mongo command it is definite that the mongodb server listen on the port 27017 (while without parameters it tries to connect there). That means in the java code you have to change this line:

MongoClient mongoClient = new MongoClient( "localhost" , 27107 );

To this line:

MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

And i am not sure this behavior of the driver that give back connection refused if there is no server listening on a host:port config is something which is good. At least a bit misleading from my point of view.

Patrick Klug
  • 14,056
  • 13
  • 71
  • 118
attish
  • 3,090
  • 16
  • 21
  • You are 100% correct. Hours of fiddling with Mongo and the problem was a 10 01. The "sometimes works and sometimes doesn't" appears problems with copy and pasting information. Copy the wrong paste the right, copy the wrong paste the wrong... :) Thanks for your second set of eyes attish! – donsiuch Oct 13 '13 at 15:46
  • 1
    In our profession problems that one really suffering to resolve always like this. :) i cannot count how many times I did the same (hundreds) . I am glad that could help! – attish Oct 13 '13 at 15:53
1

Got the same error when mongodb was newly installed on our cluster. Code worked when i run the java program on the server mongodb was installed with localhost.

To run the code outside the cluster , got the connection refused error.

The issue was :

Port on which mongodb was installed was limited to localhost. We corrected and restarted the service and it worked perfect !!

yoga
  • 1,929
  • 2
  • 15
  • 18
0

I solved this error by giving a --dbpath to mongod and migrating my entire DB there.. Now everytime I start the server(mongod), I give --dbpath. Earlier I wasn't using a --dbpath..

Vivek Sethi
  • 894
  • 2
  • 13
  • 20