[英]Spring Boot and R2DBC: io.r2dbc.spi.R2dbcNonTransientResourceException: Connection validation failed
我正在使用带有 WebFlux 和 R2DBC 的 Spring Boot 2.4.9,使用标准 Netty 服务器进行部署。 数据库是 PostgreSQL。 应用程序中只有一个 R2DBC 存储库类。
R2DBC 在application.properties
中配置了以下属性:
spring.r2dbc.username=xxx
spring.r2dbc.password=yyy
spring.r2dbc.url=r2dbc:postgresql://$host:port/database
根据文档,连接池会自动配置,因为r2dbc:pool
在类路径上。
使用存储库类时,我们偶尔会遇到此异常:
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
(堆栈中还有更多内容,但这似乎是相关的)
嵌套的io.r2dbc.spi.R2dbcNonTransientResourceException: Connection validation failed
似乎是罪魁祸首,但我已经搜索和搜索,无法弄清楚我可能做错了什么。
如果它有帮助,应用程序确实使用了这些属性,它们指向同一个数据库,并且仅用于提供到 Flyway bean 的常规 JDBC 连接:
spring.datasource.username=xxx
spring.datasource.password=yyy
spring.datasource.url=jdbc:postgresql://host:port/database
以防万一这可能会干扰 R2DBC,但我无法想象我会这样做。
此外,似乎在应用程序服务器第一次启动后,前 10 分钟左右我们看不到错误,但之后会开始出现错误。 该错误也只是偶尔发生; 它不会一直发生。
我可以在我的工作站上重现错误的唯一方法是停止并重新启动运行 PostgreSQL 的 Docker 容器,同时让应用程序服务器一直运行。 然后我得到同样的错误,但是错误是持久的并且每次都会发生,在我们的生产环境中,错误是间歇性的。
我在其他生产应用程序(但那些使用 MySQL)上使用过 Spring Boot + R2DBC 并且以前从未见过这个问题。
是什么导致了连接验证错误,我该如何解决?
谢谢!
事实证明,更新 Postgres 和 R2DBC 依赖项解决了这个问题。 我替换了这个依赖:
implementation 'io.r2dbc:r2dbc-postgresql'
具有这些依赖项:
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'
请注意,对于r2dbc-postgresql
依赖项,组从io.r2dbc
更改为org.postgres
。
R2DBC 池问题中的此评论特别有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.