如何在SpringBoot应用程序中使用hikari配置自动重新连接? [英] How to configure auto reconnection with hikari in SpringBoot application?

查看:860
本文介绍了如何在SpringBoot应用程序中使用hikari配置自动重新连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的是SpringBoot 2.1.x版本,因此Hikari是默认的DataSource实现.但是,我不确定在数据库维护/重新启动或网络连接出现问题后,如何配置Hikari设置以自动重新连接到我们的Oracle数据库.

We are using SpringBoot 2.1.x version so Hikari is the default DataSource implementation. However, I am not sure how to configure Hikari settings to auto reconnect to our Oracle database after database maintenance/restart or network connection issue.

我们具有以下hikari设置,但似乎无济于事.

We have the following hikari settings but it does not seem to help.

account.datasource.url: jdbc:oracle:thin:@myserver:1521:DEV
account.datasource.username: user
account.datasource.password: xxxx
account.datasource.driverClassName: oracle.jdbc.driver.OracleDriver

account.datasource.hikari.connection-timeout: 30000
account.datasource.hikari.maximum-pool-size: 3
account.datasource.hikari.idle-timeout: 60000
account.datasource.hikari.max-lifetime: 1800000
account.datasource.hikari.minimum-idle: 2

与数据库的网络连接恢复后,重新连接失败.

It failed to reconnect after network connection to the database got restored.

无法获得JDBC连接;嵌套的异常是java.sql.SQLTransientConnectionException:HikariPool-1-连接不可用,请求在30033ms之后超时.

Failed to obtain JDBC Connection; nested exception is java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30033ms.

任何其他account.datasource.hikari.xxxxx将有助于自动重新连接到数据库吗?

Any other account.datasource.hikari.xxxxx will help to auto reconnect to the database ?

推荐答案

来自HikariCP 文档:

From the HikariCP docs:

connectionTestQuery

connectionTestQuery

如果您的驱动程序支持JDBC4,我们强烈建议建议不要设置此属性.这是针对旧版"驱动程序的不支持JDBC4 Connection.isValid()API.这是查询该操作将在从以下位置给您提供连接之前执行池以验证与数据库的连接仍然存在活.再次尝试在不使用此属性HikariCP的情况下运行池如果您的驱动程序不兼容JDBC4,将记录错误知道.默认值:无

If your driver supports JDBC4 we strongly recommend not setting this property. This is for "legacy" drivers that do not support the JDBC4 Connection.isValid() API. This is the query that will be executed just before a connection is given to you from the pool to validate that the connection to the database is still alive. Again, try running the pool without this property, HikariCP will log an error if your driver is not JDBC4 compliant to let you know. Default: none

因此,我建议验证您的JDBC驱动程序是否确实符合JDBC4.如果不是,请设置上述属性.

So I'd suggest verifying that your JDBC Driver is actually JDBC4 compliant. If it's not - set the above property.

这篇关于如何在SpringBoot应用程序中使用hikari配置自动重新连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆