Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoDB: cursor notimeout setting isn't working in java client

Tags:

java

mongodb

i set an 'notimeout' option to a dbcursor in java:

    BasicDBObject nearbyQueries = new BasicDBObject("$gt", 0)
            .append("$lte", 2);
    DBCursor trueClassInstances = locationsCollection.find(new BasicDBObject("distanceFromHotel", nearbyQueries)).addOption(Bytes.QUERYOPTION_NOTIMEOUT).limit(100000);
    double counter = 0;
    int currentPresent = 0;
    for (DBObject instance : trueClassInstances) {

        ...
    }

even with this option i set, this exception is thrown:

Exception in thread "main" com.mongodb.MongoCursorNotFoundException: Query failed with error code -5 and error message 'Cursor 1876954464377 not found on server XXXXXX:27017' on server XXXXXXXX:27017
    at com.mongodb.connection.GetMoreProtocol.receiveMessage(GetMoreProtocol.java:115)
    at com.mongodb.connection.GetMoreProtocol.execute(GetMoreProtocol.java:68)
    at com.mongodb.connection.GetMoreProtocol.execute(GetMoreProtocol.java:37)
    at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:155)
    at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:219)
    at com.mongodb.connection.DefaultServerConnection.getMore(DefaultServerConnection.java:194)
    at com.mongodb.operation.QueryBatchCursor.getMore(QueryBatchCursor.java:197)
    at com.mongodb.operation.QueryBatchCursor.hasNext(QueryBatchCursor.java:93)
    at com.mongodb.MongoBatchCursorAdapter.hasNext(MongoBatchCursorAdapter.java:46)
    at com.mongodb.DBCursor.hasNext(DBCursor.java:152)
    at locationExtraction.DistanceClassification.FeatureAnalyzer.main(FeatureAnalyzer.java:27)

FeatureAnalyzer.java:27 is the for loop line.

this problem appear in other project with similar setting...

what am i doing wrong? maybe my choice of 'for' loop instead of this kind of iteration can cause this strange behavior?

while(cursor.hasNext())
{
      DBObject next = cursor.next();
}

Thanks

like image 862
talmosko Avatar asked Nov 09 '22 07:11

talmosko


1 Answers

Looks like you are not able to process each batch within time limit. Try reducing batch size so that each batch could be consumed before time runs out. This should help. cursor.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT).batchSize(100)

like image 82
jbochniak Avatar answered Nov 15 '22 07:11

jbochniak