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

x33g5p2x  于2021-12-19 转载在 其他  
字(3.4k)|赞(0)|评价(0)|浏览(338)

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

相关文章