I'm using Spring Data for Mongodb and sometimes I use the java driver this way :
DBCollection mycoll = mongoOperations.getCollection("mycoll");
Does it make sense to make the collection mycoll static and instantiate it through a Singleton?
I'm using Spring Data for Mongodb and sometimes I use the java driver this way :
DBCollection mycoll = mongoOperations.getCollection("mycoll");
Does it make sense to make the collection mycoll static and instantiate it through a Singleton?
You do not need a Singleton for this, you only need a Singleton if you can only ever have one instance and you need to manage that instance, see this post. The source code of the Java Mongo driver shows this is not the case for MongoDB collections. The 'getCollection` method only verifies if the collection exists in the collectionCache (a hashmap) and does not perform any database operations, so the overhead of calling it multiple times is minimal. See below the relevant code snippet from https://github.com/mongodb/mongo-java-driver
/**
* Gets a collection with a given name.
*
* @param name the name of the collection to return
* @return the collection
*/
public DBCollection getCollection(final String name) {
DBCollection collection = collectionCache.get(name);
if (collection != null) {
return collection;
}