简体   繁体   中英

Manually Increase number of Connections to be used in Hikari Pool

I want to have some specific number of connections available to be used before my microservice starts. I am using Hikari Pool and Spring Boot.

I am using the loop for specific number and calling "SELECT 1 FROM DUAL" to create connections manually, but its seems that instead of creating multiple connections, its reusing the same connection again and again.

How to do this?

You can configure the maximum size of connections by setting the spring.datasource.hikari.maximum-pool-size property. By default, this is set to 10. Before you increase this, it's recommended that you read the About Pool Sizing documentation of Hikari first.

However, if your goal is to create a new connection for every query you execute, then the answer is that it isn't possible. The way connection pools like Hikari work is that they create a pool of connections ready to be used when the application starts. The default value of this is also 10 (configured through spring.datasource.hikari.minimum-idle ). Hikari will then maintain these connections (checking whether they're alive, creating new ones if existing ones reach their maximum life time, ...).

So for your query SELECT 1 FROM DUAL , it means that Hikari will take one of its connections that's free, and executes the query using that connection. If all connections are busy and spring.datasource.hikari.maximum-pool-size isn't reached, then it will create a new connection.

However, since spring.datasource.hikari.minimum-idle and spring.datasource.hikari.maximum-pool-size are the same by default, it means that Hikari won't create a new connection after startup unless it's to replace an existing one. Hikari also recommends to keep these two the same values, as mentioned within the documentation (search for "fixed size connection pool ").

If all connections are busy and spring.datasource.hikari.maximum-pool-size is reached, then it waits until one is freed up (by default 30 seconds). If no connection is freed up, then an exception is thrown. Summarized, Hikari won't create a new connection for each call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM