Hikaricp源码解读(2)——配置介绍及对应源码
来源:互联网 发布:汽车修理app软件 编辑:程序博客网 时间:2024/06/05 15:58
2、配置使用
HikariCP的配置类HikariConfig对Properties有很好的兼容,可通过配置环境变量hikaricp.configurationFile
设置配置文件路径。
String systemProp = System.getProperty("hikaricp.configurationFile");if (systemProp != null) { loadProperties(systemProp);}
或
public HikariConfig(String propertyFileName){ this(); loadProperties(propertyFileName);}private void loadProperties(String propertyFileName){ final File propFile = new File(propertyFileName); try (final InputStream is = propFile.isFile() ? new FileInputStream(propFile) : this.getClass().getResourceAsStream(propertyFileName)) { if (is != null) { Properties props = new Properties(); props.load(is); PropertyElf.setTargetFromProperties(this, props); } else { throw new IllegalArgumentException("Cannot find property file: " + propertyFileName); } } catch (IOException io) { throw new RuntimeException("Failed to read property file", io); }}
或者通过Properties进行创建:
public HikariConfig(Properties properties) { this(); PropertyElf.setTargetFromProperties(this, properties);}
本文介绍配置基于v2.7.2展开,后续源码分析也基于此版本- poolName : 连接池的名称,用于唯一标识一个连接池,通常作用于jmx监控和日志分析等场合。- dataSourceClassName :用于指定连接池使用的DataSource的类,使用dataSourceProperties的参数变量进行辅助 - jdbcUrl :旧式连接方法,和dataSourceClassName二者选一进行使用(出现dataSourceClassName时,当前参数不生效!),搭配 “driverClassName“进行使用。- driverClassName :用于旧式连接,指定driver的class,源码如下:
if (dsClassName != null && dataSource == null) { dataSource = createInstance(dsClassName, DataSource.class); PropertyElf.setTargetFromProperties(dataSource, dataSourceProperties);}else if (jdbcUrl != null && dataSource == null) { dataSource = new DriverDataSource(jdbcUrl, driverClassName, dataSourceProperties, username, password);}
- autoCommit :是否自动提交,默认是true
- username :用于旧式连接,用户名
- password :用于旧式连接,密码
private Connection newConnection() throws Exception{ …… …… String username = config.getUsername(); String password = config.getPassword(); connection = (username == null) ? dataSource.getConnection() : dataSource.getConnection(username, password); if (connection == null) { throw new SQLTransientConnectionException("DataSource returned null unexpectedly"); } …… …… }
- connectionTimeout :获取连接的超时时间,超过后会报SQLException,默认值为30s
public Connection getConnection(final long connectionTimeout) throws SQLException{……}
- idleTimeout :连接空闲时间,housekeeper使用
- maxLifetime :连接最大存活时间,超出时间后后台会对连接进行关闭,默认30min(对正在使用的连接不会立即处理)
final long maxLifetime = config.getMaxLifetime();if (maxLifetime > 0) { // variance up to 2.5% of the maxlifetime final long variance = maxLifetime > 10_000 ? ThreadLocalRandom.current().nextLong( maxLifetime / 40 ) : 0; final long lifetime = maxLifetime - variance; poolEntry.setFutureEol(houseKeepingExecutorService.schedule( () -> { // softEvictConnection(,,false)方法会判断连接是否在用, // 对于在用的连接不立即进行关闭,直到下次取用或houseKeeper进行关闭。 if (softEvictConnection(poolEntry, "(connection has passed maxLifetime)", false /* not owner */)) { addBagItem(connectionBag.getWaitingThreadCount()); } }, lifetime, MILLISECONDS));}
- connectionTestQuery :测试连接是否有效的sql,jdbc4以上的api不建议添加该选项(通过connection.isVaild(1)替换)
- validationTimeout :验证超时时间(connection.isVaild(validationTimeout))
isUseJdbc4Validation = config.getConnectionTestQuery() == null;final int validationSeconds = (int) Math.max(1000L, validationTimeout) / 1000;if (isUseJdbc4Validation) { return connection.isValid(validationSeconds);}try (Statement statement = connection.createStatement()) { if (isNetworkTimeoutSupported != TRUE) { setQueryTimeout(statement, validationSeconds); } statement.execute(config.getConnectionTestQuery());}
- minimumIdle :连接池最小空闲数量
- maximumPoolSize :连接池最大数量
- registerMbeans :是否注册jmx监控(HikariConfig和HikariPool都实现了MXBean接口)
更多配置介绍:
HikariCP
阅读全文
0 0
- Hikaricp源码解读(2)——配置介绍及对应源码
- Hikaricp源码解读(3)——ConcurrentBag介绍
- Hikaricp源码解读(4)——Proxy*代理类介绍
- Hikaricp源码解读(5)——物理连接生命周期介绍
- Hikaricp源码解读(1)——简介
- Ibatisnet源码解读(2)—DataMapper
- springmvc基本配置及相关源码解读
- kubernetes源码解读——源码结构
- Caffe源码解读2 —— SyncedMemory
- Daggger2 概念解读、使用姿势及源码分析(2)
- mybatis源码解读(2)
- spring的配置加载原理及源码解读
- HttpClient 4.3连接池参数配置及源码解读
- HttpClient 4.3连接池参数配置及源码解读
- HttpClient 4.3连接池参数配置及源码解读
- HttpClient 4.3连接池参数配置及源码解读
- HttpClient 4.3连接池参数配置及源码解读_0
- HttpClient 4.3连接池参数配置及源码解读
- android 开发中常用的第三方库(图片加载篇——Glide)
- js 处理接收到的数据
- Platform来获取一些系统的路径和参数
- Spring Cloud构建微服务架构:服务网关(过滤器)【Dalston版】
- 让js中数据类型所有的方法都通用起来
- Hikaricp源码解读(2)——配置介绍及对应源码
- vue-cli入门(一)——项目结构
- firefly rk3399内核编译命令
- IOS CoreText系列四:图文混排之点击事件
- NOJ(1541)-分支限界-加1乘2平方
- 三子棋
- spring boot 入门
- 简单的用C语言调用Sqlite3
- Android内存泄漏场景及常见解决措施