Manually Increase number of Connections to be used in Hikari Pool
Asked Answered
G

1

6

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 ?

Gallows answered 12/9, 2022 at 8:41 Comment(1)
Don't... Set the spring.datasource.hikari.minimum-idle property, this will control how many connections Hikari will open at the start of the application (default 10). The spring.datasource.hikari.maximum-pool-size controls the maximum (default 10). If you increase the poolsize you also need to increase the max (as minimum cannot be higher than maximum). I would also suggest on studying how a connection pool works as your question and how you tried to solve this indicate you don't understand those.Planarian
F
16

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.

Funny answered 12/9, 2022 at 8:48 Comment(4)
Is this the max connections that Hikari CP can handle ? or is that when microservice will start this much connection will be available for use ?Gallows
Not sure if I understand your question correctly, but both I think. It's the amount of connections that Hikari will create when you start your microservice, and Hikari will never create another connection except if one of those initial connections dies or reaches its maximum lifetime.Funny
It isn't the amount of connections that will be created at startup, that is controlled by spring.datasource.hikari.minimum-idle (which by default is also 10). Setting the max will allow the pool to grow when it needs more connections.Planarian
@M.Deinum True, I oversimplified it a bit because Hikari recommends keeping those two values the same. I edited my answer to clarify the differences between both.Funny

© 2022 - 2024 — McMap. All rights reserved.