【问题标题】:What is the behavior of setting timeout in aggregation pipeline execution in MongoDB Java Driver?在 MongoDB Java Driver 中的聚合管道执行中设置超时的行为是什么?
【发布时间】:2017-05-24 15:08:21
【问题描述】:

我需要设置聚合管道执行的超时时间。我正在使用 MongoDB Java 驱动程序 3.2。我知道我必须使用的代码如下:

collection.aggregate(pipeline).maxTime(10, TimeUnit.SECONDS);

问题是,一旦达到超时,我无法在任何地方找到程序的行为。它会抛出异常吗?它是否会以静默方式终止并返回 null 结果?

MongoDB 官方文档什么也没说(参见cursor.maxTimeMS())。此外,Java API 不涉及任何特定行为(请参阅maxTime)。

这怎么可能?!

【问题讨论】:

  • 我希望它会返回一个空迭代。如果你想测试它,只需输入非常短的时间,这会强制立即超时,然后看看你会得到什么。
  • 我试过了。它似乎什么都不做。超时似乎不受尊重:(
  • 所以有一个小的超时它返回一个非空值?
  • @RAZ_Muh_Taz 超时时间超过抛出异常。有关详细信息,请参阅我的答案。

标签: java mongodb timeout aggregation-framework


【解决方案1】:

好的,我知道了。如果聚合管道的执行超过maxTime方法表示的时间,则抛出com.mongodb.MongoExecutionTimeoutException

异常的stacktrace如下:

com.mongodb.MongoExecutionTimeoutException: operation exceeded time limit

    at com.mongodb.connection.ProtocolHelper.createSpecialException(ProtocolHelper.java:157)
    at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:111)
    at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114)
    at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
    at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
    at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:206)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:112)
    at com.mongodb.operation.AggregateOperation$1.call(AggregateOperation.java:227)
    at com.mongodb.operation.AggregateOperation$1.call(AggregateOperation.java:223)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
    at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:223)
    at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:65)
    at com.mongodb.Mongo.execute(Mongo.java:772)
    at com.mongodb.Mongo$2.execute(Mongo.java:759)

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    相关资源
    最近更新 更多