title: Spring Boot and R2DBC: io.r2dbc.spi.R2dbcNonTransientResourceException: Connection validation failed
body: <p>I'm using Spring Boot 2.4.9 with WebFlux and R2DBC, deployed using the standard Netty server.  The database is PostgreSQL.  There is only one R2DBC repository class in the app.</p>
  <p>R2DBC is configured in <code>application.properties</code> with these properties:</p>
  <pre><code>spring.r2dbc.username=xxx
  spring.r2dbc.password=yyy
  spring.r2dbc.url=r2dbc:postgresql://$host:port/database
  </code></pre>
  <p>According to the documentation, a connection pool is automatically configured because <code>r2dbc:pool</code> is on the classpath.</p>
  <p>We <strong>occasionally</strong> incur this exception when the repository class is used:</p>
  <pre><code>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 &quot;/management/management-messages&quot; [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 
  </code></pre>
  <p>(There's more in the stack, but this is all that seems relevant)</p>
  <p>The nested <code>io.r2dbc.spi.R2dbcNonTransientResourceException: Connection validation failed</code> appears to be the culprit, but I've searched and searched and cannot figure out what I might be doing wrong.</p>
  <p>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:</p>
  <pre><code>spring.datasource.username=xxx
  spring.datasource.password=yyy
  spring.datasource.url=jdbc:postgresql://host:port/database
  </code></pre>
  <p>Just in case that might interfere with R2DBC, but I can't imagine that I would.</p>
  <p>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.  The error only occurs occasionally too; it doesn't happen all the time.</p>
  <p>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.  I then get the same error, but then the error is persistent and happens <em>every</em> time, where in our production environment, the error is intermittent.</p>
  <p>I've used Spring Boot + R2DBC on other production applications (but those used MySQL) and have never seen this problem before.</p>
  <p>What is causing that connection validation error and how can I fix it?</p>
  <p>Thank you!</p>