0

I have the following Java code to save new entry to MongoDB if the entry is not in DB. I run it in Java Timer for every 2 seconds.

        MongoClient mongoClient = null;
        try {
            mongoClient = new MongoClient("localhost", 27017);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

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

        DBCollection coll = db.getCollection("testcollection");

        // Search for existing entries

        BasicDBObject query = new BasicDBObject("link", entry_url);

        DBCursor cursor = coll.find(query);
        try {
            // If it is a new entry, insert
            if (cursor.hasNext() == false) {
                // Insert new entry
                BasicDBObject doc = new BasicDBObject("link", entry_url)
                        .append("a_time", accept_time).append(
                                "p_time", formatter.format(date));
                coll.insert(doc); 
            }
        } finally {
            cursor.close();
        }

The problem is after several minutes, there is a com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting}] from mongoDB. It refers to cursor.hasNext(). Any suggestions for this problem?

Exception in thread "Timer-0" com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting}]
    at com.mongodb.BaseCluster.getDescription(BaseCluster.java:128)
    at com.mongodb.DBTCPConnector.getClusterDescription(DBTCPConnector.java:396)
    at com.mongodb.DBTCPConnector.getType(DBTCPConnector.java:569)
    at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:370)
    at com.mongodb.Mongo.isMongosConnection(Mongo.java:623)
    at com.mongodb.DBCursor._check(DBCursor.java:494)
    at com.mongodb.DBCursor._hasNext(DBCursor.java:621)
    at com.mongodb.DBCursor.hasNext(DBCursor.java:657)

The Timer implementation

         try {
            Timer timer = new Timer();
            timer.schedule(new STimer(), 0, 2 * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }

Based on the comments below, I closed the mongoClient connection. Problem solved.

3
  • Where is the Java Timer implementation? I think you need to ensure to close the mongoclient also Commented Nov 26, 2014 at 14:27
  • @Shervin, I just edited the post. Thanks for your suggestion to close the mongoClient connection. Commented Nov 26, 2014 at 14:32
  • Great! I will change the comment to answer, and then you can accept it so its easier to see what the answer is Commented Nov 27, 2014 at 7:27

1 Answer 1

2

You must also ensure MongoClient is properly closed

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

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.