Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalStateException: open

Tags:

java

mongodb

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..

like image 956
naresh nandu Avatar asked Dec 03 '25 15:12

naresh nandu


1 Answers

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

like image 113
Jens Avatar answered Dec 05 '25 05:12

Jens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!