简体   繁体   中英

Failed to obtain R2DBC Connection Java Spring

I am getting following exeption connecting to Mssql Server.

> org.springframework.dao.DataAccessResourceFailureException: Failed to obtain R2DBC Connection; nested exception is java.net.UnknownHostException: failed to resolve '' after 10 queries 
    at org.springframework.r2dbc.connection.ConnectionFactoryUtils.lambda$getConnection$0(ConnectionFactoryUtils.java:88) ~[spring-r2dbc-5.3.2.jar:5.3.2]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ Handler com.reactive.testreactive.controller.TestStreamController#findAll() [DispatcherHandler]
    |_ checkpoint ⇢ HTTP GET "/test" [ExceptionHandlingWebHandler]

I connected to JDBC with the same configuration in properties but having an issue while trying to connect to R2DBC. Happens on rest and not on starting an app.

   @Bean
public MssqlConnectionFactory connectionFactory() {
    return new MssqlConnectionFactory(MssqlConnectionConfiguration.builder()
            .host("host")
            .port(1433)
            .database("DataBase")
            .username("username")
            .password("password")
            .build());
}

I am facing similar issue with R2DBC using the exact same connection info. Although I am getting a connection closed by peer instead of a UnknownHostException

Do you use absolutely the same configuration? Because if you are starting using R2DBC you should replace database URL: jdbc:postgresql://... -> r2dbc:postgresql://

If anyone still looking for solution, I'm adding solution below.

First, make sure your database host is visible. By looking at your error statement which says -- failed to resolve '' after 10 queries , means it is empty '' . The way that you have defined the host name is not correct. If you are injecting the host value from properties, you might want to cross check that and then follow the steps below.

If you are configuring through YMLs, you can use below configurations:

spring:
    data:
        r2dbc:
            repositories:
                enabled: true
    r2dbc:
        url: r2dbc:sqlserver://<just_host>:<port>
        username: <db_username>
        password: <db_password>
        name: <db_name>

spring.data.r2dbc.repositories.enabled: true is optional here.

If you are creating custom bean, you can create bean like below. Please note that I'm returning ConnectionFactory rather than MssqlConnectionFactory .

@Bean
public ConnectionFactory connectionFactory() {
  return new MssqlConnectionFactory(
      MssqlConnectionConfiguration.builder()
          .host("just_host")
          .database("db_name")
          .port(1433)
          .username("db_username")
          .password("db_password")
          .build());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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