mongodb连接池 [英] mongodb connection pooling
问题描述
我正在使用Java驱动程序访问mongodb.我以为数据库连接池是由驱动程序内部处理的,但是每次访问数据库时,连接计数都会增加.
I am using Java driver to access mongodb. I assume the db connection pooling is internally handled by the driver.But my connection count getting increased every time I access db.
这是我的serverStatus日志.
This is my serverStatus log.
"connections" : {
"current" : 276,
"available" : 543
}
我需要显式关闭mongo连接吗?我应该如何在Java中管理连接池?
Do I need to explicitly close mongo connections? how should I manage connection pooling in java?
推荐答案
您应该使用单个Mongo对象,这样它将为您提供缓冲.但是,如果确实使用多个对象,则确实需要显式调用.close()
.
You should use a single Mongo object, so it will do pooling for you. However, if you do use multiple objects, you do need to call .close()
explicitly.
来自: http://www.mongodb.org/display/DOCS/Java+Tutorial
Mongo类设计为线程安全的,并在它们之间共享 线程.通常,您只为一个给定的数据库集群创建1个实例 并在您的应用中使用它.如果出于某种原因您决定创建 许多mongo实例,请注意:
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:
所有资源使用限制(最大连接数等)适用于每个mongo 实例以处置实例,请确保调用mongo.close() 清理资源
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
这篇关于mongodb连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!