How to keep the connection pool from closing with a java driver on a mongodb?(如何防止连接池在 mongodb 上使用 java 驱动程序关闭?)
问题描述
我正在从 java 驱动程序 2.12.3 升级到 3.3.0.奇怪的是,收集池似乎突然行动起来".
I'm in the middle of upgrading from java driver 2.12.3 to 3.3.0. Curiously it seems that the collection pool is suddenly "acting up".
我的设置如下:
在主线程中建立连接:
mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
mongoClient.setWriteConcern(new WriteConcern(0, 10)); // deprecated, replace soon
database = mongoClient.getDatabase("Example");
// java.util.logging.Logger.getLogger("org.mongodb.driver").setLevel(Level.SEVERE);
在数百个线程中使用:
org.bson.Document oldDoc = DBInteractions.readOneFromDb("articles");
使用这样的函数:
static synchronized Document readOneFromDb(String col) {
return database.getCollection(col).find().limit(1).sort(new Document().append("count", 1)).first();
}
对于每次数据库交互,我都会收到这样的警告:
And for every DB interaction I get such a warning:
Sep 26, 2016 2:33:19 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMATION: Closed connection [connectionId{localValue:42, serverValue:248}] to localhost:27017 because the pool has been closed.
看起来好像连接池在一次交互后就关闭了.但为什么?非常不解 有人有什么想法吗?
It looks as if the connection pool is closed after just one interaction. But why? very puzzled Anyone an idea?
推荐答案
https://api.mongodb.com/java/3.1/com/mongodb/MongoClientOptions.html
查看链接.有几种方法可能对您有所帮助.查看 connection 和 connection pool 的超时相关方法.
Look at the link. There are several method that can probably help you. Look into the timeout related methods for connection and connection pool.
编辑:添加正确答案(在下面的评论中)
EDIT: added the correct answer (it was in the comments below)
MongoClientOptions options = new MongoClientOptions.Builder().socketKeepAlive(true).build();
MongoClient client = new MongoClient("host", options);
这篇关于如何防止连接池在 mongodb 上使用 java 驱动程序关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何防止连接池在 mongodb 上使用 java 驱动程序关闭?
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01