Search code examples
springkotlinr2dbc

R2DBC - Is there a way to use two different database clients when using a CrudRepository?


Fairly new to Java, Spring and R2DBC.

I am using spring and R2DBC to create to database clients using the properties from my config.

I have a ReactiveCrudRepository that I want to use to make queries to the database, but my application keeps throwing an error

Is this possible to do? If not, what is the best approach?


Solution

  • To follow up on my comment, you'd have:

    DataSource1Configuration.java:

    @EnableR2dbcRepositories(
        basePackages = {"<namespace of repo1>"},
        entityOperationsRef = "dataSource1EntityOperations")
    

    Then:

      @Bean
      public R2dbcEntityOperations dataSource1EntityOperations(ConnectionFactory datasource1ConnectionFactory) {
    DatabaseClient databaseClient =
        DatabaseClient.builder().connectionFactory(datasource1ConnectionFactory).build();
    
    // rest of creation logic
    }
    

    How you create the R2dbcEntityOperations bean really depends on what database you're using, how you're getting the connection factory and so on ... mine is vastly different from what you would do since I was building up a lot of custom functionality.

    And then do a similiar configuration for DataSource2Configuration.java.