Modify Hikari properties at runtime

霸气de小男生 提交于 2020-12-06 17:42:09

问题


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?


回答1:


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




回答2:


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);


来源:https://stackoverflow.com/questions/49301308/modify-hikari-properties-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!