After reading the mongo documentation that says each instance of a MongoClient handles its own pooling, how would I go about only having one instance across my whole application?
This seems like it could be a scenario for using a singleton bean, but this seems like it would defeat the purpose of connection pooling. If only one user would be able to access the bean that contains the MongoClient instance at a time, surely multiple connections in the pool would never be used at the same time.
Have I got my understanding of singletons wrong, or is that indeed the right way to go about it?
Since you are in a java ee environment, the best way to implement this would be to use CDI producers:
Then in every bean you want to use it in:
The
javadoc
says:So, when you create a singleton with the client in it. It can be re-used as mentioned in the Javadoc. No synchronization is required, since it is thread safe.
One of the implementations could be:
and use the client as, throughout the application.
Connection pooling
will be taken care by theMongoClient
as documented.or use the @singleton annotation:
Refer: http://tomee.apache.org/singleton-example.html