springboot配置默认数据库连接池并解决初始连接未生效问题

目前Spring Boot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池。 

在springboot1.5之前默认tomcat连接池, 版本org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder的源码

private static final String[] DATA_SOURCE_TYPE_NAMES = 
new String[] { "org.apache.tomcat.jdbc.pool.DataSource", "com.zaxxer.hikari.HikariDataSource", 
"org.apache.commons.dbcp.BasicDataSource", // deprecated "org.apache.commons.dbcp2.BasicDataSource" 
};

spring boot 2.0的版本默认连接池为HikariCP;

接着我们配置 application.properties

# 驱动配置信息
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/mealsystem?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver

#连接池的配置信息
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
 

至此,小伙伴们以为配置已经完成了,但是当我们配置完这些发现并没有生效。可以通过show processlist 查看mysql 初始化连接上,发现并不是我们想要的连接数,

,最后 通过重写datasource方法 
数据库连接可以使用DataSource池进行自动配置。

import rt org.apache.tomcat.jdbc.pool.DataSource;
ie;
import rt org.springframework.boot.context.properties.ConfigurationtionProperties;
import rt org.springframework.context.annotation.Bean;
im;
import rt org.springframework.context.annotation.Configurationtion;
import rt org.springframework.context.annotation.PropertySourcource;


@Configuration
@ConfigurationProperties(prefix = "= "spring.datasource")

public class DataSourceConfig {

    private String url;
    private String username;
    private String password;
    private int maxTotal;
    private int minIdle;
    private int maxIdle;
    private int initialSize;
    private String logAbandoned;
    private String removeAbandoned;
    private String removeAbandonedTimeout;
    private String maxWaitMillis;
    private String timeBetweenEvictionRunsMillis;
    private String numTestsPerEvictionRun;
    private String minEvictableIdleTimeMillis;




    @Bean
    public DataSource getDataSource() {
        DataSource  dataSource = new DataSource();
           dataSource.setDriverClasClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl(url(url);
           dataSource.setUsername(ume(username);
           dataSource.setPassword(prd(password);
           dataSource.setMaxActive(ive(maxTotal);
           dataSource.setMinIdle(mie(minIdle);
           dataSource.setMaxIdle(mae(maxIdle);
           dataSource.setInitialSizlSize(initialSize);
        return dataSource;
    }

  //getset方法省略

}

在经过测试 ,发现mysql链接确实增加了,缠绕几天的问题终于解决了,豁然开朗

你可能感兴趣的:(小功能)