【发布时间】:2023-04-05 04:12:01
【问题描述】:
我用的是spring scheduler+spring data mongo。
现在我遇到一个问题。MongoTemple 上的 find() 方法会阻塞。
程序块开启
List<HistoricalDataModal> historicalList = mongoOperation.find(symbolQuery, HistoricalDataModal.class,"EibHistoricalDataInf");
更新完整代码:
我将 find 替换为 findOne,总是阻止 Java代码
public void realtime() {
try {
Query query = new Query();
List<ContractDetails> contractDetailList = mongoOperation.find(query, ContractDetails.class,"EibContractInf");
if(contractDetailList == null || contractDetailList.size() == 0) {
logger.error("contractdetail error");
return;
}
for(int index = 0;index < contractDetailList.size();index++) {
Query symbolQuery = new Query();
symbolQuery.addCriteria(Criteria.where("symbol").is(contractDetailList.get(index).contract().symbol()));
FundamentalModal fundamentalDetail = mongoOperation.findOne(symbolQuery, FundamentalModal.class,"EibFundamentalInf");
if(fundamentalDetail == null)
{
logger.error("fundamentalDetail error");
continue;
}
logger.debug("debug1");
logger.debug("debug");
/*List<*/HistoricalDataModal/*>*/ historicalList = mongoOperation.findOne(symbolQuery, HistoricalDataModal.class,"EibHistoricalDataInf");
logger.debug("debug");
if(historicalList == null /*|| historicalList.size() == 0*/) {
logger.error("historicalList error");
continue;
}
logger.debug("debug");
}
}catch (Exception e) {
// TODO: handle exception
}
}
日志输出: img
如果我将HistoricalDataModal替换为FundamentalModal,程序不会阻塞。所以这是HistoricalDataModal的问题。但我不知道为什么。
【问题讨论】:
-
如果你尝试 mongoOperation.findOne 会发生什么?它给你一个结果还是它仍然阻塞?
-
@DanielC。没有findOne,findOne不要阻塞。find方法会阻塞
-
假设数据库太大,那么一种可能的解决方案是使用 Pageable 来限制从 Mongo DB 获得的数据量。像这样 Pageable myPagable = new PageRequest(0, 10);然后将其设置为这样的查询:symbolQuery.with(myPagable);它将返回 10 个文档
-
@DanielC.我使用mongo-shell获取相同的集合,只返回4条记录。所以我认为这不是因为数据库很大。
-
还有一个问题,为什么if里面有continue语句?你因为某种原因而循环吗?您也可以尝试使用 System.out.println("debug") 而不是 logger.debug 来查看输出吗?