Hikaricp源码解读(2)——配置介绍及对应源码

摘要:
=Null){Propertiesprops=newProperties();props.load;PropertyElf.setTargetFromProperties;}else{thrownewIllegalArgumentException;}}catch{thrown ewRuntimeException;}}或通过Properties创建:publicHikariConfig{this();PropertyElf.setTargetFromProperty;}本文中描述的配置基于v2.7.2部署,随后的源代码分析也基于这个版本的poolName:连接池的名称,用于唯一标识连接池,通常用于jmx监视和日志分析。DataSourceClassName:用于指定连接池使用的DataSource的类。将dataSourceProperties的参数变量用于辅助jdbcUrl:legacy连接方法,选择要使用的和dataSourceClassName之一(当出现dataSourceClassName时,当前参数无效!DriverClassName:用于旧连接,请指定驱动程序的类,源代码如下:如果(dsClassName!}connectionTimeout:获取连接的超时。超时后,将报告SQLException。默认值为30 publicConnectionetConnectionthroughSQLException{

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

免责声明:文章转载自《Hikaricp源码解读(2)——配置介绍及对应源码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇python解析库之 XPathSLB(Server Load Balancing 服务器负载均衡)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

U-boot中SPL功能和源码流程分析

在U-boot目录下,有个比较重要的目录就是SPL的,SPL到底是什么呢?为什么要用它呢? SPL(Secondary programloader)是uboot第一阶段执行的代码。主要负责搬移uboot第二阶段的代码到 系统内存(System Ram,也叫片外内存)中运行。SPL是由固化在芯片内部的ROM引导的。我们知道很多芯 片厂商固化的ROM支持从na...

【转】Android 源码编译make的错误处理--不错

原文网址:http://blog.csdn.net/ithomer/article/details/6977386 Android源码下载:官方下载或参考android源码下载方式 Android编译版本:PLATFORM_VERSION=4.0.1(最新Android 4.0.1) OS 操作系统平台:Linux yanggang 2.6.35-30-g...

wireshark源码分析二

一、源代码结构 在wireshark源代码根目录下,可以看到以下子目录: 1)物理结构     其中,epan文件夹负责所有网络协议识别工作,plugins里面存放了wireshark所有插件,gtk文件夹里面是wireshark的界面部分代码,其余文件夹没有单独研究。 2)逻辑结构     下图给出了Ethereal功能模块:    a) GTK1/2...

Linux源码编译安装和卸载

Linux下正常的编译安装/卸载 源码的安装一般由3个步骤组成: 配置(configure) 编译(make) 安装(make install)。 configure文件是一个可执行的脚本文件,它有很多选项,在待安装的源码目录下使用命令./configure –help可以输出详细的选项列表。 其中--prefix选项是配置安装目录,如果不配置该选项,...

Spring使用@Async注解

    本文讲述@Async注解,在Spring体系中的应用。本文仅说明@Async注解的应用规则,对于原理,调用逻辑,源码分析,暂不介绍。对于异步方法调用,从Spring3开始提供了@Async注解,该注解可以被标注在方法上,以便异步地调用该方法。调用者将在调用时立即返回,方法的实际执行将提交给Spring TaskExecutor的任务中,由指定的线程...

opendpi 源码分析(二)

先介绍opendpi如何设置各个协议的处理,然后介绍如何处理每一个包,最后是软件的流程图。 第一:首先看下要用到的重要的宏 View Code #define IPOQUE_SAVE_AS_BITMASK(bitmask,value) (bitmask)=(((IPOQUE_PROTOCOL_BITMASK)1)<<(value)) #de...