Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use both Spring Data R2DBC and Spring Data JPA in a single Spring boot application?

I have an application which is using Spring data JPA and hibernate envers for db auditing. Since R2DBC doesn't support auditing yet, is it possible to use combination of both in a single application?

If yes, Plan is to use Spring Data JPA for insert, update and delete operations, so that all DB auditing will be handled by hibernate envers. And use R2DBC for reactive non-blocking API's to read data.

If no, Is there any suggestions on how to achieve both reactive API's and auditing?

like image 588
Purushotham Avatar asked Jun 08 '20 00:06

Purushotham


1 Answers

  1. Spring provided simple Auditing via @EnableR2dbcAuditing, check my example.

  2. Mix JPA in a reactive application is also possible, I have an example to demo run JPA in a reactive application, but not added r2dbc in it.

  3. For your plan, a better solution is applying the cqrs pattern on the database topology, use database cluster for your application.

    • JPA for applying changes, use the main/master database to accept the modification, and sync changes to the secondary/slave database.
    • r2dbc for queries as you expected, use the secondary/slave database to query.
    • use a gateway at the front for the query and command service.

Update: I have created a sample to demo JPA and R2dbc coexistence in a single webflux application. But I do not suggest using it in real-world applications. Consider the 3rd solution above if you really need it.

like image 118
Hantsy Avatar answered Sep 17 '22 13:09

Hantsy