Do I need to explicitly close the connection?

I keep one instance of MongoClient and DB in my application, and every time I want to perform some operation, I call getCollection() .
I am wondering if you need to explicitly close the connection, just like connection.close() in JDBC.

To emphasize, I have only one instance of MongoClient . My question is not closing MongoClient , but closing the connections, which I think opens when I call getCollection() .

+8
java mongodb
source share
2 answers

No, you do not need to close database connections - your only connection is through MongoClient, and as the documentation says, it processes the connection pool for you.

The only resource you want to clear is the cursor , which you must close () when you are done with it.

+10
source share

You should close if you have a lot of MongoClient.

The MongoClient instance is actually a pool of database connections; you only need one instance of the MongoClient class, even with multiple threads.

MongoClient.close () to clean resources

MongoClient.close () - closes the base connector, which, in turn, closes all open connections. After the call, this Mongolian instance can not last longer.

More details: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/

+1
source share

All Articles