Is it OK to use one MongoDatabase instance throughout the web application?

I’m working on a web project in Java, and use mongodb-driver-sync 4.3.
Since there’s only one database in use, can I keep one MongoDatabase instance (as Singleton) for use in the whole web application while still utilizing the connection pool feature of the MongoClient ?

Yes, you can, though there is virtually no cost in creating a new MongoDatabase instance. So long as you cache the MongoClient you should be good.


1 Like

Thank you for your reply.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.