-
关于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.get ();
}
@Bean
public SqlSessionTemplate mySqlSessionTemplate(@Qualifier(\"mySqlSessionFactory\") SqlSessionFactory sqlSessionFactory) {
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
return sqlSessionTemplate;
}
注意配置类中的属性注入时小心不要和配件文件对应错了。之前因为不小心把setMaxWait(environment.getProperty(\"spring.datasource.maxActive\", Integer.class)),导致连接池在没有达到最大连接数时就经常报获取不到连接异常。一个手误查了好久才查到。
继续阅读与本文标签相同的文章
上一篇 :
BIM工程师的能力,远比你想象的要大!
下一篇 :
5G未来终端设备可能只剩下显示屏?
-
大陆集团:仓储智慧化建设是我国物流行业降本增效最有效的途径之一
2026-05-18栏目: 教程
-
5G真的来了!2020年将在超340个城市覆盖5G
2026-05-18栏目: 教程
-
Windows 10计算器应用更新:完全支持三角函数运算
2026-05-18栏目: 教程
-
Docker容器实战(五) - 特殊的进程!
2026-05-18栏目: 教程
-
靠颜值进站!刷脸支付与轨道交通的大联合
2026-05-18栏目: 教程
