It is said that there should be one Mongo
instance per JVM. I'm trying to setup Mongo
in JEE6 app and so far I have it done like this.
@Startup
@Singleton // EJB not CDI singleton
public class MongoDatabase {
private Mongo mongo;
@PostConstruct
public void init() throws Exception {
mongo = new Mongo();
}
@PreDestroy
public void shutdown() {
mongo.close();
}
@Produces
public DBCollection getProjectsCollection() {
return mongo.getDB("testdatabase").getCollection("mycollection");
}
}
This is ok, I can @Inject
it to all CDI services I want and starts at app startup, but it is not one per JVM, it is one per application. If I have several apps deployed on my app server all of them could setup its own Mongo
if needed.
How to setup one Mongo
per server in JEE6 so that all apps deployed can have access to the same connection pool (something similar to datasources to RDBMS)? Can I register it in JNDI somehow? Or maybe it is not needed as per application is enough?