如何让 Spring Boot 自动重新连接到 PostgreSQL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39563585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:23:37  来源:igfitidea点击:

How do I get Spring Boot to automatically reconnect to PostgreSQL?

javaspringpostgresqlspring-bootspring-jdbc

提问by mattm

I am running Spring Boot connecting to a PostgreSQL database. I have verified that data is written to the database if Spring Boot is started after the database.

我正在运行连接到 PostgreSQL 数据库的 Spring Boot。我已经验证了如果在数据库之后启动Spring Boot,则数据写入数据库。

spring.datasource.url = jdbc:postgresql://localhost/dbname
spring.datasource.username = user
spring.datasource.password = secret
spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1

When there are changes in the database in development, I run into exceptions: PreparedStatementCallback; SQL []; This connection has been closed.; nested exception is org.postgresql.util.PSQLException: This connection has been closed.

当开发中的数据库发生变化时,我遇到了异常: PreparedStatementCallback; SQL []; This connection has been closed.; nested exception is org.postgresql.util.PSQLException: This connection has been closed.

Note that the database is accessible again when the exceptions occur; I think the problem is that connection is stale because the database endpoint it originally connected to is no longer available because of the restart. Restarting Spring Boot resolves the issue.

注意,发生异常时,数据库是可以再次访问的;我认为问题在于连接已经过时,因为它最初连接的数据库端点由于重新启动而不再可用。重新启动 Spring Boot 可以解决该问题。

How do I configure Spring Boot to reconnect to PostgreSQL so that I do not need to restart it?

如何配置 Spring Boot 以重新连接到 PostgreSQL,这样我就不需要重新启动它?

I have attempted to follow the answers in Spring Boot JPA - configuring auto reconnectand How to reconnect database if the connection closed in spring jpa?. I am not sure whether the PostgreSQL behavior is different from MySQL. My reading of the Spring Boot documentationhas not helped; I do not know the components described well enough to understand what documentation I should be looking at.

我试图遵循Spring Boot JPA 中的答案- 配置自动重新连接如果连接在 spring jpa 中关闭,如何重新连接数据库?. 我不确定 PostgreSQL 的行为是否与 MySQL 不同。我对Spring Boot 文档的阅读没有帮助;我不太了解所描述的组件,无法理解我应该查看哪些文档。

回答by rhorvath

Spring boot should be configured to reconnect automatically, problem is that it is not aware of the broken connection.

Spring boot 应该配置为自动重新连接,问题是它不知道断开的连接。

Since you are already using test on borrow and validation query, just try reducing validation intervalso it is executed every time.

由于您已经在借用和验证查询上使用测试,只需尝试减少验证间隔,以便每次都执行。

spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validation-query=SELECT 1
spring.datasource.tomcat.validation-interval=0

Tomcat jdbc connection poolon testOnBorrow:

Tomcat的JDBC连接池testOnBorrow

(boolean) The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another. NOTE - for a true value to have any effect, the validationQuery or validatorClassName parameter must be set to a non-null string. In order to have a more efficient validation, see validationInterval. Default value is false

(boolean) 在从池中借用对象之前是否将对其进行验证的指示。如果对象无法验证,它将从池中删除,我们将尝试借用另一个。注意 - 要使真值产生任何效果,validationQuery 或validatorClassName 参数必须设置为非空字符串。为了进行更有效的验证,请参阅 validationInterval。默认值为 false

But be aware of validationInterval:

但请注意validationInterval

(long) avoid excess validation, only run validation at most at this frequency - time in milliseconds. If a connection is due for validation, but has been validated previously within this interval, it will not be validated again.The default value is 30000 (30 seconds).

(长)避免过度验证,最多只能在此频率下运行验证 - 时间以毫秒为单位。如果某个连接需要验证,但之前已在此时间间隔内验证过,则不会再次验证该连接。默认值为 30000(30 秒)。