在我的项目中,我们使用的是Spring Boot 2.0.3.Release。我们已经配置了多个数据源,默认情况下,Spring Boot选择Hikari数据源作为连接池。下面是我的代码。
application.properties
first.datasource.jdbcUrl=jdbc:sqlserver://localhost:1433;databaseName=test1
first.datasource.username=admin
first.datasource.password=admin
first.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDeriver
first.datasource.hikari.maximumlPoolSize=50
# Second Datasource
second.datasource.jdbcUrl=jdbc:postgresql://db-server-bar:5432/test2
second.datasource.username=admin
second.datasource.password=admin
second.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDeriver
second.datasource.hikari.maximumlPoolSize=50
FirstConfig.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = { "com.org.first.repo" }
)
public class FirstConfig {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "first.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.packages("com.org.first.entity")
.persistenceUnit("first")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
SecondConfig.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager",
basePackages = { "com.org.second.repo" }
)
public class SecondConfig{
@Bean(name = "barDataSource")
@ConfigurationProperties(prefix = "second.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "barEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
barEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("barDataSource") DataSource dataSource
) {
return
builder
.dataSource(dataSource)
.packages("com.org.second.entity")
.persistenceUnit("second")
.build();
}
@Bean(name = "barTransactionManager")
public PlatformTransactionManager barTransactionManager(
@Qualifier("barEntityManagerFactory") EntityManagerFactory
barEntityManagerFactory
) {
return new JpaTransactionManager(barEntityManagerFactory);
}
}
配置未选择我在属性文件中设置的最大池大小。当我签入显示为10的数据源对象时,如何覆盖在hikari数据源中设置的默认值?
答案 0 :(得分:0)
@Configuration
@ConfigurationProperties(prefix = "params.datasource")
public class JpaConfig extends HikariConfig {
@Bean
public DataSource dataSource() throws SQLException {
return new HikariDataSource(this);
}
}
application.yml
params:
datasource:
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDeriver
jdbcUrl: jdbc:sqlserver://localhost:1433;databaseName=test1
username: login
password: password
maximumPoolSize: 5
答案 1 :(得分:0)
如我所见,您尚未添加数据源属性bean。
请创建一个dataSourceProperties bean。
@Bean
@Primary
@ConfigurationProperties(prefix = "first.datasource")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "first.datasource")
public DataSource dataSource() {
return dataSourceProperties().initializeDataSourceBuilder().build();
}
这对我有用。