gpt4 book ai didi

java - mongodb 连接池

转载 作者:IT老高 更新时间:2023-10-28 13:18:09 26 4
gpt4 key购买 nike

我正在使用 Java 驱动程序访问 mongodb。我假设数据库连接池由驱动程序在内部处理。但是每次访问数据库时我的连接数都会增加。

这是我的服务器状态日志。

"connections" : {
"current" : 276,
"available" : 543
}

我是否需要明确关闭 mongo 连接?我应该如何管理java中的连接池?

最佳答案

您应该使用单个 Mongo 对象,因此它将为您进行池化。但是,如果您确实使用多个对象,则需要显式调用 .close()

发件人:http://www.mongodb.org/display/DOCS/Java+Tutorial

The Mongo class is designed to be thread safe and shared among threads. Typically you create only 1 instance for a given DB cluster and use it across your app. If for some reason you decide to create many mongo intances, note that:

all resource usage limits (max connections, etc) apply per mongo instance to dispose of an instance, make sure you call mongo.close() to clean up resources

关于java - mongodb 连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8968125/

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