I am using the latest spring-data-mongodb (1.1.0.M2) and the latest Mongo Driver (2.9.0-RC1). I have a situation where I have multiple clients connecting to my application a
So after much research and experimentation, I have concluded that this is not yet possibly with the current spring-data-mongodb
project. I tried baja's method above and ran into a specific hurdle. The MongoTemplate
runs its ensureIndexes()
method from within its constructor. This method calls out the the database to make sure annotated indexes exist in the database. The constructor for MongoTemplate
gets called when Spring
starts up so I never even have a chance to set a ThreadLocal
variable. I have to have a default already set when Spring
starts, then change it when a request comes in. This is not allowable because I don't want nor do I have a default database.
All was not lost though. Our original plan was to have each client running on its own application server, pointed at its own MongoDB
database on the MongoDB
server. Then we can provide a -Dprovider=
system variable and each server runs pointing only to one database.
We were instructed to have a multi-tenant application, hence the attempt at the ThreadLocal
variable. But since it did not work, we were able to run the application the way we had originally designed.
I believe there is a way though to make this all work, it just takes more than is described in the other posts. You have to make your own RepositoryFactoryBean
. Here is the example from the Spring Data MongoDB Reference Docs. You would still have to implement your own MongoTemplate
and delay or remove the ensureIndexes()
call. But you would have to rewrite a few classes to make sure your MongoTemplate
is called instead of Spring's
. In other words, a lot of work. Work that I would like to see happen or even do, I just did not have the time.
Thanks for the responses.