[英]How to add tomcat connection pool properties to custom DataSource in Spring?
I want to have multiple DataSource
in my application. 我想在我的应用程序中有多个
DataSource
。 Therefore cannot rely on the datasource autoconfiguration of Spring and have to create my own, like this: 因此,不能依靠Spring的数据源自动配置而不得不创建自己的数据源,如下所示:
@Bean
@Primary
@ConfigurationProperties("spring.datasource.custom")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
spring.datasource.custom.url=jdbc:mysql://localhost/tablename
spring.datasource.custom.username=root
spring.datasource.custom.password=rootpw
spring.datasource.custom.driver-class-name=com.mysql.jdbc.Driver
#the important part:
spring.datasource.custom.tomcat.test-on-borrow=true
spring.datasource.custom.tomcat.validation-query=SELECT 1
Problem: the .tomcat.*
properties are not automatically picked up. 问题:
.tomcat.*
属性不会自动获取。
Question: how can I get them into the DataSource
? 问题:如何将它们放入
DataSource
?
You need to create multiple Datasource beans with one of them being @Primary
and you can set tomcat connection pool properties like this 您需要创建多个数据源bean,其中一个是
@Primary
并且可以像这样设置tomcat连接池属性。
@Value("${spring.datasource.custom.tomcat.validation-query}")
private String validationQuery;
@Value("${spring.datasource.custom.tomcat.test-on-borrow}")
private boolean onBorrow;
org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
ds.setValidationQuery(validationQuery);
ds.setTestOnBorrow(onBorrow);
Instead of return DataSourceBuilder.create().build();
而不是
return DataSourceBuilder.create().build();
you need to return the datasource u created above 您需要返回上面创建的数据源
With the following lines in application.properties: 在application.properties中包含以下几行:
spring.datasource.custom.url=jdbc:mysql://localhost/tablename
spring.datasource.custom.username=root
spring.datasource.custom.password=rootpw
spring.datasource.custom.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.custom.type=org.apache.tomcat.jdbc.pool.DataSource
#the important part:
spring.datasource.custom.tomcat.test-on-borrow=true
spring.datasource.custom.tomcat.validation-query=SELECT 1
Your configuration beans should look like this: 您的配置bean应该如下所示:
@Bean
@Primary
@ConfigurationProperties("spring.datasource.custom")
public DataSourceProperties primaryDataSourceProperties() {
return new DataSourceProperties;
}
@Bean
@Primary
@ConfigurationProperties("spring.datasource.custom.tomcat")
public DataSource primaryDataSource() {
return primaryDataSourceProperties().initializeDataSourceBuilder().build();
}
My current Spring Boot version is 1.5.18 我当前的Spring Boot版本是1.5.18
I got this working very simply by looking how spring boot does it through the org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.Tomcat class. 通过查看org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.Tomcat类中的spring boot是如何完成的,我可以使其工作非常简单。
Example: 例:
@Bean
@Primary // or @Qualifier("foobar")
@ConfigurationProperties("foobar.datasource")
DataSourceProperties foobarDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary // or @Qualifier("foobar")
@ConfigurationProperties("foobar.datasource.tomcat")
public org.apache.tomcat.jdbc.pool.DataSource foobarDataSource(DataSourceProperties properties) {
org.apache.tomcat.jdbc.pool.DataSource dataSource = createDataSource(properties, org.apache.tomcat.jdbc.pool.DataSource.class);
DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(properties.determineUrl());
String validationQuery = databaseDriver.getValidationQuery();
if (validationQuery != null) {
dataSource.setTestOnBorrow(true);
dataSource.setValidationQuery(validationQuery);
}
return dataSource;
}
@SuppressWarnings("unchecked")
protected <T> T createDataSource(/*@Qualifier("foobar")*/ DataSourceProperties properties, Class<? extends DataSource> type) {
return (T) properties.initializeDataSourceBuilder().type(type).build();
}
And this properties will get picked up: 并且此属性将被提取:
foobar.datasource.tomcat.test-on-borrow=false
foobar.datasource.tomcat.test-while-idle=true
foobar.datasource.tomcat.validation-interval=5000
foobar.datasource.tomcat.<whateve other props tomcat's pool accepts>=xxx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.