Reactive and Non Reactive repository in a Spring Boot application with H2 database
Asked Answered
V

0

1

In addition to a non-reactive JPA repository, I introduced a reactive repository in my Spring Boot app with H2 database.

com.app.respository.BusinessRepository extends JpaRepository
com.app.respository.r2dbc.PendingBusinessRepository extends ReactiveCrudRepository

And I added a connection factory for reactive H2.

@Configuration
@EnableR2dbcRepositories
public class R2DBCConfiguration extends AbstractR2dbcConfiguration {
    @Bean
    public H2ConnectionFactory connectionFactory() {
        return new H2ConnectionFactory(
                H2ConnectionConfiguration.builder()
                        .url("jdbc:h2:file:~/data/demo-rxdb")
                        .username("sa")
                        .password("password")
                        .build());
    }
}

After this change, my application is not able to find the non-reactive repository. It says:

Field businessRepository in com.app.service.BusinessServiceImpl required a bean of type 'com.app.repository.BusinessRepository' that could not be found.

The injection point has the following annotations:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.app.repository.BusinessRepository' in your configuration.

I can only guess that maybe my H2 database is now reactive (non-blocking) which is not supported by a JPARepository (blocking). But is my assumption correct?

I have mixed needs. I need only PendingBusiness table data in a non-blocking way (which is constantly being fluxed out on UI through event stream) and I need the rest of the tables' data in a traditional blocking way. Is it possible to achieve what I want through a single H2 database instance?

Valina answered 17/11, 2020 at 11:51 Comment(2)
Does this answer your question? Is it possible to use both Spring Data R2DBC and Spring Data JPA in a single Spring boot application?Complected
Hi Hantsy, In your example, do both repositories work on a single instance of a database? Or would there be two instances, one for each repository? I have two repositories and used your JpaConfig and R2DBCconfig but when my application starts I do not see reactive repository failed to inject in the service. I am using H2 file-based database.Valina

© 2022 - 2024 — McMap. All rights reserved.