Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Introduction

R2DBC (Reactive Relational Database Connectivity) is an effort presented by Pivotal during Spring One Platform 2018. It intends to create a reactive API to SQL databases.

In other words, this effort creates a database connection using fully non-blocking drivers.

In this tutorial, we'll have a look at an example of an application using Spring Data R2BDC. For a guide to the more low-level R2DBC API, have a look at our previous article.

2. Our First Spring Data R2DBC Project

To begin with, the R2DBC project is very recent. At this moment, only PostGres, MSSQL, and H2 have R2DBC drivers. Further, we can't use all Spring Boot functionality with it. Therefore there're some steps that we'll need to manually add. But, we can leverage projects like Spring Data to help us.

We'll create a Maven project first. At this point, there are a few dependency issues with R2DBC, so our pom.xml will be bigger than it'd normally be.

For the scope of this article, we'll use H2 as our database and we'll create reactive CRUD functions for our application.

Let's open the pom.xml of the generated project and add the appropriate dependencies as well as some early-release Spring repositories:


Other required artifacts include Lombok, Spring WebFlux and a few others that complete our project dependencies.

3. Connection Factory

When working with a database, we need a connection factory. So, of course, we'll need the same thing with R2DBC.

So we'll now add the details to connect to our instance:

class R2DBCConfiguration extends AbstractR2dbcConfiguration {
    public H2ConnectionFactory connectionFactory() {
        return new H2ConnectionFactory(

The first thing we notice in the code above is the @EnableR2dbcRepositories. We need this annotation to use Spring Data functionality. In addition, we're extending the AbstractR2dbcConfiguration since it'll provide a lot of beans that we'd need later on.

4. Our First R2DBC Application

Our next step is to create the repository:

interface PlayerRepository extends ReactiveCrudRepository<Player, Integer> {}

The ReactiveCrudRepository interface is very useful. It provides, for example, basic CRUD functionality.

Finally, we'll define our model class. We'll use Lombok to avoid boilerplate code:

class Player {
    Integer id;
    String name;
    Integer age;

5. Testing

It's time to test our code. So, let's start by creating a few test cases:

public void whenDeleteAll_then0IsExpected() {

public void whenInsert6_then6AreExpected() {

6. Custom Queries

We can also generate custom queries. In order to add it, we'll need to change our PlayerRepository:

@Query("select id, name, age from player where name = $1")
Flux<Player> findAllByName(String name);

@Query("select * from player where age = $1")
Flux<Player> findByAge(int age);

In addition to the existing tests, we'll add tests to our recently updated repository:

public void whenSearchForCR7_then1IsExpected() {

public void whenSearchFor32YearsOld_then2AreExpected() {

private void insertPlayers() {
    List<Player> players = Arrays.asList(
        new Player(1, "Kaka", 37),
        new Player(2, "Messi", 32),
        new Player(3, "Mbappé", 20),
        new Player(4, "CR7", 34),
        new Player(5, "Lewandowski", 30),
        new Player(6, "Cavani", 32)

7. Batches

Another feature of R2DBC is to create batches. A batch is useful when executing multiple SQL statements as they'll perform better than individual operations.

To create a Batch we need a Connection object:

Batch batch = connection.createBatch();

After our application creates the Batch instance, we can add as many SQL statements as we want. To execute it, we'll invoke the execute() method. The result of a batch is a Publisher that'll return a result object for each statement.

So let's jump into the code and see how we can create a Batch:

public void whenBatchHas2Operations_then2AreExpected() {
      .flatMapMany(connection -> Flux.from(connection
        .add("select * from player")
        .add("select * from player")

8. Conclusion

To summarize, R2DBC is still in an early stage. It's an attempt to create an SPI that will define a reactive API to SQL databases. When used with Spring WebFlux, R2DBC allows us to write an application that handles data asynchronously from the top and all the way down to the database.

As always the code is available at GitHub.

Persistence bottom

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

Persistence footer banner
Comments are closed on this article!