如何配置和使用两个数据源?

例如,这是我的第一个数据源:

application.properties

#first db
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver

#second db ...

应用程序类

@SpringBootApplication
public class SampleApplication
{
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}

如何修改应用。属性添加另一个数据源?我如何自动装配它以供不同的存储库使用?


当前回答

参考官方文件


创建多个数据源的工作方式与创建第一个数据源相同。如果您正在为JDBC或JPA使用默认的自动配置,那么您可能希望将其中一个标记为@Primary(然后任何@Autowired注入都将接收该配置)。

@Bean
@Primary
@ConfigurationProperties(prefix="datasource.primary")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="datasource.secondary")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

其他回答

在Spring Boot application.properties中声明一个数据源

spring.datasource.company.url=jdbc:mysql://localhost/company_db?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.company.username=root
spring.datasource.company.password=root
spring.datasource.company.platform=mysql


spring.datasource.employee.url=jdbc:mysql://localhost/employee_db?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.employee.username=root
spring.datasource.employee.password=root
spring.datasource.employee.platform=mysql

使用多个数据源时,我们需要声明多个bean Spring应用程序上下文中的不同映射。 使用配置类


@Configuration
@EnableJpaRepositories(basePackages = "com.example.multiple.datasources.entity.company",
        entityManagerFactoryRef = "companyEntityManagerFactory",
        transactionManagerRef = "companyTransactionManager")
public class CompanyDataSourceConfiguration {

    @Bean
    @ConfigurationProperties("spring.datasource.company")
    public DataSourceProperties companyDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.company.configuration")
    public DataSource companyDataSource() {
        return companyDataSourceProperties().initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();
    }

    @Bean(name = "companyEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean companyEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(companyDataSource()).packages(Company.class).build();
    }

    @Bean
    public PlatformTransactionManager companyTransactionManager(
            final @Qualifier("companyEntityManagerFactory") LocalContainerEntityManagerFactoryBean companyEntityManagerFactory
    ) {
        return new JpaTransactionManager(companyEntityManagerFactory.getObject());

    }


}

我们需要声明其中一个数据源为@Primary。这是因为 EntityManagerFactoryBuilder在jpbasaseconfiguration和中声明 该类需要注入单个数据源。


@Configuration
@EnableJpaRepositories(basePackages = "com.example.multiple.datasources.entity.employee",
        entityManagerFactoryRef = "employeeEntityManagerFactory",
        transactionManagerRef = "employeeTransactionManager")
public class EmployeeDatasourceConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.employee")
    public DataSourceProperties employeeDataSourceProperties() {
        return new DataSourceProperties();

    }

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.employee.configuration")
    public DataSource employeeDataSource() {
        return employeeDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    @Primary
    @Bean("employeeEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean employeeEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(employeeDataSource()).packages(Employee.class).build();
    }

    @Primary
    @Bean
    public PlatformTransactionManager employeeTransactionManager(
            final @Qualifier("employeeEntityManagerFactory") LocalContainerEntityManagerFactoryBean employeeEntityManagerFactory
    ) {
        return new JpaTransactionManager(employeeEntityManagerFactory.getObject());

    }

}

2022-05-29更新Spring Boot 1.5.8。RELEASE应该与Spring Boot 2.x一起工作

大多数答案不提供如何使用它们(作为数据源本身和作为事务),只提供如何配置它们。

此外,您应该知道如何同时提交/回滚两个数据源的事务。

您可以在https://github.com/surasint/surasint-examples/tree/master/spring-boot-jdbi/10_spring-boot-two-databases中看到可运行的示例和一些解释(看看您可以在README.txt中尝试什么)

我在这里复制了一些代码。

首先你必须设置应用程序。像这样的属性

#Database
database1.datasource.url=jdbc:mysql://localhost/testdb
database1.datasource.username=root
database1.datasource.password=root
database1.datasource.driver-class-name=com.mysql.jdbc.Driver

database2.datasource.url=jdbc:mysql://localhost/testdb2
database2.datasource.username=root
database2.datasource.password=root
database2.datasource.driver-class-name=com.mysql.jdbc.Driver

然后像这样将它们定义为提供者(@Bean):

@Bean(name = "datasource1")
@ConfigurationProperties("database1.datasource")
@Primary
public DataSource dataSource(){
    return DataSourceBuilder.create().build();
}

@Bean(name = "datasource2")
@ConfigurationProperties("database2.datasource")
public DataSource dataSource2(){
    return DataSourceBuilder.create().build();
}

注意,我有@Bean(name="datasource1")和@Bean(name="datasource2"),那么当我们需要数据源时,你可以使用它作为@Qualifier("datasource1")和@Qualifier("datasource2"),例如

@Qualifier("datasource1")
@Autowired
private DataSource dataSource;

如果你真的关心事务,你必须为它们定义DataSourceTransactionManager,就像这样:

@Bean(name="tm1")
@Autowired
@Primary
DataSourceTransactionManager tm1(@Qualifier ("datasource1") DataSource datasource) {
    DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
    return txm;
}

@Bean(name="tm2")
@Autowired
DataSourceTransactionManager tm2(@Qualifier ("datasource2") DataSource datasource) {
    DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
    return txm;
}

然后你就可以用它

@Transactional //this will use the first datasource because it is @primary

or

@Transactional("tm2")

最重要的部分,你很难在任何地方找到一个例子:如果你想要一个方法来提交/回滚两个数据库的事务,你需要tm1和tm2的ChainedTransactionManager,如下所示:

@Bean(name = "chainedTransactionManager")
public ChainedTransactionManager getChainedTransactionManager(@Qualifier ("tm1") DataSourceTransactionManager tm1, @Qualifier ("tm2") DataSourceTransactionManager tm2){
    return new ChainedTransactionManager(tm1, tm2);
}

要使用它,可以在@Transactional(value="chainedTransactionManager")方法中添加该注释

@Transactional(value="chainedTransactionManager")
public void insertAll() {
    UserBean test = new UserBean();
    test.setUsername("username" + new Date().getTime());
    userDao.insert(test);

    userDao2.insert(test);
}

这应该足够了。参见上面链接中的示例和详细信息。

给你。

添加到您的应用程序中。属性文件:

#first db
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver

#second db ...
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver

在任何带@Configuration注解的类中添加以下方法:

@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

参考官方文件


创建多个数据源的工作方式与创建第一个数据源相同。如果您正在为JDBC或JPA使用默认的自动配置,那么您可能希望将其中一个标记为@Primary(然后任何@Autowired注入都将接收该配置)。

@Bean
@Primary
@ConfigurationProperties(prefix="datasource.primary")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="datasource.secondary")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}
# Here '1stDB' is the database name
spring.datasource.url=jdbc:mysql://localhost/A
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 
 
# Here '2ndDB' is the database name
spring.second-datasourcee.url=jdbc:mysql://localhost/B
spring.second-datasource.username=root
spring.second-datasource.password=root
spring.second-datasource.driver-class-name=com.mysql.jdbc.Driver
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource firstDataSource() {
    return DataSourceBuilder.create().build();
}
 
@Bean
@ConfigurationProperties(prefix = "spring.second-datasource")
public DataSource secondDataSource() {
   return DataSourceBuilder.create().build();
}