Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

292
Views
MongoDB multitenancy (Java): How to switch MongoDB databases, with different DB credentials, at run-time, using MongoClient?


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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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));
over 4 years ago · Santiago Trujillo Report

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!