Cannot suspend HikariCP

J.O*_*sen 1 java connection-pooling jdbc hikaricp

The exception is thrown when trying to suspend HikariCP pool while simulating network connection fail for a couple of seconds. Why can the pool cannot be suspended? Are there other easy ways to simulate lost network connection (to MySQL server on localhost)?

Configuration:

final String configFile = "src/main/resources/db.properties";
HikariConfig config = new HikariConfig(configFile);
config.setRegisterMbeans(true);
config.setPoolName("hikari-pool-1");
ds = new HikariDataSource(config);
Run Code Online (Sandbox Code Playgroud)

Properties:

jdbcUrl=jdbc:mysql://localhost:3306/db?user=user&password=password
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
dataSource.useServerPrepStmts=true
dataSource.useLocalSessionState=true
dataSource.useLocalTransactionState=true
dataSource.rewriteBatchedStatements=true
dataSource.cacheResultSetMetadata=true
dataSource.cacheServerConfiguration=true
dataSource.elideSetAutoCommits=true
dataSource.maintainTimeStats=false
Run Code Online (Sandbox Code Playgroud)

JUnit:

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (hikari-pool-1)");
HikariPoolMXBean poolProxy = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);
poolProxy.suspendPool();
Run Code Online (Sandbox Code Playgroud)

Exception

java.lang.IllegalStateException: hikari-pool-1 - is not suspendable
Run Code Online (Sandbox Code Playgroud)

Mar*_*eel 5

By default suspension of the pool is not allowed, you need to explicitly enable pool suspension.

See also the documentation:

?allowPoolSuspension
This property controls whether the pool can be suspended and resumed through JMX. This is useful for certain failover automation scenarios. When the pool is suspended, calls to getConnection() will not timeout and will be held until the pool is resumed. Default: false

In short, add:

config.setAllowPoolSuspension(true)
Run Code Online (Sandbox Code Playgroud)