我是否需要明确关闭连接? [英] Do I need to explicitly close connection?
问题描述
我在我的应用程序中持有一个 MongoClient
和 DB
的实例,每次我想要执行一些操作我调用 getCollection()
。
我想知道是否需要显式关闭连接,就像 JDBC中的connection.close()
。
I'm holding one instance of MongoClient
and DB
in my application, and every time that I want to execute some operation I call getCollection()
.
I'm wondering if I need to explicitly close the connection, just like connection.close()
in JDBC.
为了强调,我只有一个 MongoClient
实例。我的问题不是关闭 MongoClient
,而是关闭我认为在我调用 getCollection()
时打开的连接。
To emphasize, I have only one MongoClient
instance. My question is not about closing MongoClient
but closing the connections I believe it opens when I'm calling getCollection()
.
推荐答案
不,您不需要关闭与DB的连接 - 您唯一的连接是通过MongoClient并且如文档所述 - 它为您处理连接池。
No, you do not need to close connections to DB - your only connection is via MongoClient and as the documentation states - it handles connection pooling for you.
您想要清理的唯一资源是光标你应该 close()当你完成它。
The only resource that you would want to clean up would be a cursor which you should close() when you're done with it.
这篇关于我是否需要明确关闭连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!