3

while I'm trying to read data from Mongodb I'm getting this Exception,

java.lang.IllegalStateException: open
    at org.bson.util.Assertions.isTrue(Assertions.java:36)
    at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:369)
    at com.mongodb.Mongo.isMongosConnection(Mongo.java:645)
    at com.mongodb.DBCursor._check(DBCursor.java:454)
    at com.mongodb.DBCursor._hasNext(DBCursor.java:546)
    at com.mongodb.DBCursor.hasNext(DBCursor.java:571)
    at com.calsoftlabs.mongo.client.impl.ReadingData.main(ReadingData.java:93)

MyCode

    public static DBCursor firstRecord(String ip) throws Exception {

        DBObject query = new BasicDBObject("client_ip", ip);

        DBCollection collection = getConnection();

        DBObject obj = new BasicDBObject("_id", 1);

        DBCursor cursor = collection.find(query).sort(obj).limit(1);

        mongo.close();

        return cursor;

    }
    DBCursor cursor = ReadingData.firstRecord(ip);

                    while (cursor.hasNext()) { 

                        cursor.next();

                        myList.add(new BasicDBObject("client_ip", (String) cursor.curr().get(
                                "client_ip")).append("timestamp",
                                (String) cursor.curr().get("timestamp")).append("total_traffic",
                                (String) cursor.curr().get("total_traffic")));

                    }

By using the above code I'm reading first record from set of records in Mongodb, but the line cursor.hasNext() throwing IllegalStateException. Please suggest me how to resolve this issue..

2
  • 2
    This mongo.close(); is suspicious, you're trying to use the db after it... Commented Feb 11, 2015 at 6:28
  • see also jira.mongodb.org/browse/JAVA-1251 Commented Feb 11, 2015 at 6:31

1 Answer 1

4

Thats why you call mongo.close(); befor calling cursor.hasNext()). Remove the mongo.close(); from firstRecord and add it after your loop

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.