您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章给大家分享的是有关druid中怎么配置数据连接池,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency>
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
@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;
}
}
@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);
}
}
@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中怎么配置数据连接池,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。