gpt4 book ai didi

java - Spring boot - 多个DataSource的连接池属性

转载 作者:行者123 更新时间:2023-12-02 02:14:28 26 4
gpt4 key购买 nike

我正在使用 Spring Boot 版本 1.5.2,并且我有一个用例,我需要在应用程序中配置 2 个数据源。我已经成功让应用程序连接到 2 个数据库,但是我无法为它们设置连接池属性。

这是配置类:

@Configuration
public class DataSourceConfig {

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

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

在调试 DataSourceBuilder 时,我可以看到它正在实例化 org.apache.tomcat.jdbc.pool.DataSource 类型的 bean,这正是我想要的,但是 DataSource 对象中的 poolProperties 字段始终包含一些默认属性,而不包含一些默认属性我的意图是什么。

这是我的 application.yml:

spring:
profiles: dev

datasource:
one:
url: jdbc:mariadb://localhost:3306/one
username: user1
password: password
driverClassName: org.mariadb.jdbc.Driver
initialize: true
tomcat:
testOnBorrow: true
validation-query: SELECT 1
testWhileIdle: true
continueOnError: true
initialSize: 2
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 5000
minIdle: 2
maxIdle: 3
maxActive: 30
two:
url: jdbc:mariadb://localhost:3306/two
username: user1
password: password
driverClassName: org.mariadb.jdbc.Driver
initialize: true
tomcat:
testOnBorrow: true
validation-query: SELECT 1
testWhileIdle: true
continueOnError: true
initialSize: 2
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 5000
minIdle: 2
maxIdle: 3
maxActive: 30

我错过了什么吗? DataSourceBuilder 是否能够查找以这种方式配置的池属性?使用我所需的池属性配置 2 个数据源的最佳方法是什么?

最佳答案

首先,您应该忽略 DataSourceAutoConfiguration:

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

然后,将您的配置保留在您的问题中。如果你使用mybatis作为DAO框架,你应该添加这些配置,如下所示:

@Configuration
@MapperScan(basePackages = {"mapperpackage1"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {

@Autowired
@Qualifier("oneDataSource")
private DataSource ds1;


@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds1);

return factoryBean.getObject();

}

@Bean
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1());
return template;
}
}

同样:

@Configuration
@MapperScan(basePackages = {"mapperpackage2"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbAConfig2 {

@Autowired
@Qualifier("twoDataSource")
private DataSource ds2;


@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ds2);

return factoryBean.getObject();

}

@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
return template;
}
}

关于java - Spring boot - 多个DataSource的连接池属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49502046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com