当前位置:网站首页>5、 Initialization analysis II of hikaricp source code analysis
5、 Initialization analysis II of hikaricp source code analysis
2022-06-25 19:11:00 【User 1422411】
Welcome to my blog , Synchronize updates : Fengshan bieyuan
Source code version 2.4.5-SNAPSHOT
HikariPool The initialization
In the last section , We talked about pool = fastPathPool = new HikariPool(this); Medium new HikariPool(this). Let's look at the code :
public HikariPool(final HikariConfig config) {
//①
//PoolBase
super(config);
//②
// Construct a connectionBag For saving connections , connectionBag Is the core of the connection pool
this.connectionBag = new ConcurrentBag<>(this);
// Initialize connection counters , Used to count the number of connections in the connection pool
this.totalConnections = new AtomicInteger();
// Depending on whether it is allowed to suspend the connection pool , Initialize lock
this.suspendResumeLock = config.isAllowPoolSuspension() ? new SuspendResumeLock() : SuspendResumeLock.FAUX_LOCK;
//③
// Connection pool statistics
if (config.getMetricsTrackerFactory() != null) {
setMetricsTrackerFactory(config.getMetricsTrackerFactory());
} else {
setMetricRegistry(config.getMetricRegistry());
}
setHealthCheckRegistry(config.getHealthCheckRegistry());
// register JMX dependent bean
registerMBeans(this);
//④
checkFailFast();
//⑤
ThreadFactory threadFactory = config.getThreadFactory();
this.addConnectionExecutor = createThreadPoolExecutor(config.getMaximumPoolSize(), poolName + " connection adder", threadFactory, new ThreadPoolExecutor.DiscardPolicy());
this.closeConnectionExecutor = createThreadPoolExecutor(config.getMaximumPoolSize(), poolName + " connection closer", threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
if (config.getScheduledExecutorService() == null) {
threadFactory = threadFactory != null ? threadFactory : new DefaultThreadFactory(poolName + " housekeeper", true);
this.houseKeepingExecutorService = new ScheduledThreadPoolExecutor(1, threadFactory, new ThreadPoolExecutor.DiscardPolicy());
this.houseKeepingExecutorService.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
this.houseKeepingExecutorService.setRemoveOnCancelPolicy(true);
} else {
this.houseKeepingExecutorService = config.getScheduledExecutorService();
}
//⑥
// Default 30s To run a
this.houseKeepingExecutorService.scheduleWithFixedDelay(new HouseKeeper(), 0L, HOUSEKEEPING_PERIOD_MS, MILLISECONDS);
//⑦
this.leakTask = new ProxyLeakTask(config.getLeakDetectionThreshold(), houseKeepingExecutorService);
}You can see that the code is very long , It's a little bit more complicated , It doesn't matter , Let's slowly analyze .
① Initializes the parent class
super(config); Medium super It stands for com.zaxxer.hikari.pool.PoolBase.PoolBase Is a connection pool abstraction closer to the bottom . It defines some database connection related configurations , such as : Commit transactions automatically , Whether to connect read-only , Whether to use JDBC4, Network request timeout, etc . Some important methods : initialization JDBC Of dataSource, Verify that the connection is alive , Reset the connection default configuration, etc . call super(config); Purpose , It's initialization PoolBase These database configurations in .
Through this super We can find out ,HikariCP The initialization of is passed layer by layer , Suppose a subclass inherits the parent class , The parent class inherits its parent class , So when initializing , Use the same configuration class , Pass to the subclass first , Then to the parent class , Then to the grandfather class , Each layer uses HikariConfig To initialize your own configuration , We can learn this initialization method , Very elegant .
Concrete PoolBase Initialization process , We won't go further , It's not very complicated , You can combine my code comments to see , The notes are very clear .
② initialization ConcurrentBag
ConcurrentBag Is a generic pool model container , As a whole HikariCP At the heart of , We will analyze it in a separate chapter , Here you just understand that the container used to save the database connection is initialized , Its interior is a CopyOnWriteArrayList, For saving connections .
totalConnections Well , It can be understood literally , Is a connected counter , Used to record the number of connections in the connection pool . Its type is AtomicInteger, About Atomic Atomic class at the beginning , We are 《HikariCP Source code analysis access connection process 1 》 Detailed analysis in AtomicBoolean Principle , This is about the same , You can read the previous article .totalConnections This counter , It will be added when adding new connections to the connection pool 1, The number of connections in the connection pool will decrease after they are closed 1.
suspendResumeLock Are we in 《HikariCP Source code analysis access connection process 2 》 The focus of the analysis in , I won't go into details here . Here is to create a connection pool suspended lock , Or token bucket , Used when the connection pool is suspended , Controls which users cannot obtain connections from the connection pool . If the user does not enable the connection pool suspend function , Create an empty lock implementation FAUX_LOCK, convenient JIT Optimize it .
③ Monitor initialization
We mentioned in the previous analysis article on getting connections , When you get the connection , Will report their status to the monitoring platform , Here is the relevant configuration for initializing the monitoring platform . Users can customize the implementation of the monitoring platform , Register it with HikariCP in , Can be HikariCP call .
It is worth mentioning that registerMBeans(this); This code . This is registration JMX dependent MBean, Only those with databases configured isRegisterMbeans Configuration item ,HikariCP Will register MBean, We can use JMX Modify the connection pool configuration during operation . If you don't configure isRegisterMbeans, So use JMX Modifying the configuration will result in an error . Yes JMX Interested students , You can learn relevant contents by yourself .
④ Fast failure
There is only one line of code checkFailFast();, But we took it out alone , That means it's interesting .
Just look at the code :
private void checkFailFast() {
if (config.isInitializationFailFast()) {
try {
newConnection().close();
} catch (Throwable e) {
try {
shutdown();
} catch (Throwable ex) {
e.addSuppressed(ex);
}
throw new PoolInitializationException(e);
}
}
} The code looks a lot , In fact, the key is not much .isInitializationFailFast It's a HikariCP Configuration item for , Its default value is true. Old rules , First guess from the literal meaning , Seems to be : Initialization fails quickly . Take another look at the following code newConnection().close();, This creates a connection , Then it was shut down immediately ! Combine the above clues , What's the meaning of this? ? In fact, it's very easy to understand . Is initializing HikariCP When , Create a connection , Then close it immediately , If an error is reported, it cannot be established , Close the entire connection pool , Throw the wrong .
The purpose is during startup , Create a connection to verify if there are errors in key parameters , If the connection cannot be established , Throw an error immediately , It is convenient for users to find problems in time . such as : Our database password is wrong . Without this immediate failure of validation , After your online deployment is successful , The problem can only be found after obtaining the connection for the first time , This is sad , Maybe you will be scolded .
⑤ Initializes the thread pool
HikariCP There are several thread pools in :
- closeConnectionExecutor : The thread pool used to close the underlying connection , Only one thread , The maximum thread task queue is the maximum number of connections in the connection pool , Tasks out of queue , Will keep trying to add .
- addConnectionExecutor: Thread pool for adding new connections , Only one thread , The maximum thread task queue is the maximum number of connections in the connection pool , Tasks out of queue , Outright, .
- houseKeepingExecutorService: This is a timed thread pool , There is only one thread by default , It has many functions : Used to perform connection leak detection 、 Close connections that have expired idle time 、 Reclaim idle connections 、 Detection time callback .
closeConnectionExecutor The queue task abandonment strategy of is a little different , It will try again and again , It is based on the consideration that the connection must be closed , The direct abandonment of other tasks has little impact .
There are two configurations that can affect the thread pool , One is scheduledExecutor: Used to provide to houseKeepingExecutorService Thread pool used , If the user does not customize , Use the default 1 A pool of threads . The other is threadFactory: Used to generate threads in the thread pool ,HikariCP When the thread pool is generated , Call the thread factory to get the thread .
⑥ Start the connection management task
Look at the code :
this.houseKeepingExecutorService.scheduleWithFixedDelay(new HouseKeeper(), 0L, HOUSEKEEPING_PERIOD_MS, MILLISECONDS);
Here to houseKeepingExecutorService A task is submitted in the thread pool : every other 30 second , Just do it once HouseKeeper Mission . The main function of this task is : Detection time callback , Adjust the connections in the connection pool . What is time callback ? For example, the system time of the server is not accurate , Later, the user modified the system time of the server , because HikariCP Is a time sensitive framework , It relies on scheduled tasks to manage connections , If the system time changes , Then the timing task is not accurate .
There are two situations :
- First, the user adjusted the time faster , This is the time ,HikariCP Don't do anything? , Because time is running out , It just speeds up the execution of scheduled tasks , Expire the connection earlier , This has little effect on the connection pool , Because the connection pool will automatically add new connections .
- Second, the user slowed down the time , That is to say, the time is reversed . The fallback time is right HikariCP It has great influence , For example, it was still poor 1 Second task , It may be over now 15 Seconds before execution , This may trigger a connection that should have expired in its lifetime , It won't expire . therefore , This is the time ,HikariCP All connections in the connection pool will be soft evicted , Make all connections unavailable , Then re create the new connection .
because HouseKeeper The task is complicated , Our separate chapter analyses .
⑦ Create the parent task of the connection leak detection task
Look at the code :
this.leakTask = new ProxyLeakTask(config.getLeakDetectionThreshold(), houseKeepingExecutorService);
We are 《HikariCP Source code analysis of the acquisition connection process 3 》 When analyzing connection leak detection in , Mentioned , When the user gets each connection , A scheduled task for connection leak detection will be created for this connection , Within a specified time , Throw a connection leak warning .
When creating a connection leak detection task , A parent task parameter will be used , Get the maximum time of connection leakage and the thread pool used to execute the task from the parent task , Then use these two parameters to create the task . This parent task , It was created here , These two parameters are passed during creation : The maximum time of connection leakage and the thread pool used to execute tasks .
thus ,HikariDataSource Initialization completes the analysis . We have any questions , You can bring it up , Let's discuss and study together .
边栏推荐
- Current situation of China's hydraulic cylinder industry in 2020 (with application fields, policies and regulations, supply and demand status and enterprise pattern) [figure]
- Analysis of global tea production, consumption and import and export trade: China's tea production ranks first in the world [figure]
- Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
- 网络安全检测与防范 练习题(三)
- JVM | runtime data area (heap space)
- solidity日期工具
- English name of each stage of software version
- PHP Chinese regular
- 2021 development status of China's cloud game industry and analysis of major service providers: Although cloud games are still in their infancy, the market prospect is huge [figure]
- 五、HikariCP源码分析之初始化分析二
猜你喜欢

Combing the latest Data Mining Event Scheme!
![Analysis on policy, output and market scale of China's natural gas hydrogen production industry in 2020 [figure]](/img/f2/ec0fe8bec503c8788d6d549845b95c.jpg)
Analysis on policy, output and market scale of China's natural gas hydrogen production industry in 2020 [figure]

What is an operator?

Cutting feet to fit shoes - talking about the ramp reconstruction on the track

JVM | runtime data area (heap space)

【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用

How to quickly close port 8080

谈谈CNN中的位置和尺度问题

Analysis on the market scale and pattern of contrast agents in China in 2021: Jiangsu Hengrui pharmaceutical, general electric, Yangzijiang Pharmaceutical Group, Bayer and bleco account for more than

Sorting out the latest data mining competition scheme!
随机推荐
QQ机器人疫情查询/疫情关注等【最新beta2版本】
Process of vacuum and vacuum full
QQ机器人:群成员自我禁言管理【最新beta2版本】
Error record: preg_ match(): Compilation failed: range out of order in character class at offset 13
Network security detection and prevention test questions (II)
Oriental Wealth function (the most complete edition of Childe Yong)
QQ robot epidemic situation query / epidemic situation concern [latest beta2 version]
The meanings of /32, /48, /64 in IPv6 addresses
R语言plotly可视化:plotly可视化二维直方图等高线图(Basic 2D Histogram Contour)
Solve the problem that sublime Text3 package control cannot install plug-ins
LeetCode-101-对称二叉树
What is an operator?
Solidity date tool
Divine reversion EA
Google cloud SSH enable root password login
TCP/IP 测试题(五)
mysql视图讲解
Miner's Diary: why should I go mining on April 5, 2021
Analysis on market scale and supply of China's needle coke industry in 2020 [figure]
【C语言练习——打印上三角及其变形(带空格版)】