Druid - SpringBoot配置数据库连接池druid

  • 关于Druid

    DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池,是目前最好的连接池。

  • yml文件配置

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    #下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大
    initialSize: 10
    minIdle: 2
    maxActive: 30
    #配置获取连接等待超时的时间
    maxWait: 6000
    #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    #配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    #打开PSCache,并且指定每个连接上PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall
    #通过connectProperties属性来打开mergeSql功能;慢SQL记录
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    #合并多个DruidDataSource的监控数据
    #useGlobalDataSourceStat: true
    #druid recycle
    removeAbandoned: true
    removeAbandonedTimeout: 100
    logAbandoned: false
  • 配置类

        配置自己的datasource:

        

@Autowired
    private Environment environment;


    @Bean(name = "myDatasource")
    public DataSource datasource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(environment.getProperty("datasource.my.jdbcUrl"));
        dataSource.setUsername(environment.getProperty("datasource.my.username"));
        dataSource.setPassword(environment.getProperty("datasource.my.password"));
        dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));
        dataSource.setInitialSize(environment.getProperty("spring.datasource.initialSize", Integer.class));
        dataSource.setMinIdle(environment.getProperty("spring.datasource.minIdle", Integer.class));
        dataSource.setMaxActive(environment.getProperty("spring.datasource.maxActive", Integer.class));
        dataSource.setMaxWait(environment.getProperty("spring.datasource.maxWait", Long.class));
        dataSource.setTimeBetweenEvictionRunsMillis(environment.getProperty("spring.datasource.timeBetweenEvictionRunsMillis", Long.class));
        dataSource.setMinEvictableIdleTimeMillis(environment.getProperty("spring.datasource.minEvictableIdleTimeMillis", Long.class));
        dataSource.setValidationQuery(environment.getProperty("spring.datasource.validationQuery"));
        dataSource.setTestWhileIdle(environment.getProperty("spring.datasource.testWhileIdle", Boolean.class));
        dataSource.setTestOnBorrow(environment.getProperty("spring.datasource.testOnBorrow", Boolean.class));
        dataSource.setTestOnReturn(environment.getProperty("spring.datasource.testOnReturn", Boolean.class));
        dataSource.setPoolPreparedStatements(environment.getProperty("spring.datasource.poolPreparedStatements", Boolean.class));
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(environment.getProperty("spring.datasource.maxPoolPreparedStatementPerConnectionSize", Integer.class));
        dataSource.setConnectProperties(environment.getProperty("spring.datasource.connectionProperties", Properties.class));
        try {
            dataSource.setFilters(environment.getProperty("spring.datasource.filters"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }


    @Bean
    public SqlSessionFactory mySqlSessionFactory(@Qualifier("myDatasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }


    @Bean
    public SqlSessionTemplate mySqlSessionTemplate(@Qualifier("mySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
        return sqlSessionTemplate;
    }

 注意配置类中的属性注入时小心不要和配件文件对应错了。之前因为不小心把setMaxWait(environment.getProperty("spring.datasource.maxActive", Integer.class)),导致连接池在没有达到最大连接数时就经常报获取不到连接异常一个手误查了好久才查到。

猜你喜欢

转载自blog.csdn.net/b9x__/article/details/85093376