简体   繁体   English

Spring Boot 和 R2DBC:io.r2dbc.spi.R2dbcNonTransientResourceException:连接验证失败

[英]Spring Boot and R2DBC: io.r2dbc.spi.R2dbcNonTransientResourceException: Connection validation failed

I'm using Spring Boot 2.4.9 with WebFlux and R2DBC, deployed using the standard Netty server.我正在使用带有 WebFlux 和 R2DBC 的 Spring Boot 2.4.9,使用标准 Netty 服务器进行部署。 The database is PostgreSQL.数据库是 PostgreSQL。 There is only one R2DBC repository class in the app.应用程序中只有一个 R2DBC 存储库类。

R2DBC is configured in application.properties with these properties: R2DBC 在application.properties中配置了以下属性:

spring.r2dbc.username=xxx
spring.r2dbc.password=yyy
spring.r2dbc.url=r2dbc:postgresql://$host:port/database

According to the documentation, a connection pool is automatically configured because r2dbc:pool is on the classpath.根据文档,连接池会自动配置,因为r2dbc:pool在类路径上。

We occasionally incur this exception when the repository class is used:使用存储库类时,我们偶尔会遇到此异常:

org.springframework.dao.DataAccessResourceFailureException: Failed to obtain R2DBC Connection; nested exception is io.r2dbc.spi.R2dbcNonTransientResourceException: Connection validation failed
    at org.springframework.r2dbc.connection.ConnectionFactoryUtils.lambda$getConnection$0(ConnectionFactoryUtils.java:88)
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ Handler com.myco.ManagementController#viewMessages() [DispatcherHandler]
    |_ checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ HTTP GET "/management/management-messages" [ExceptionHandlingWebHandler]
Stack trace:
        at org.springframework.r2dbc.connection.ConnectionFactoryUtils.lambda$getConnection$0(ConnectionFactoryUtils.java:88)
        at reactor.core.publisher.Mono.lambda$onErrorMap$31(Mono.java:3676)
        at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:94)
        at 

(There's more in the stack, but this is all that seems relevant) (堆栈中还有更多内容,但这似乎是相关的)

The nested io.r2dbc.spi.R2dbcNonTransientResourceException: Connection validation failed appears to be the culprit, but I've searched and searched and cannot figure out what I might be doing wrong.嵌套的io.r2dbc.spi.R2dbcNonTransientResourceException: Connection validation failed似乎是罪魁祸首,但我已经搜索和搜索,无法弄清楚我可能做错了什么。

If it helps at all, the application does use these properties, which point to the same database, and are only used to give a regular JDBC connection to a Flyway bean:如果它有帮助,应用程序确实使用了这些属性,它们指向同一个数据库,并且仅用于提供到 Flyway bean 的常规 JDBC 连接:

spring.datasource.username=xxx
spring.datasource.password=yyy
spring.datasource.url=jdbc:postgresql://host:port/database

Just in case that might interfere with R2DBC, but I can't imagine that I would.以防万一这可能会干扰 R2DBC,但我无法想象我会这样做。

In addition, it seems like after the application server first starts up, we don't see the error for something like the first 10 or so minutes, but the error will start appearing after that.此外,似乎在应用程序服务器第一次启动后,前 10 分钟左右我们看不到错误,但之后会开始出现错误。 The error only occurs occasionally too;该错误也只是偶尔发生; it doesn't happen all the time.它不会一直发生。

The only way that I can reproduce the error on my workstation is when I stop and restart the Docker container in which PostgreSQL is running, while leaving the application server running the whole time.我可以在我的工作站上重现错误的唯一方法是停止并重新启动运行 PostgreSQL 的 Docker 容器,同时让应用程序服务器一直运行。 I then get the same error, but then the error is persistent and happens every time, where in our production environment, the error is intermittent.然后我得到同样的错误,但是错误是持久的并且每次都会发生,在我们的生产环境中,错误是间歇性的。

I've used Spring Boot + R2DBC on other production applications (but those used MySQL) and have never seen this problem before.我在其他生产应用程序(但那些使用 MySQL)上使用过 Spring Boot + R2DBC 并且以前从未见过这个问题。

What is causing that connection validation error and how can I fix it?是什么导致了连接验证错误,我该如何解决?

Thank you!谢谢!

As it turns out, updating Postgres and R2DBC dependencies fixed the problem.事实证明,更新 Postgres 和 R2DBC 依赖项解决了这个问题。 I replaced this dependency:我替换了这个依赖:

implementation 'io.r2dbc:r2dbc-postgresql'

with these dependencies:具有这些依赖项:

implementation 'org.postgresql:r2dbc-postgresql:0.9.0.RELEASE'
implementation 'io.r2dbc:r2dbc-pool:0.9.0.RELEASE'
implementation 'io.r2dbc:r2dbc-spi:0.9.1.RELEASE'

Note that for the r2dbc-postgresql dependency, the group changed from io.r2dbc to org.postgres .请注意,对于r2dbc-postgresql依赖项,组从io.r2dbc更改为org.postgres

This comment in the R2DBC pool issues was especially helpful. R2DBC 池问题中的此评论特别有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM