gpt4 book ai didi

java - 如何防止连接池在 mongodb 上使用 java 驱动程序关闭?

转载 作者:IT老高 更新时间:2023-10-28 13:35:54 27 4
gpt4 key购买 nike

我正在从 java 驱动程序 2.12.3 升级到 3.3.0。奇怪的是,收集池似乎突然“行动起来”。

我的设置如下:

在主线程中建立连接:

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();
}

对于每次数据库交互,我都会收到这样的警告:

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.

看起来好像连接池在一次交互后就关闭了。但为什么? 非常不解 有人有什么想法吗?

最佳答案

https://api.mongodb.com/java/3.1/com/mongodb/MongoClientOptions.html

查看链接。有几种方法可能对您有所帮助。查看 connectionconnection pool 的超时相关方法。


编辑:添加正确答案(在下面的评论中)

MongoClientOptions options = new MongoClientOptions.Builder().socketKeepAlive(true).build(); 
MongoClient client = new MongoClient("host", options);

关于java - 如何防止连接池在 mongodb 上使用 java 驱动程序关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39703240/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com