Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
}
like image 461
meuhedet meuhedet Avatar asked Oct 16 '25 02:10

meuhedet meuhedet


1 Answers

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());
}
like image 115
Anish Panthi Avatar answered Oct 17 '25 14:10

Anish Panthi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!