Introduce AbstractRoutingMongoDatabaseFactory
See original GitHub issueHello maintainers!
I am currently using Spring Data MongoDB in a multi-tenant environment. To give a little context, as a security requirement from our customers, we need to have a different credential set for every tenant, which forces us to have multiple instances of MongoClient
.
We managed to make this work by overriding the method doGetMongoDatabase
from SimpleMongoClientDatabaseFactory
, which I believe the way people usually do it. However, we’re facing a really big issue with this way of doing thing: since we have multi instances of MongoClient
, using @Transactional
doesn’t work properly. We end up with this error
java.lang.IllegalStateException: state should be: ClientSession from same MongoClient
Would you happen to have a fix for that? Or is it in your roadmap to offer an official support for multi-tenancy?
Thank you for your help!
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Since this never was a priority this ticket has to line up in the queue. For now, the workaround would be to not use
@Transactional
infrastructure but rather make use of the programmaticClientSession
API onMongoTemplate.withSession(…)
where you control the session lifecycle.@divyajnu08 you might also want to have a look at https://github.com/spring-projects/spring-data-cassandra/tree/main/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/session/lookup that contains a Cassandra variant.