druid中怎么配置数据连接池
本篇文章给大家分享的是有关druid中怎么配置数据连接池,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

为沂南等地区用户提供了全套网页设计制作服务,及沂南网站建设行业解决方案。主营业务为成都网站设计、网站制作、沂南网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
springboot 项目
pom.xml druid 部分
com.alibaba druid-spring-boot-starter 1.1.21
application.xml druid 部分
spring: ####整合数据库层 datasource: driver-class-name: com.MySQL.cj.jdbc.Driver name: demo url: jdbc:mysql://127.0.0.1:3306/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource druid: # 初始连接数 initialSize: 5 # 最小连接池数量 minIdle: 10 # 最大连接池数量 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最大生存的时间,单位是毫秒 maxEvictableIdleTimeMillis: 900000 # 配置检测连接是否有效 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false webStatFilter: enabled: true statViewServlet: enabled: true # 设置白名单,不填则允许所有访问 allow: url-pattern: /druid/* # 控制台管理用户名和密码 login-username: easy login-password: 1 filter: stat: enabled: true # 慢SQL记录 log-slow-sql: true slow-sql-millis: 20000 merge-sql: true wall: config: multi-statement-allow: true
DruidDataSourceDecorator.java 读取yaml 配置,修饰 DataSource
@Configuration
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DruidDataSourceDecorator {
// 读取 Druid 配置
// 初始连接数
private int initialSize;
// 最小连接池数量
private int minIdle;
// 最大连接池数量
private int maxActive;
// 配置获取连接等待超时的时间
private int maxWait;
// 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
private int timeBetweenEvictionRunsMillis;
// 配置一个连接在池中最小生存的时间,单位是毫秒
private int minEvictableIdleTimeMillis;
// 配置一个连接在池中最大生存的时间,单位是毫秒
private int maxEvictableIdleTimeMillis;
// 配置检测连接是否有效
private String validationQuery;
//
private boolean testWhileIdle;
//
private boolean testOnBorrow;
//
private boolean testOnReturn;
省略 set、get ...
public DruidDataSource decorat(DruidDataSource source) {
/** 配置初始化大小、最小、最大 */
source.setInitialSize(initialSize);
source.setMaxActive(maxActive);
source.setMinIdle(minIdle);
/** 配置获取连接等待超时的时间 */
source.setMaxWait(maxWait);
/** 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 */
source.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
/** 配置一个连接在池中最小、最大生存的时间,单位是毫秒 */
source.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
source.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis);
/**
* 用来检测连接是否有效的sql,要求是一个查询语句,常用select
* 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
*/
source.setValidationQuery(validationQuery);
/**
* 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
*/
source.setTestWhileIdle(testWhileIdle);
/** 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */
source.setTestOnBorrow(testOnBorrow);
/** 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */
source.setTestOnReturn(testOnReturn);
return source;
}
}DataSourceConfig.java 数据源配置
@Configuration
public class DataSourceConfig {
// 修饰一下 DataSource
@Autowired
private DruidDataSourceDecorator decorator ;
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
DataSource dataSource() {
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return decorator.decorat(dataSource);
}
}mybaties 配置
@Configuration
@MapperScan(basePackages = "com.xxx.mapper", sqlSessionTemplateRef = "sqlSessionTemplate")
public class HySessionFactory {
@Resource(name = "dataSource")
DataSource dataSource;
SqlSessionFactory hySqlSessionFactory() {
SqlSessionFactory sessionFactory = null;
try {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// 扫描的XML
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
sessionFactory = bean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return sessionFactory;
}
@Bean
SqlSessionTemplate sqlSessionTemplate() {
return new SqlSessionTemplate(sqlSessionFactory());
}
}以上就是druid中怎么配置数据连接池,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
当前题目:druid中怎么配置数据连接池
文章源于:http://www.jxjierui.cn/article/jccohp.html


咨询
建站咨询
