I am starting a MULTI-TENANT project in spring data mongodb. After reading this post, collection per tenant is a pretty decent approach in mongodb.
So how can I achieve this in Spring Data Mongo? Apparently, There's no out-of-box solution provided, but I thought I could tweak MongoTemplate by overriding determineCollectionName method but I assume its not a public nor protected method for purpose.
However, I set up database per tenant approach very easily by extending SimpleMongoDbFactory and using LocalThread variable by following the tips provided here.
So, the questions are:
- Is there any safe way I can override the collection and domain class name mappings? p.s: This should happen at runtime, so I think @collection annotation won't help at all.
- If the collection per tenant is impossible, how much performance and resource penalties will I get for taking multi db approach?
You could extend MappingMongoEntityInformation to override getCollectionName(). The repository operations call getCollectionName() on each operation. I'm assuming the tenantId would be a ThreadLocal
And the overridden class with the constructors omitted.
Then create your repository: