当前位置:网站首页>二、HikariCP源码分析之获取连接流程二
二、HikariCP源码分析之获取连接流程二
2022-07-29 21:42:00 【InfoQ】
HikariPool的getConnection()方法
getConnection()public final Connection getConnection() throws SQLException {
return getConnection(connectionTimeout);
}
connectionTimeoutgetConnection(connectionTimeout);public final Connection getConnection(final long hardTimeout) throws SQLException {
//①
//获取连接的时候申请令牌, 主要是为了连接池挂起的时候, 控制用户不能获取连接
//当连接池挂起的时候, Semaphore的 10000 个令牌都会被占用, 此处就会一直阻塞线程等待令牌
suspendResumeLock.acquire();
//记录获取连接的开始时间, 用于超时判断
final long startTime = clockSource.currentTime();
try {
long timeout = hardTimeout;
do {
//②
//从连接池获取连接, 超时时间timeout
final PoolEntry poolEntry = connectionBag.borrow(timeout, MILLISECONDS);
//borrow方法在超时的时候才会返回 null
if (poolEntry == null) {
break; // We timed out... break and throw exception
}
final long now = clockSource.currentTime();
//③
//获取连接的时候, 判断连接是否已经被标记移除
if (poolEntry.isMarkedEvicted() || (clockSource.elapsedMillis(poolEntry.lastAccessed, now) > ALIVE_BYPASS_WINDOW_MS && !isConnectionAlive(poolEntry.connection))) {
//如果连接超出maxLifetime, 或者连接测试不通过, 就关闭连接
closeConnection(poolEntry, "(connection is evicted or dead)"); // Throw away the dead connection (passed max age or failed alive test)
//剩余超时时间
timeout = hardTimeout - clockSource.elapsedMillis(startTime);
} else {
//④
//记录连接借用
metricsTracker.recordBorrowStats(poolEntry, startTime);
//创建ProxyConnection, ProxyConnection是Connection的包装, 同时也创建一个泄露检测的定时任务
return poolEntry.createProxyConnection(leakTask.schedule(poolEntry), now);
}
} while (timeout > 0L);
} catch (InterruptedException e) {
throw new SQLException(poolName + " - Interrupted during connection acquisition", e);
} finally {
//释放锁
suspendResumeLock.release();
}
//⑤
//获取连接超时才会执行下面的代码
logPoolState("Timeout failure ");
metricsTracker.recordConnectionTimeout();
String sqlState = null;
final Throwable originalException = getLastConnectionFailure();
if (originalException instanceof SQLException) {
sqlState = ((SQLException) originalException).getSQLState();
}
final SQLException connectionException = new SQLTransientConnectionException(poolName + " - Connection is not available, request timed out after " + clockSource.elapsedMillis(startTime) + "ms.", sqlState, originalException);
if (originalException instanceof SQLException) {
connectionException.setNextException((SQLException) originalException);
}
throw connectionException;
}
①Semaphore
suspendResumeLock.acquire();
//记录获取连接的开始时间, 用于超时判断
final long startTime = clockSource.currentTime();
suspendResumeLock- 挂起HikariCP
- 挂起连接池
- 更改数据库连接池配置,或者更改 DNS 配置(指向新的主服务器)
- 软驱逐连接池中现有的连接
- 恢复连接池
- 怎么实现的
suspendResumeLockcom.zaxxer.hikari.util.SuspendResumeLockSemaphoreSemaphoreSemaphoreSemaphoreSemaphorepublic void suspend() {
//MAX_PERMITS = 10000
acquisitionSemaphore.acquireUninterruptibly(MAX_PERMITS);
}
suspend()SemaphoreisAllowPoolSuspension=trueisAllowPoolSuspensionsuspendResumeLockthis.suspendResumeLock = config.isAllowPoolSuspension() ? new SuspendResumeLock() : SuspendResumeLock.FAUX_LOCK;suspendResumeLockFAUX_LOCKFAUX_LOCKpublic static final SuspendResumeLock FAUX_LOCK = new SuspendResumeLock(false) {
@Override
public void acquire() {}
@Override
public void release() {}
@Override
public void suspend() {}
@Override
public void resume() {}
};
clockSource②获取连接
//②
//从连接池获取连接, 超时时间timeout
final PoolEntry poolEntry = connectionBag.borrow(timeout, MILLISECONDS);
//borrow方法在超时的时候才会返回 null
if (poolEntry == null) {
break; // We timed out... break and throw exception
}
connectionBagpoolEntrypoolEntryconnectionBagCopyOnWriteArrayListconnectionBagconnectionBagtimeouttimeoutconnectionTimeouttimeout边栏推荐
- C语言操作符详解(1)
- How to implement your personal knowledge base?
- MySQL的TIMESTAMP数据类型
- Verilog 加法器设计
- 华东师范大学副校长周傲英:数据赋能,从数据库到数据中台
- One of the uses of linkedlist: Get the address of the structure variable through the address of the structure member
- HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
- spyder打不开解决方案
- [BUG]一个数组new的时候sizeof()忘乘上个数
- leetcode122. Best Time to Buy and Sell Stock II
猜你喜欢
随机推荐
ict的终极模式 是软件研发
【LeetCode】36、有效的数独
leetcode122. Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II(简单)
【板栗糖GIS】DOS—如何在目录文件中批量建立子文件夹
Official announcement!Suzhou Wujiang Development Zone launches electronic labor contract platform
【板栗糖GIS】arcmap—标注太长,如何换行显示
spyder打不开解决方案
GBASE 8s PAM插入式身份验证模块
【HDLBits 刷题】Verilog Language(4)Procedures 和 More Verilog Features 部分
HMS Core audio editing service audio source separation and spatial audio rendering, helping to quickly enter the world of 3D audio
GTK进行rgb绘图
SwiftUI Apple App Store 发布App的初学者完整指南
程序员自由工作的三大痛点?一文教你统统解决
03-树2 List Leaves
bright day
结合布林线理解现货白银走势图的方法
百度智能云章淼:详解企业级七层负载均衡开源软件BFE
解决报错 WARNING: IPv4 forwarding is disabled. Networking will not work.
qt中qstring合并字符串
CNCF Keith Chan:分布式云时代,云原生社区的发展与趋势


![[BUG]一个数组new的时候sizeof()忘乘上个数](/img/d7/fa821aee0626e715bbb4e422e7e2fb.png)






