简体   繁体   中英

How to add tomcat connection pool properties to custom DataSource in Spring?

I want to have multiple DataSource in my application. Therefore cannot rely on the datasource autoconfiguration of Spring and have to create my own, like this:

        @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.

Question: how can I get them into the DataSource ?

You need to create multiple Datasource beans with one of them being @Primary and you can set tomcat connection pool properties like this

@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(); you need to return the datasource u created above

With the following lines in 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
@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

I got this working very simply by looking how spring boot does it through the org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.Tomcat class.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM