当前位置:网站首页>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 .
边栏推荐
- R语言使用DALEX包的model_profile函数基于条件依赖CDP方法解释多个分类模型中某个连续特征和目标值y的关系(Conditional Dependence Plots)
- QQ robot flash forwarding / recall message forwarding [latest beta2 version]
- JVM|运行时数据区(堆空间)
- What are Baidu collection skills? 2022 Baidu article collection skills
- Solve the problem that sublime Text3 package control cannot install plug-ins
- Network security detection and prevention test questions (4)
- 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]
- Current situation of China's hydraulic cylinder industry in 2020 (with application fields, policies and regulations, supply and demand status and enterprise pattern) [figure]
- 网络安全检测与防范 练习题(三)
- User management and permissions
猜你喜欢
![Analysis on development status and development suggestions of e-commerce industry in Xinjiang in 2020 [figure]](/img/d1/8ed2958ef365e17494bade6e29ee04.jpg)
Analysis on development status and development suggestions of e-commerce industry in Xinjiang in 2020 [figure]

华为发布两大昇腾计划 推动AI人才发展和科研创新

Ali vision AI training camp-day01
![Analysis of China's medical device industry development environment (PEST) in 2021: the awareness of medical care is enhanced, and the demand for medical device products is also rising [figure]](/img/3d/7cd57d0ed728880fd467babd73ed3f.jpg)
Analysis of China's medical device industry development environment (PEST) in 2021: the awareness of medical care is enhanced, and the demand for medical device products is also rising [figure]

JVM|运行时数据区(堆空间)

【C语言练习——打印上三角及其变形(带空格版)】

JVM | runtime data area (heap space)
![Analysis on market scale and supply of China's needle coke industry in 2020 [figure]](/img/79/6b08b62be8768484f548b6e18bd810.jpg)
Analysis on market scale and supply of China's needle coke industry in 2020 [figure]

QQ机器人:群成员自我禁言管理【最新beta2版本】

谈谈CNN中的位置和尺度问题
随机推荐
Network security detection and prevention test questions (4)
Process of vacuum and vacuum full
PHP synchronizes website content to hundreds of websites to improve SEO ranking
Is it safe to open an account with flush?
What are Baidu collection skills? 2022 Baidu article collection skills
Bloom filter
Is CICC wealth safe? How long does it take to open an account
GenICam GenTL 标准 ver1.5(1)
Network security detection and prevention exercises (III)
R语言plotly可视化:plotly可视化二维直方图等高线图(Basic 2D Histogram Contour)
Solidity date tool
Analysis of China's medical device industry development environment (PEST) in 2021: the awareness of medical care is enhanced, and the demand for medical device products is also rising [figure]
Analysis of global tea production, consumption and import and export trade: China's tea production ranks first in the world [figure]
Why are life science enterprises on the cloud in succession?
六、HikariConfig的配置解析
华为发布两大昇腾计划 推动AI人才发展和科研创新
In 2021, China's private equity market is growing, and the scale of private equity fund management reaches 19.78 trillion yuan [figure]
English name of each stage of software version
PHP数据库连接version1.1
Overview and trend analysis of China's CT examination equipment industry in 2021 [figure]