Should my MongoDB DAOs request unique database objects?

I am using the standard MongoDB Java driver to collapse my own DAOs. I know that all my DAOs can use the same Mongo instance, but should all my DAOs accessing the same database have to use the same DB object, or is there any good reason to request a new DB object or each?

Thanks!

+4
source share
2 answers

You can use generic instances of the Mongo class, the DB class, and the DBCollection class if this is practical for you. Regardless of whether you want in terms of design, it is up to you. I would definitely use Mongo instances as singlets, as they are relatively heavy (they have their own thread pool, etc.).

+2
source

The "good reason" for sharing a Mongo object is the built-in connection pool. If it is not practical to share your instance of the Mongo object between the DAO, then this is a good reason (in my opinion) to create new instances. If practical, you should share it.

Remember that you must use .close() when you are finished using the Mongo instance so as not to leave open connections.

+1
source

Source: https://habr.com/ru/post/1392965/


All Articles