3

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());
}
meuhedet meuhedet
  • 465
  • 2
  • 5
  • 9
  • this will help https://stackoverflow.com/questions/57971278/connection-pool-size-with-postgres-r2dbc-pool – deadshot Dec 15 '20 at 10:23
  • Failed to obtain R2DBC Connection; nested exception is java.net.UnknownHostException: failed to resolve 'Database' after 10 queries – meuhedet meuhedet Dec 15 '20 at 10:42
  • Does this answer your question? [Connection pool size with postgres r2dbc-pool](https://stackoverflow.com/questions/57971278/connection-pool-size-with-postgres-r2dbc-pool) – Karthik VU Dec 15 '20 at 11:19
  • Did not help: following error: Failed to obtain R2DBC Connection; nested exception is java.net.UnknownHostException: failed to resolve 'Database' after 10 queries – meuhedet meuhedet Dec 15 '20 at 11:30
  • can you show your configuration ? the error tells that the host of connection is invalid – Zinc Dec 15 '20 at 16:52
  • I edited. Connected with the same configuration to jdbc and it worked – meuhedet meuhedet Dec 16 '20 at 07:56
  • You are using Spring BOot, use that to configure R2DBC instead of manually providing a bean. Does it happen all the time or after a period of time? Looks like a connection leak to me. – M. Deinum Dec 16 '20 at 12:53
  • So bassicly in my organisation i needed to configured another instance. Do you know guys if i can use R2DBC to read from database reactively when there is an insert in it? – meuhedet meuhedet Dec 16 '20 at 13:24
  • 2
    Any luck figuring this out? I am hitting the a very similar issue where it works with JDBC, but not with R2DBC using the exact same connection info. Although I am getting a connection timed out instead of a UnknownHostException – SeanFranklin Jan 22 '21 at 02:30

2 Answers2

2

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());
}
Anish Panthi
  • 797
  • 8
  • 17
0

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