连接池以及数据源的配置


 

配置连接池的信息

public class DruidProp {

    private String url;
    private String username;
    private String password;
    private String driverClassName;
    //初始化大小,最小,最大
    private Integer initialSize;
    private Integer minIdle;
    private Integer maxActive;
    //配置获取连接等待超时的时间
    private Long maxWait;
    //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    private Long timeBetweenEvictionRunsMillis;
    //配置一个连接在池中最小生存的时间,单位是毫秒
    private Long minEvictableIdleTimeMillis;
    private String validationQuery;
    private Boolean testWhileIdle;
    private Boolean testOnBorrow;
    private Boolean testOnReturn;
    //打开PSCache,并且指定每个连接上PSCache的大小
    private Boolean poolPreparedStatements;
    private Integer maxPoolPreparedStatementPerConnectionSize;
    //配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    private String filters;

    @PostConstruct
    public void init(){
        //默认用mysql
        if(driverClassName == null) driverClassName = "com.mysql.jdbc.Driver";
        if(initialSize == null) initialSize = 8;
        if(minIdle == null) minIdle = 1;
        if(maxActive == null) maxActive = 20;
        if(maxWait == null) maxWait = 60000L;
        if(timeBetweenEvictionRunsMillis == null) timeBetweenEvictionRunsMillis = 60000L;
        if(minEvictableIdleTimeMillis == null) minEvictableIdleTimeMillis = 300000L;
        if(validationQuery == null) validationQuery = "select 1 from dual";
        if(testWhileIdle == null) testWhileIdle = true;
        if(testOnBorrow == null) testOnBorrow = false;
        if(testOnReturn == null) testOnReturn = false;
        if(poolPreparedStatements == null) poolPreparedStatements = true;
        if(maxPoolPreparedStatementPerConnectionSize == null) maxPoolPreparedStatementPerConnectionSize = 20;
        if(filters == null) filters = "stat, wall";

    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

   .......get.....set
}

 

DruidConfig类通过注解@ConfigurationProperties获取数据库连接信息,并通过dataSource()方法设置数据源信息。

 

druidcfg:
  datasource:
    url: jdbc:mysql://url:3306/gcloud-seccenter?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true
    username: root
    password: root

 

/*

 */
@Configuration
public class DruidConfig {

    @Bean
    @ConfigurationProperties(prefix = "druidcfg.datasource")
    public DruidProp druidProp(){
        return new DruidProp();
    }

    @Bean
    @Primary
    public DataSource dataSource(DruidProp druidProp) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(druidProp.getUrl());
        dataSource.setUsername(druidProp.getUsername());
        dataSource.setPassword(druidProp.getPassword());
        dataSource.setDriverClassName(druidProp.getDriverClassName());
        dataSource.setInitialSize(druidProp.getInitialSize());
        dataSource.setMinIdle(druidProp.getMinIdle());
        dataSource.setMaxActive(druidProp.getMaxActive());
        dataSource.setTimeBetweenEvictionRunsMillis(druidProp.getTimeBetweenEvictionRunsMillis());
        dataSource.setMinEvictableIdleTimeMillis(druidProp.getMinEvictableIdleTimeMillis());
        dataSource.setValidationQuery(druidProp.getValidationQuery());
        dataSource.setTestWhileIdle(druidProp.getTestWhileIdle());
        dataSource.setTestOnBorrow(druidProp.getTestOnBorrow());
        dataSource.setTestOnReturn(druidProp.getTestOnReturn());
        dataSource.setPoolPreparedStatements(druidProp.getPoolPreparedStatements());
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(druidProp.getMaxPoolPreparedStatementPerConnectionSize());
        try {
            dataSource.setFilters(druidProp.getFilters());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值