5


I am facing an issue regarding MongoDB multi-tenancy. I have two different mongoDB databases (db1 and db2). These both have different credentials.

db1 credentials:
userName: admin
password: passwd

db2 credentials:
userName: admin1
password: passwd1

I need to switch from one database to other at run-time. I have autowired mongoTemplate with db1 credentials, but now I am unable to update the template with db2 credentials. Is this possible? If yes, how? If not, please tell me any other way to switch the databases at run-time with different credentials.

Note that, I am aware of "SimpleMongoDbFactory". One can extend "SimpleMongoDbFactory" and can override "getDb" method and pass the required dbName in super.getDb("dbName") for multitenancy. But, this does not work with two databases with different credentials.

4
  • Why not create two separate mongo templates each referencing it's own simplemongodbfactory ? Commented Jan 3, 2017 at 14:55
  • This would work for 2 or fixed number of databases. But what if, the number of databases is not fixed? Commented Jan 4, 2017 at 7:25
  • Hi Sumit, I have same requirement if you have solved please guide me. Commented Oct 20, 2017 at 5:49
  • Hi Lakshaman, this is not possible by using Java MongoClient. You have to know the number of databases, before initializing mongoTemplate, as you can see in stackoverflow.com/a/41446383/2163876. One of the solutions is "taking the checkout of Java MongoClient and update the code according to our requirement". Commented Nov 1, 2017 at 6:46

2 Answers 2

4

What if you create a MongoCredential for each DB and pass them to a MongoClient that you pass to your SimpleMongoDbFactory

    MongoCredential credential1 = MongoCredential.createCredential("admin", db1, "password");
MongoCredential credential2 = MongoCredential.createCredential("admin1", db2, "password1");
    MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential1, credential2));
Sign up to request clarification or add additional context in comments.

8 Comments

How does this approach works with MongoDB Connection Pools? How many connections would be present at a time?
If I am not wrong the connection pooling is manage by the MongoClient internally. You can anyway pass to the MongoClient construction a MongoClientOptions (you can for example configure the number of connection per host).
Anyway what is really important for the connection pooling is that you have only one instance of your MongoClient for your application
Ok, Thanks! That was really helpful.
Note that multiple credentials per client is now deprecated and will be removed in a future version: jira.mongodb.org/browse/JAVA-2656
|
0

Create independent MongoTemplate instances each one with it's own credentials and select the appropriate in runtime.

Every connection is established using the credentials so if you change them on an existing connection you are essentially destroying the connection and creating a new one and would not be taking advantage of pooling.

2 Comments

Yeah! For two databases, it would work but for 'n' number of databases, is there a proper way for multi-tenancy along with connection pooling?
You would need to keep a some sort of map of MongoTemplate instances and select the appropriate on runtime. If n is large and only a few of the instances are expected to be active on any given time then i would make sure to set the minimum connection to 0 to avoid establishing unused connections to the server and potentially exhausting its resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.