Reactive Spring Boot with Relational DB using R2DBC

Syed Hasan
2 min readSep 6, 2020

Reactive practice in development is still quite a new concept for Spring Developers. And in most cases, we come up with tutorials that connect with Mongo DB, which is a nosql database. Therefore it often becomes unfamiliar for existing applications using relational databases to migrate from blocking programming to non-blocking concept. In this post I will try to demonstrate the ways to overcome these issues and how easily we can migrate a Servlet application to reactive application.

Database connectivity

For this application I will connect with PostgreSQL as relational DB. We will require the following dependencies for database connectivity.

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

<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>0.8.4.RELEASE</version>
</dependency>

Make sure to use proper version of your need.

The next thing is configuration.

Configuration

We can configure the application for connecting to database in 2 ways. One is writing configuration in properties file and another one is writing our own configuration implementation.

a) Configuring with application.properties file

b) Alternate way: writing our own connection implementation

For this we have to write a configuration class and extend AbstractR2dbcConfiguration class. We will have to annotate this configuration class with @EnableR2dbcRepositories annotation and scan the packages containing database models. Also we will have to annotate this class with @Configuration annotation. The source code is given below.

Next, we will test our connection.

Execute SQL Scripts

We will execute drop and create table commands using databaseClient. Keep in mind that we are not using hibernate, so we will not get support to use hibernate.hbm2ddl.auto=create-drop. We will have to drop the dable and create it manually before application startup if needed. For simplicity, I have implemented CommandLineRunner interface in Main class and written our SQL execution code in run method.

Using Repository layer

To use repository layer like our traditional spring boot applications, we will have to create an interface and extend it with ReactiveCrudRepository. This will provide us many facilities similar to Spring Data JPA.

Now we can call our repository from service, and our service from controllers. You may already know that, in reactive practice, we wrap up our return type with Mono for returning 0..1 element. For list of elements we use Flux. Therefore I hope you won’t be afraid to see them in our code.

The complete code contains a simple Spring boot reactive back-end application and angular based front end application, which was a practice project from LinkedIn Learning. The source code can be found here.

--

--

Syed Hasan

Software Engineer | Back-End Developer | Spring Developer | Cloud Enthusiast