如何在Spring中将tomcat连接池属性添加到自定义DataSource?

时间:2017-11-14 11:17:12

标签: java spring tomcat spring-boot

我希望在我的应用程序中有多个DataSource。因此不能依赖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

问题:.tomcat.*属性不会自动获取。

问题:我怎样才能将它们带入DataSource

3 个答案:

答案 0 :(得分:2)

您需要创建多个数据源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);

而不是return DataSourceBuilder.create().build();,您需要返回上面创建的数据源

答案 1 :(得分:1)

在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

您的配置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();
}

我当前的Spring Boot版本是1.5.18

答案 2 :(得分:0)

我通过org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.Tomcat类查看spring boot是如何工作的,非常简单。

示例:

@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();
}

此属性将被提取:

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