Hikaricp源碼解讀(2)——配置介紹及對應源碼


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM