Introduction about Reactive Relational Database Connectivity (R2DBC)

In the past, when working with Reactive applications that use database, we would usually use a MongoDB database because very few databases supported the Reactive mechanism except MongoDB. But now you do not need to use MongoDB database anymore, Reactive Relational Database Connectivity (R2DBC) will help us work with many other relational database systems according to Reactive mechanism. R2DBC is a spec that defines how we will work with relational databases like MySQL, PostgreSQL, … according to Reactive mechanism. It provides us with a Service Provider Interface (SPI) that we can implement driver for each respective database system. There are many drivers that implement this SPI, for example, R2DBC PostgreSQL implements R2DBC for PostgreSQL database, similarly, we also have R2DBC MySQL support for MySQL database, … In this tutorial, I will introduce to you all about R2DBC with an example of how to use the R2DBC PostgreSQL driver to manipulate the PostgreSQL database!

You can see details of the specs that R2DBC defines here.

First, I will create a new Maven project:

Introduction about Reactive Relational Database Connectivity (R2DBC)

with R2DBC PostgreSQL driver dependency as follows:

Similar to when you work with JDBC, the first thing we need to do with any database is setup a connection to it. With R2DBC, we will use the ConnectionFactory object to do this.

There are two ways we can do this in R2DBC:

  • Using the connection URL
  • Using directly Java code

With the connection URL, you can use the following URL for PostgreSQL database (for other database systems, the connection URL will be different a bit!):

Inside:

  • host is PostgreSQL server,
  • port is the running PostgreSQL port. PostgreSQL’s default port is 5432.
  • database is the name of the database we will be working with.
  • username is the user logged into the PostgreSQL database
  • password is the password of the user

Then use the ConnectionFactories object to get the ConnectionFactory object from this connection URL:

If you use Java code, you can use the Builder object in the ConnectionFactoryOptions class to do this:

After we have a ConnectionFactory object, we can create a Publisher of connections to the database by calling:

Initialize the Project Reactor Flux object to subscribe to this Publisher object:

Now, we can use the connection from this Flux object to manipulate the database, for example:

Here, we use the createStatement() method to pass the query you want to use to the Connection execute object and return the results.

Now, for example, in my PostgreSQL database server test database, there is a table named student with the column name and address as follows:

To insert a new record into this table with R2DBC, I will write the following code:

As you can see, we can bind the value we want to the query using the bind() method of the Statement object and after we have bound the data, we can call the execute() method of this Statement object to run the SQL statement.

The result after executing the SQL statement will be returned in the Result object of R2DBC, we must consume this Result object in order to fully execute this SQL statement. Therefore, we need to write more code like this:

The getRowsUpdated() method will return the number of records that are updated after the execution of the SQL statement. The Result object also contains another method called map() that allows us to get the data returned from the database, with a SELECT statement, for example.

We need to call the subscribe() method to subscribe to Publisher.

The entire code could be rewritten as follows:

Here, I have used the doOnNext() method to get information about the number of records that are updated in the database. And since we need to wait for the program to finish running and then exit, we also use Thread.sleep() for 5 seconds to do this.

The results when running the above code are as follows:

Introduction about Reactive Relational Database Connectivity (R2DBC)

Check database:

Introduction about Reactive Relational Database Connectivity (R2DBC)

Add Comment