micronaut-data:多个数据源 [英] micronaut-data : multiple data sources

查看:308
本文介绍了micronaut-data:多个数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在yml中有多个数据库,如何获取国家/地区特定的存储库?我需要在运行时获取国家/地区名称,并基于国家/地区名称,我需要对该国家/地区数据库进行操作,而单个数据库将使用yml中的默认数据库存储库.

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.

:在多数据源场景中,@ io.micronaut.data.annotation.Repository批注可用于指定要使用的datsource配置.默认情况下,Micronaut Data将查找默认数据源.

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

错误:

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(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(){
    }
}

服务

@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();
     }
    }
}   

推荐答案

我不确定这是否对您有帮助,但是我可以根据您所说的方式想到一种方法-

I can't be sure if this helps you, but one way I could think of based on what you have said -

  1. 您已经为3个国家/地区配置了3个不同的数据源.

  1. You already have 3 different datasources configured, for 3 countries you have.

您也许可以定义3个单独的存储库类.每个标记为@Repository("india-db")等

You perhaps can define 3 separate Repository classes. Each marked as @Repository("india-db") etc

与@Transactional一起使用的是@TransactionAdvice("india-db")

Along with @Transactional also mark your method with @TransactionAdvice("india-db")

现在的代码将是

if(country.equals("india"){  
   indiaAppRepository.operation();

}

如果所有区域的代码都相同,并且区域设置之间没有冲突,则可以将操作移至父类或接口默认方法.

If the code is same across all regions, and no conflicts across regional setup, it could be possible to move the operation to a parent class or interface default method.

可能有更多方法可以执行此操作.但这就是我如何使用2个不同的数据源的方式.我正在使用Micronaut 2.0和micronaut-data 1.0.2 @TransactionAdvice似乎也只能在方法上工作.当我尝试将其添加到班级时,它没有工作.拦截器似乎只是在方法上寻找它.

There possibly are more ways to do this. But this is how I got it working with 2 different datasources. I am using Micronaut 2.0 and micronaut-data 1.0.2 @TransactionAdvice also seems to work only on methods. When I tried adding it on the class, it didnt work. The interceptor seems to be looking for it, only on the method.

这篇关于micronaut-data:多个数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆