liveBook · Manning

R2DBC

As explained in Spring in Action, Sixth Edition

R2DBC is a specification and API for nonblocking reactive connectivity to relational databases.

R2DBC (Reactive Relational Database Connectivity) is a separate specification for working with relational databases using reactive types. It provides a nonblocking alternative to JDBC and is built on Reactive Streams rather than being part of Java SE. It can be used with databases including MySQL, PostgreSQL, H2, and Oracle. section 13.1

Spring Data R2DBC adds automatic repository support. To use it in a Spring Boot application, add the Spring Data R2DBC starter, a relational database, and that database’s R2DBC driver. For an in-memory H2 setup, both the H2 library and its R2DBC driver are needed. section 13.1

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>

This Maven dependency adds the Spring Data R2DBC starter; it does not replace the database and driver dependencies. section 13.1

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.r2dbc</groupId>
    <artifactId>r2dbc-h2</artifactId>
    <scope>runtime</scope>
</dependency>

These runtime dependencies supply the H2 database and its matching R2DBC driver. section 13.1

A practical limit appears when an order contains tacos: Spring Data R2DBC does not directly persist relationships in domain classes. The order can hold a taco collection for application use, but marking it `@Transient` prevents R2DBC from trying to persist that collection. Taco IDs are stored separately. section 13.1.4 section 13.4

Listing 13.8 Adding a Taco collection to TacoOrder
@Data
public class TacoOrder {
 
  ...
  
  @Transient
  private transient List<Taco> tacos = new ArrayList<>();
 
  public void addTaco(Taco taco) {
    tacos.add(taco);
    if (taco.getId() != null) {
      tacoIds.add(taco.getId());
    }
  }
 
}

The order keeps tacos in a transient collection while `addTaco()` also records each saved taco’s ID. section 13.1.4

Load an order with its tacos

A service can fetch an order through its repository, look up tacos using the stored IDs, and add them to the order before returning a `Mono<TacoOrder>`. `flatMap()` keeps the result as a `Mono<TacoOrder>` when the lookup itself produces a reactive result, rather than nesting one `Mono` inside another. section 13.1.4

The takeaway: R2DBC enables nonblocking relational persistence, while Spring Data R2DBC supplies repositories. Plan to handle domain relationships explicitly rather than expecting them to be persisted automatically. section 13.1 section 13.4

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines R2DBC against JDBC, walks through dependencies and repository-based relationship handling, but does not examine specification internals.
Examples
Uses H2 dependencies and an order-and-tacos service that saves related objects and loads them by stored IDs.
What it assumes
Assumes a Spring Boot project while showing starter and driver setup and briefly explaining reactive mapping operators.
What you can do after
Apply it via Spring Data R2DBC to configure nonblocking relational repositories and handle unsupported domain relationships explicitly.
How it connects
Connects Reactive Streams, JDBC, Spring Data repositories, domain relationships, and Reactor’s Mono and Flux.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.