2

Where can I find information about Hikari properties that can be modified at runtime? I tried to modify connectionTimeout. I can do it and it will be modified in the HikariDataSource without an exception (checked by setting and then getting the property) but it takes no effect. If I initially do:

HikariConfig config = new HikariConfig();
config.setConnectionTimeout(12000);
HikariDataSource pool = new HikariDataSource(config);

and later on I do

config.setConnectionTimeout(5000);

Hikari tries to get a new connection for 12 seconds instead of 5 seconds.

Or is there a way to change the value with effect? Are there other properties with the same behaviour?

du-it
  • 2,561
  • 8
  • 42
  • 80

3 Answers3

4

You can do this through the MX bean, but you don't need to use JMX

public void updateTimeout(final long connectionTimeoutMs, final HikariDataSource ds) {
    var poolBean = ds.getHikariPoolMXBean();
    var configBean = ds.getHikariConfigMXBean();
    
    poolBean.suspendPool(); // Block new connections being leased
    
    configBean.setConnectionTimeout(connectionTimeoutMs);
    
    poolBean.softEvictConnections(); // Close unused cnxns & mark open ones for disposal
    poolBean.resumePool(); // Re-enable connections
}

Bear in mind you will need to enable pool suspension in your initial config

var config = new HikariConfig();
...
config.setAllowPoolSuspension(true);
Jon Bates
  • 3,055
  • 2
  • 30
  • 48
2

You can't dynamically update the property values by resetting them on the config object - the config object is ultimately read once when instantiating the Hikari Pool (have a look at the source code in PoolBase.java to see how this works.

You can however do what you want and update the connection timeout value at runtime via JMX. How to do this is explained in the hikari documentation here

0xadecimal
  • 696
  • 5
  • 18
1

If your JVM has JMX enabled (I recommend for every prod), you could:

  1. SSH-tunnel JMX port to your local machine
  2. Connect to the VM in a JMX client like JConsole
  3. Operate pool MBean as needed

Note: JMX port must never be public to the internet, be sure that firewall protects you.

SSH Tunnel command example:

ssh -i ${key_path} -N -L 9000:localhost:9000 -L 9001:localhost:9001 ${user}@${address}

Pool MBean configuring

snowindy
  • 3,117
  • 9
  • 40
  • 54