2

I'm using mongo-java-driver-2.11.3.jar and also tried with mongo-2.10.1.jar both are giving the following error.The database is connected but the collection insert and selection are not working.

import java.net.UnknownHostException;
import java.util.Set;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;

public class mongo_test {
    public MongoClient mongo = null;
    public DB mongodb = null;

    private void mongo_startconnection() {
        try {
            mongo = new MongoClient("10.0.2.15", 27017);
            mongodb = mongo.getDB("foobar");
            System.out.println("Mongodb is connected");
            BasicDBObject doc = new BasicDBObject();
            DBCollection collection = mongodb.getCollection("url_ta");
            Set<String> colls = mongodb.getCollectionNames();
            for (String s : colls) {
                System.out.println(s);
            }
            System.out.println("done");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e1) {
            e1.printStackTrace();
        }
    }

    public static void main(String args[]) {
        mongo_test MT = new mongo_test();
        MT.mongo_startconnection();
    }
}

I'm getting this error:

Mongodb is connected
Nov 26, 2013 11:34:39 AM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on /10.0.2.15:27017
java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    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 mongo_test.mongo_startconnection(mongo_test.java:29)
    at mongo_test.main(mongo_test.java:69)
com.mongodb.MongoException$Network: Read operation to server /10.0.2.15:27017 failed on database foobar 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 mongo_test.mongo_startconnection(mongo_test.java:29)
    at mongo_test.main(mongo_test.java:69)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    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 
3
  • Can you try connecting to the mongo server using the mongo shell command on the same machine that you are running the application? I think you might have network issues that are preventing connection to it. Commented Nov 26, 2013 at 6:58
  • The mongo server is connected up and running but if I try to work on collections then I'm getting the above error Commented Nov 26, 2013 at 7:32
  • If that is the case, do you have multiple multiple MongoDB servers in a replicaSet configuration? Are you connecting to a slave server? Commented Nov 26, 2013 at 8:04

2 Answers 2

2

1.Trying to find out whether your ip address and port number is reachable. use command: telnet 10.0.2.15",27017 to check if such a port for this ip is open for connection.

2.If you have the authority to login the mongodb server ,just login and then:

  • If your mongodb is a cluster , Use commmand : ps -ef|grep mongos to check the mongodb server instance is up and the port is just 27017.

    if your mongodb is just a single machine ,use ps -ef|grep mongod to make the same check.

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

3 Comments

Thanks Vico,I have checked the both commands and mongodb is up,but when I try work with collections then I'm getting the above error.
Well ,I download you code and run it on my own machine , it works just fine.Driver version I use is 2.10.1.Just download it here:github.com/mongodb/mongo-java-driver/downloads
Very helpful, @Vico_Wu! This could be an answer.
0
package raamji.com.start;

//import com.mongodb.MongoClient;
import com.mongodb.Mongo;
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;

import java.net.UnknownHostException;
import java.util.Arrays;

public class Start {

    public static void main(String[] args) throws UnknownHostException {
        //crete Connection
        Mongo mongoClient = new Mongo("ind-asingh", 27017);//or "localhost",27017
        System.out.println("mongoClient :" + mongoClient);
        DB db = mongoClient.getDB("DB Name");//e.g. MYDB
        System.out.println("db :" + db);

//---------------------------Access your Collection-----------------------------
        DBCollection collection = db.getCollection("message");
        System.out.println("collection :" + collection);
        System.out.println("Message Count: " + collection.getCount());

        DBCursor cursor = collection.find();
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }
        } finally {
            cursor.close();
        }


    }//end pf psvm

}//end of class

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.