Spring Data R2DBC with PostgreSQL Database on Amazon RDS
In this story, we’ll implement a sample REST API that uses Spring Data R2DBC with PostgreSQL Database on Amazon Relational Database Service.
The Reactive Relational Database Connectivity (R2DBC) project brings reactive programming APIs to relational databases. It defines a non-blocking SPI for database driver implementors and client library authors. R2DBC drivers fully implement the database wire protocol on top of a non-blocking I/O layer.
Creating an RDS PostgreSQL Instance
Amazon Relational Database Service (Amazon RDS) makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while automating time-consuming administration tasks, such as hardware provisioning, database setup, patching, and backups. It frees you to focus on your applications so you can give them the fast performance, high availability, security, and compatibility they need. — https://aws.amazon.com/rds/
- Log in to the AWS Management Console and open the Amazon RDS service.
- Create PostgreSQL DB instance on the right button.
3. Configure Security Group for PostgreSQL DB instance
We want to connect to our instance outside the AWS environment. By default, the instance’s VPC security group does not allow any inbound connections. We need to add an inbound rule on the instance security group VPC
4. Connecting to the PostgreSQL DB instance
Open pgAdmin4 then connect to our instance.
Now, we’ll create the first database “book-rdbc-db”
Spring Boot App Setup
We will start by creating a simple Spring Boot project from start.spring.io, with the following dependencies: Spring Reactive Web, Spring Data R2DBC, Spring Data JPA, Liquibase Migration, PostgreSQL Driver, Spring Data JDBC, R2DBC driver, and Lombok.
Spring Data R2DBC supports drivers through R2DBC’s pluggable SPI mechanism. It reacts to database specifics by inspecting the ConnectionFactory and selecting the appropriate database dialect.
The Spring Data reactive driver requires a configuration to connect to the PostgresDB. We can either add our database connection details in application.yml.
We added liquibase configurations to create the database schema and insert data before use by Spring Data R2DBC.
We will be creating a new changelog that creates a new column. Here is our db.changelog-1.0.xml.
Let’s get to the code
Next, add the Java code that will use R2DBC to store and retrieve data from your PostgreSQL database.
Our entity class to represent the Book:
- The id of an entity should be annotated with Spring Data’s @Id annotation.
- Do note that we can not add @Entity (It’s not JPA)
- The @Table annotation is optional.
- Lombok is recommended to be used to avoid boilerplate code.
To manage that class, you’ll need a repository.
To configure R2DBC repositories, we use the @EnableR2dbcRepositories annotation in the Spring boot launcher class.
Complete the implementation by creating the controller.
Test the REST API
- GET all Book
- Create new Book
Congratulations 😊!! Our REST application with Spring Data R2DBC is ready.
In this story, we implemented a REST API application that uses Spring Data R2DBC with PostgreSQL Database on Amazon Relational Database Service.
Although Spring Data R2DBC has advantages. It is not a full ORM like JPA and does not currently support relationships. So developers have to put more manual effort to perform join actions.
The complete source code is available on GitHub.