我的 java mongodb 客户端不断收到以下异常:
Caused by: com.mongodb.MongoCursorNotFoundException: Query failed with error code -5 and error message 'Cursor 105639140478 not found on server server1:27017' 在服务器 server1:27017
代码如下:
MongoCollection<Document> db = (bunch of code to load up one of these objects)
FindIterable<Document> cur = db.find().projection(desiredFields).noCursorTimeout(true);
MongoCursor<Document> cursor = cur.iterator();
while(cursor.hasNext()) {
Document o = cursor.next();
doStuff(o);
}
我已经设置了 noCursorTimeout,但我仍然遇到异常。它在超时前处理了大约 110,000 条记录,因此它显然能够进行某种工作(否则它不会处理任何记录)但同时它也无法正常工作,因为我希望它在之后不会超时具体设置 noCursorTimeout(true)。
最佳答案
来自在线文档:
如果 session 空闲时间超过 30 分钟,MongoDB 服务器会将该 session 标记为已过期,并可能随时将其关闭。当 MongoDB 服务器关闭 session 时,它还会终止任何正在进行的操作并打开与 session 关联的游标。这包括配置了 noCursorTimeout 或大于 30 分钟的 maxTimeMS 的游标。
建议的解决方案是减少批量大小或手动刷新 session
对于返回游标的操作,如果游标可能空闲超过 30 分钟,请使用 Session.startSession() 在显式 session 中发出操作,并使用 refreshSessions 命令定期刷新 session
没有很好的记录,所以这里有一个例子:
BsonDocument sid = client.startSession().getServerSession().getIdentifier();
在迭代器循环中
database.runCommand(new Document("refreshSessions",List.of(sid)));
关于java - 即使在设置 noCursorTimeout 后也找不到 mongo 游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52296108/