com.mongodb.MongoCursorNotFoundException: Query failed with error code
-5 and error message 'Cursor 18253096929 not found on server localhost:27017' on server localhost:27017 at
com.mongodb.connection.GetMoreProtocol.execute(GetMoreProtocol.java:90)
at
com.mongodb.connection.GetMoreProtocol.execute(GetMoreProtocol.java:49)
at
com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
at
com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:260)
at
com.mongodb.connection.DefaultServerConnection.getMore(DefaultServerConnection.java:225)
at
com.mongodb.operation.QueryBatchCursor.getMore(QueryBatchCursor.java:195)
at
com.mongodb.operation.QueryBatchCursor.hasNext(QueryBatchCursor.java:91)
at
com.mongodb.MongoBatchCursorAdapter.hasNext(MongoBatchCursorAdapter.java:46)
at com.downloader.DownloadHtml.saveHtml(DownloadHtml.java:75) at
com.downloader.DownloadHtml.main(DownloadHtml.java:23)
从你给的异常信息来看,这个错误并不是一开始就报的,而是在做GetMore的时候。就是说,这个游标开始的时候是正常执行的,后来失败了(验证下是不是我说的这样)。几点我能想到的可能原因:
游标闲置时间太长
参考关于游标的文档,
By default, the server will automatically close the cursor after 10 minutes of inactivity, or if client has exhausted the cursor. To override this behavior in the mongo shell, you can use the cursor.noCursorTimeout() method
虽然说的是shell,但是在Java驱动中也是同样的道理,对应的参数自己去找一下了。
自己配置的负载均衡
有些用户在mongodb前面加了keepalived或者其他类似功能的组件,导致请求被转发到了错误的结点上从而找不到之前的游标。mongodb配合驱动本身就可以做到高可用,没有必要在前面加任何负载均衡。
看看你是不是以上两种情况之一。