Getting mongo cursor not found even after setting noCursorTimeout

Viewed 975

I keep getting the following exception from my java mongodb client:

Caused by: com.mongodb.MongoCursorNotFoundException: Query failed with error code -5 and error message 'Cursor 105639140478 not found on server server1:27017' on server server1:27017

The code is as follows:

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);
}

I have set the noCursorTimeout, but I am still getting the exception. It processes about 110,000 records before timing out, so it is clearly capable of sort-of working (otherwise it would process no records) but at the same time it isn't working properly either, because I would expect it to not time out after specifically setting noCursorTimeout(true).

1 Answers

From online docs:

If a session is idle for longer than 30 minutes, the MongoDB server marks that session as expired and may close it at any time. When the MongoDB server closes the session, it also kills any in-progress operations and open cursors associated with the session. This includes cursors configured with noCursorTimeout or a maxTimeMS greater than 30 minutes.

Proposed solution is to decrease batch size or manually refresh sessions

For operations that return a cursor, if the cursor may be idle for longer than 30 minutes, issue the operation within an explicit session using Session.startSession() and periodically refresh the session using the refreshSessions command

Not well documented, so here's an example:

BsonDocument sid = client.startSession().getServerSession().getIdentifier();

inside the iterator loop

database.runCommand(new Document("refreshSessions",List.of(sid)));
Related