简体   繁体   English

mongoDB:游标notimeout设置在java客户端中不起作用

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

i set an 'notimeout' option to a dbcursor in java: 我在java中为dbcursor设置了'notimeout'选项:

    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. FeatureAnalyzer.java:27是for循环行。

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? 也许我选择'for'循环而不是这种迭代会导致这种奇怪的行为?

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

Thanks 谢谢

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)

so the problem is solved. 所以问题解决了。 this is very strange but there is a problem with using 'for' loop for iterating on cursor. 这很奇怪,但使用'for'循环迭代游标时出现问题。 so dont do it like i did it, use 'while' loop: 所以不要像我这样做,使用'while'循环:

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

before use cursor.hasNext() and cursor.next() to do business logical, just before you get the mongo cursor, invoke FindIterable object's noCursorTimeout(true) method. 在使用cursor.hasNext()和cursor.next()做业务逻辑之前,在获取mongo游标之前,调用FindIterable对象的noCursorTimeout(true)方法。 for example: 例如:

FindIterable<Document> findIterable = sentenceColl.find(condition);
// set no timeout
findIterable.noCursorTimeout(true);
MongoCursor<Document> mongoCursor = findIterable.iterator();

while (mongoCursor.hasNext()) {
    mongoCursor.next();
}

try this: 尝试这个:

Iterator<BasicDBObject> it = null;
it = coll.find("{"field": {$in:#}}", fieldList).with(
        new QueryModifier() {
          public void modify(DBCursor cursor) {
              cursor.setOptions(Bytes.QUERYOPTION_NOTIMEOUT);
          }
        }
    ).as(BasicDBObject.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM