0

I am trying to configure two mongos in my spring boot. But it always picks up only one here is my code.

Mongo 1 configuration

@Configuration
public class MongoOneConfiguration extends AbstractMongoConfiguration {

    @Override
    @Bean(name = "mongoOneClient")
    public MongoClient mongoClient() {
        return new MongoClient(new ServerAddress(mongoOnehostname, mongoOnehostport), 
                new MongoClientOptions.Builder()
                    .connectTimeout(mongoOneConnTimeoutMillis)
                    .build());
    }

    @Override
    public String getDatabaseName() {
        return mongoOneDatabaseName;
    }

    @Bean(name = "mongoOneTemplate")
    public MongoOperations mongoOperations() {
        try {
            return mongoTemplate();
        } catch (Exception e) {

        }
    }
}

Mongo 2 configuration

@Configuration
public class MongoTwoConfiguration extends AbstractMongoConfiguration {

    @Override
    @Bean(name = "mongoTwoClient")
    public MongoClient mongoClient() {
        return new MongoClient(new ServerAddress(mongoTwohostname, mongoTwohostport), 
                new MongoClientOptions.Builder()
                    .connectTimeout(mongoTwoConnTimeoutMillis)
                    .build());
    }

    @Override
    public String getDatabaseName() {
        return mongoTwoDatabaseName;
    }

    @Bean(name = "mongoTwoTemplate")
    public MongoOperations mongoOperations() {
        try {
            return mongoTemplate();
        } catch (Exception e) {

        }
    }
}

Dao 1:

@Component
public class MongoOneDao  {
    @Autowired
    @Qualifier("mongoOneTemplate")
    private MongoOperations mongoOneTemplate;


    public List<Person> getAll() {
        return mongoOneTemplate.findAll(Person.class);
    }
}

Dao 2:

@Component
public class MongoTwoDao  {
    @Autowired
    @Qualifier("mongoTwoTemplate")
    private MongoOperations mongoTwoTemplate;


    public List<Person> getAll() {
        return mongoTwoTemplate.findAll(Person.class);
    }
}

Whichever Dao I use MongoOneDao or MongoTwoDao , it always calls same database. Can you please help what I am missing.

PS: I dont want to use repositories.

Manas Saxena
  • 2,171
  • 6
  • 39
  • 58

2 Answers2

0

I think you should create 2 different MongoDbFactory instances in the configuration and use different factories to create different MongoTemplates .Take a look at this Link. It could help

atakansaltik
  • 91
  • 1
  • 3
0
@Configuration
public class MongoOneConfiguration {

    @Bean
    public MongoClient mongoOneClient() {
        return new MongoClient("localhost");
    }

    @Bean
    public MongoTemplate mongoOneTemplate() throws Exception {
        return new MongoTemplate(mongoOneClient(), "db1");
    }
}
 @Configuration
public class MongoTwoConfiguration {


    public MongoClient mongoTwoClient() {
        return new MongoClient("localhost");
    }

    @Bean
    public MongoTemplate mongoTwoTemplate() throws Exception {
        return new MongoTemplate(mongoTwoClient(), "db2");
    }
}

Define two different mongo template with different DBs, it should work.

sonus21
  • 5,178
  • 2
  • 23
  • 48
  • 1
    Getting error when trying this Parameter 0 of method mongoDbFactory in org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration required a single bean, but 2 were found: - mongoOneClient: defined by method 'mongoClient' in class path resource [com/mongo/MongoOneConfiguration.class] - mongoTwoClient: defined by method 'mongoClient' in class path resource [com/mongo/MongoTwoConfiguration.class] – – Manas Saxena Jun 17 '20 at 16:21
  • oh! If that's the case then do not generate bean for the 2nd client. – sonus21 Jun 18 '20 at 07:05
  • @ManasSaxena Disable Mongo data auto configurations, It should work. ```@SpringBootApplication(exclude = MongoDataAutoConfiguration.class)``` – Muaz Jun 20 '21 at 17:46