JVM Advent

The JVM Programming Advent Calendar

R2DBC – Reactive relational database connectivity


R2DBC is a specification designed for reactive programming /stream oriented access to SQL databases. It is fully non-blocking. The main initiative of R2dbc is to establish an open standard on reactive programming with SQL databases. This comprises specifications (R2DBC-SPI) and R2DBC drive implementations.
Here are the current driver implementations supported by R2dbc

  1. r2dbc-h2
  2. r2dbc-postgres
  3. r2dbc-mssql
  4. r2dbc-mysql
  5. jasync-sql
  6. Google cloud-spanner-r2dbc

R2DBC has created the need for a non-blocking application to handle concurrency with a small number of threads and scale with fewer hardware resources. It’s an alternate extension for database access instead of standard JDBC API.

Let’s get started
Include the following dependencies to your pom.xml



R2dbc Configuration
Connection factory – This can be configured via application YAML

url: r2dbc:postgres://admin:admin@localhost:5432/customer

This can be initialized from the above configured ConnectionFactory instance. Also, this class can use to populate and initialize the database using SQL scripts. Here is the sample configuration

public ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory){
ConnectionFactoryInitializer initializer = newConnectionFactoryInitializer();

CompositeDatabasePopulator populator = newCompositeDatabasePopulator();
return initializer;

This gives full control over the reactive repository and it uses project reactor types and also built on top of Reactive Streams.

public interface CustomerRepository extends ReactiveCrudRepository<Customer,Integer>{


Testing with StepVerifier
The StepVerifier will help us understand how data is getting processed in each step of a reactive stream. Verify method will validate all the expectations defined above.

In the below example, verify the expected onNext call count from the stream.

public void findAll_withExistingRecord_returnAllRecord(){

How about Transaction
You can always use @Transactional around the method to enable the transaction in a typical annotation way however, R2dbc provide another an option to define this in a programmatic way using TransactionalOperator


You can download the project code from my [GitHub Repository].

Next Post

Previous Post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024 JVM Advent | Powered by steinhauer.software Logosteinhauer.software

Theme by Anders Norén