文章5 | 阅读 2348 | 点赞0
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);
}
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");
}
…… ……
}
public Connection getConnection(final long connectionTimeout) throws SQLException
{……}
对正在使用的连接不会立即处理
)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));
}
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());
}
更多配置介绍:
HikariCP
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/taisenki/article/details/78329456
内容来源于网络,如有侵权,请联系作者删除!