J.Olufsen
J.Olufsen

Reputation: 13915

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

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

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

Exception

java.lang.IllegalStateException: hikari-pool-1 - is not suspendable

Upvotes: 1

Views: 5551

Answers (2)

brettw
brettw

Reputation: 11114

These are all great answers. Unfortunately, suspending the pool is not a good way of simulating a network outage. Depending on whether you are on Windows, Linux, or Mac, there are likely different options.

On Linux, the best way to simulate a network failure is to enable/disable a firewall rule that DROPS (not DENY) traffic to the MySQL port. It would go something like this (check online references):

iptables -A INPUT -i lo -p tcp --dport 3306 -j DROP

And disabling (removing the block) would go something like this:

iptables -D INPUT -i lo -p tcp --dport 3306 -j DROP

Upvotes: 2

Mark Rotteveel
Mark Rotteveel

Reputation: 109257

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)

Upvotes: 5

Related Questions