- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
MongoIterable
有两个 forEach
方法:
Iterable.forEach(Consumer)
MongoIterable.forEach(Block)
在最新版本的 mongo java 驱动程序中,MongoIterable.forEach
已被弃用,并发出通知称用户应切换到 Iterable.forEach
。
但是,MongoIterable.forEach
方法是在主实现类中实现的,或多或少如下所示:
try (MongoCursor<TResult> cursor = iterator()) {
while (cursor.hasNext()) {
block.apply(cursor.next());
}
}
并且没有相应的 Iterable.forEach
重写。这意味着 Iterable.forEach
泄漏了一个未关闭的 MongoCursor
,因此,如果遵循弃用注释,则将通过更改来泄漏大量游标到Iterable.forEach
。
那么当替换泄漏游标对象时,为什么 MongoIterable.forEach
被标记为已弃用?
最佳答案
在 https://jira.mongodb.org/browse/JAVA-3046 ,说明了变化的主要原因:
The forEach(Block block) method on MongoIterable conflicts with the default forEach method added to java.lang.Iterable in Java 8. If you try to use either with a lambda expression you get a compiler error which forces you to cast the lambda to either Block or Consumer, which is not intuitive.
The default forEach method in Iterable, though, is dangerous to use with MongoIterable because it's not possible for it to force the MongoCursor to be closed in the case where Consumer.accept throws an exception, but in scope of JAVA-2010 we plan to address that. Given that, it makes sense to deprecate MongoIterable#forEach(Block block) so that it can be removed in the next major release and users will stop getting the compiler error.
关于java - 为什么 mongo 弃用了 MongoIterable.forEach(Block)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54579054/
我是一名优秀的程序员,十分优秀!