当前位置:网站首页>四、HikariCP源码分析之初始化分析一
四、HikariCP源码分析之初始化分析一
2022-07-29 21:41:00 【InfoQ】
HikariDataSource的初始化
HikariDataSourceHikariDataSource两个构造函数
public HikariDataSource() {
super();
fastPathPool = null;
}
public HikariDataSource(HikariConfig configuration) {
configuration.validate();
configuration.copyState(this);
LOGGER.info("{} - Started.", configuration.getPoolName());
pool = fastPathPool = new HikariPool(this);
}
两种初始化方式
- 使用无参构造初始化
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("root");
dataSource.setPassword("123");
//设置数据库独有的属性
dataSource.addDataSourceProperty("cachePrepStmts", "true");
//从连接池获取连接
Connection connection = dataSource.getConnection();
- 使用有参构造初始化
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/test");
config.setUsername("root");
config.setPassword("123");
//设置数据库独有的属性
config.addDataSourceProperty("cachePrepStmts", "true");
//使用HikariConfig构造HikariDataSource
HikariDataSource dataSource = new HikariDataSource(config);
//从连接池获取连接
Connection connection = dataSource.getConnection();
HikariConfigHikariDataSourceHikariConfigHikariConfig无参构造代码分析
super();
fastPathPool = null;
super();HikariDataSourceHikariConfigHikariDataSourceHikariConfigsuper();HikariConfigpublic HikariConfig() {
//①
dataSourceProperties = new Properties();
healthCheckProperties = new Properties();
minIdle = -1;
maxPoolSize = -1;
//MAX_LIFETIME=30分钟
maxLifetime = MAX_LIFETIME;
//CONNECTION_TIMEOUT=30 秒
connectionTimeout = CONNECTION_TIMEOUT;
//VALIDATION_TIMEOUT=5 秒
validationTimeout = VALIDATION_TIMEOUT;
//IDLE_TIMEOUT=10分钟
idleTimeout = IDLE_TIMEOUT;
isAutoCommit = true;
isInitializationFailFast = true;
//②
String systemProp = System.getProperty("hikaricp.configurationFile");
if (systemProp != null) {
loadProperties(systemProp);
}
}
- ①
- ②
System.getPropertyhikaricp.configurationFilehikaricp.configurationFilehikaricp.configurationFile-Dhikaricp.configurationFile=xxxxx.propertiesloadPropertiesHikariConfigfastPathPool = null;有参构造代码分析
configuration.validate();
configuration.copyState(this);
LOGGER.info("{} - Started.", configuration.getPoolName());
pool = fastPathPool = new HikariPool(this);
configuration.validate();configuration.copyState(this);configuration.copyState(this);thisHikariDataSourceconfigurationHikariConfigHikariConfigHikariDataSourcecopyStatepublic void copyState(HikariConfig other) {
for (Field field : HikariConfig.class.getDeclaredFields()) {
if (!Modifier.isFinal(field.getModifiers())) {
field.setAccessible(true);
try {
field.set(other, field.get(this));
} catch (Exception e) {
throw new RuntimeException("Failed to copy HikariConfig state: " + e.getMessage(), e);
}
}
}
}
HikariConfigthisHikariDataSourceHikariDataSourceHikariConfigpool = fastPathPool = new HikariPool(this);pool = fastPathPoolnew HikariPool(this)HikariPoolHikariPool边栏推荐
- 仿Modbus消息帧进行通信
- 【板栗糖GIS】arcmap—如何快捷替换属性表中的部分内容
- The implementation of the flood control project and safety construction project in the flood storage and detention areas in Luluze and Ningjinbo was launched
- linkedlist的用处之一:通过结构体成员的地址获取结构体变量的地址
- 杨辉三角的各种输出:
- Add obsolete flag to pdf
- 小程序微信定位不准
- SwiftUI CoreData 教程之如何加速搜索速度
- 亚马逊登录参数metadata1,encryptedPwd逆向分析
- How to implement your personal knowledge base?
猜你喜欢
随机推荐
leetcode122. Best Time to Buy and Sell Stock II
在Ferora35中安装oracle-database-xe-21c
OPEN数据 | 新库上线 | CnOpenDataA股上市公司社会责任报告数据
Small program WeChat positioning is not accurate
新库上线 | CnOpenData租赁和商务服务业工商注册企业基本信息数据
系列(jupyter自动保存失败)
华东师范大学副校长周傲英:数据赋能,从数据库到数据中台
Add a logo to the upper left corner of the picture, add time and address to the lower left corner, and wrap the line when the address reaches the specified length
SwiftUI Apple App Store 发布App的初学者完整指南
[BUG]一个数组new的时候sizeof()忘乘上个数
B. Party(图论/暴力/顶点的度数)
华为畅享50 Pro评测:HarmonyOS加持 更流畅更安全
笔记:fgets函数详解
Xshell 7 提示 “要继续使用此程序,您必须应用最新的更新或使用新版本”
SQL教程之性能不仅仅是查询
容器网络硬核技术内幕 (22) 安全与自由兼顾
阿里 P8 爆出的这份大厂面试指南,看完工资暴涨 30k!
GBASE 8s 用户标示与鉴别
Get the Qiniu cloud address file and save it locally
第3章业务功能开发(线索关联市场活动,动态搜索)









