hikari配置断开重连,HikariCP自动重新连接

I use jpa+hibernate+hikariCP. Today I got connection closed error. I setted connectionTimeout and ideleTimeout properties(hikari). If in meantime use does not do any operition hikari close the pool connections auto. So that I got closed connection problem. My question how can I set properly hiker(jpa,hibernate) properties so if user comes back after 3-4 hours and try to do some operation hikari auto reconnect to db? is it possible?

I use hikari v2.6.1 and hibernate v5.2.8.Final

解决方案

This should already be working. Are you holding the connection open during this time? By default HikariCP closes and renews connections every 30 minutes (maxLifetime default). If your database has a shorter idle timeout than that, then you need to adjust the setting in HikariCP to match.

如果你正在使用HikariCP连接池,可以通过以下方式配置断开重连: ```java HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://localhost/mydatabase"); config.setUsername("myuser"); config.setPassword("mypassword"); config.setConnectionTimeout(30000); // 连接超时时间30秒 config.setMaximumPoolSize(10); // 连接池最大连接数10 config.setMinimumIdle(5); // 连接池最小空闲连接数5 config.setAutoCommit(false); // 关闭自动提交 config.setConnectionInitSql("SET NAMES utf8mb4"); // 连接初始化 SQL config.setConnectionTestQuery("SELECT 1"); // 连接测试 SQL config.setValidationTimeout(5000); // 连接验证超时时间5秒 config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); HikariDataSource ds = new HikariDataSource(config); ds.setConnectionTimeout(30000); // 连接超时时间30秒 ds.setMaximumPoolSize(10); // 连接池最大连接数10 ds.setMinimumIdle(5); // 连接池最小空闲连接数5 ds.setAutoCommit(false); // 关闭自动提交 ds.setConnectionInitSql("SET NAMES utf8mb4"); // 连接初始化 SQL ds.setConnectionTestQuery("SELECT 1"); // 连接测试 SQL ds.setValidationTimeout(5000); // 连接验证超时时间5秒 ds.addDataSourceProperty("cachePrepStmts", "true"); ds.addDataSourceProperty("prepStmtCacheSize", "250"); ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); ds.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(30)); // 连接泄漏检测时间30秒 ds.setConnectionTimeout(30000); ds.setMaximumPoolSize(10); ds.setMinimumIdle(5); ds.setConnectionInitSql("SET NAMES utf8mb4"); ds.setConnectionTestQuery("SELECT 1"); ds.setValidationTimeout(5000); ds.addDataSourceProperty("cachePrepStmts", "true"); ds.addDataSourceProperty("prepStmtCacheSize", "250"); ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); ds.setConnectionTimeout(30000); ds.setMaximumPoolSize(10); ds.setMinimumIdle(5); ds.setConnectionInitSql("SET NAMES utf8mb4"); ds.setConnectionTestQuery("SELECT 1"); ds.setValidationTimeout(5000); ds.addDataSourceProperty("cachePrepStmts", "true"); ds.addDataSourceProperty("prepStmtCacheSize", "250"); ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); ds.setConnectionTimeout(30000); ds.setMaximumPoolSize(10); ds.setMinimumIdle(5); ds.setConnectionInitSql("SET NAMES utf8mb4"); ds.setConnectionTestQuery("SELECT 1"); ds.setValidationTimeout(5000); ds.addDataSourceProperty("cachePrepStmts", "true"); ds.addDataSourceProperty("prepStmtCacheSize", "250"); ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); ds.setConnectionTimeout(30000); ds.setMaximumPoolSize(10); ds.setMinimumIdle(5); ds.setConnectionInitSql("SET NAMES utf8mb4"); ds.setConnectionTestQuery("SELECT 1"); ds.setValidationTimeout(5000); ds.addDataSourceProperty("cachePrepStmts", "true"); ds.addDataSourceProperty("prepStmtCacheSize", "250"); ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); ds.setConnectionTimeout(30000); ds.setMaximumPoolSize(10); ds.setMinimumIdle(5); ds.setConnectionInitSql("SET NAMES utf8mb4"); ds.setConnectionTestQuery("SELECT 1"); ds.setValidationTimeout(5000); ds.addDataSourceProperty("cachePrepStmts", "true"); ds.addDataSourceProperty("prepStmtCacheSize", "250"); ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); ds.setConnectionTimeout(30000); ds.setMaximumPoolSize(10); ds.setMinimumIdle(5); ds.setConnectionInitSql("SET NAMES utf8mb4"); ds.setConnectionTestQuery("SELECT 1"); ds.setValidationTimeout(5000); ds.addDataSourceProperty("cachePrepStmts", "true"); ds.addDataSourceProperty("prepStmtCacheSize", "250"); ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); ds.setConnectionTimeout(30000); ds.setMaximumPoolSize(10); ds.setMinimumIdle(5); ds.setConnectionInitSql("SET NAMES utf8mb4"); ds.setConnectionTestQuery("SELECT 1"); ds.setValidationTimeout(5000); ds.addDataSourceProperty("cachePrepStmts", "true"); ds.addDataSourceProperty("prepStmtCacheSize", "250"); ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); // 添加监听器 ds.setDataSourceCallback(() -> { Connection connection = ds.getConnection(); connection.setAutoCommit(false); return connection; }); ds.addHealthCheckCallback(() -> { try (Connection connection = ds.getConnection()) { connection.isValid(1); return HealthCheckResult.healthy(); } catch (SQLException e) { return HealthCheckResult.unhealthy(e); } }); ds.setMetricsTrackerFactory(new CodahaleMetricsTrackerFactory(registry)); ds.setMetricRegistry(registry); ``` 其中,`setConnectionTimeout`设置连接超时时间,`setMaximumPoolSize`设置连接池最大连接数,`setMinimumIdle`设置连接池最小空闲连接数。 另外,你还可以通过添加监听器和健康检查回调来检测连接是否正常,从而实现断开重连的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值