spring-projects / spring-projects/spring-data-mongodb

Introduce AbstractRoutingMongoDatabaseFactory

Open
#3,636 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

for: team-attention type: enhancement
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Hello 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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with SimpleMongoClientDatabaseFactory and its doGetMongoDatabase method, then trace how @Transactional obtains MongoDB sessions. The requested outcome is an official AbstractRoutingMongoDatabaseFactory that supports multiple MongoClient instances for tenant-specific credentials without the ClientSession mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb, spring
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.