I have multiple databases in yml, how to get country specific repository? i am getting country name at runtime and based on country name i need to do operation with that country database, with single database it taking default database repository from yml.
as per micronaut document : In multiple datasource scenario, the @io.micronaut.data.annotation.Repository annotation can be used to specify the datsource configuration to use. By default Micronaut Data will look for the default datasource.
my query remains same for all database, only db instance get selected at runtime for operations.
https://micronaut-projects.github.io/micronaut-data/snapshot/guide/index.html
Error:
Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations, io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations, io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations] yml
datasources: india-db: validationQuery: SELECT 1 FROM DUAL driverClass: oracle.jdbc.driver.OracleDriver url: url-1 autoCommit: true username: username password: password australia-db: validationQuery: SELECT 1 FROM DUAL driverClass: oracle.jdbc.driver.OracleDriver url: url-2 autoCommit: true username: password password: password japan-db: validationQuery: SELECT 1 FROM DUAL driverClass: oracle.jdbc.driver.OracleDriver url: url-3 autoCommit: true username: username password: password Repository
//@Repository(value = "india-db") @JdbcRepository(dialect = Dialect.ORACLE) public class AppRepository { private final JdbcOperations jdbcOperations; @Inject public AppRepository(JdbcOperations jdbcOperations) { this.jdbcOperations = jdbcOperations; } @Transactional public String operation(){ } } Service
@Singleton public class AppService { AppRepository appRepository; @Inject public AppService(AppRepository appRepository){ this.appRepository=appRepository; } public void method(String country){ if(country.equals("india"){ //do india operation with india-db appRepository.operation(); }else if(australia){ //do australia operation with australia -db appRepository.operation(); }else if(japan){ //do japan operation with japan-db appRepository.operation(); } else{ throw exception(); } } }