Spring Data R2DBC is a Spring Data module that enables reactive, non-blocking database access for relational databases using Mono and Flux. Unlike JDBC or JPA, it avoids blocking calls, making applications more scalable and high-performing.
- Reactive Model: Uses non-blocking APIs with Mono and Flux
- High Performance: Efficiently handles large concurrent requests
- Scalable: Well-suited for microservices and cloud-native systems
- Event-Driven: Ideal for real-time and asynchronous workflows
- Database Support: Works with R2DBC-supported databases like PostgreSQL and MySQL
Prerequisites
- Java 17 or higher
- Spring Framework 6.0.11 or higher
- R2DBC and above
- A database that supports R2DBC, such as PostgreSQL, MySQL, or MongoDB
Dependencies
Maven Dependencies:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-r2dbc -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>3.1.3</version>
</dependency>
<!-- R2DBC driver for your chosen database -->
<!-- https://mvnrepository.com/artifact/io.r2dbc/r2dbc-postgresql -->
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>0.8.13.RELEASE</version>
</dependency>
<!-- Spring Boot Starter for Web (Optional, if building a web application) -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.3</version>
</dependency>
<!-- Other dependencies your application may need -->
</dependencies>
Gradle Dependencies:
dependencies {
// Spring Boot Starter for R2DBC
implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
// R2DBC driver for your chosen database
implementation 'io.r2dbc:r2dbc-postgresql' // Example for PostgreSQL
// Spring Boot Starter for Web (Optional, if building a web application)
implementation 'org.springframework.boot:spring-boot-starter-web'
// Other dependencies your application may need
}
Basic steps to connect a database to a Spring Data R2DBC Application
1. Add R2DBC driver dependency in pom.xml. For example for Postgres:
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
</dependency>
2. Define database configuration properties in application.properties:
spring.r2dbc.url=r2dbc:postgresql://localhost:5432/database
spring.r2dbc.username=username
spring.r2dbc.password=password
3. Autowire ConnectionFactory in a configuration class:
@Configuration
public class R2dbcConfig {
@Autowired
ConnectionFactory connectionFactory;
}
4. Define entity classes and repository interfaces:
5. Autowire repository in a service class:
@Service
public class UserService {
private UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
6. Run the application. Spring Data R2DBC will autoconfigure the connection:
7. Invoke repository methods from service classes to perform CRUD operations:
Step-by-Step Implementation
Step 1: Create Spring Boot Project
- Use Spring Initializr
- Select dependencies: Spring Web , Spring Data R2DBC and PostgreSQL Driver (or your DB)
- R2DBC = Reactive (Non-blocking DB access)
Step 2: Add Dependencies
- Add R2DBC + database driver
- Example: PostgreSQL
Maven
<dependencies>
<!-- Spring Data R2DBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<!-- R2DBC PostgreSQL Driver -->
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
</dependency>
<!-- Optional: Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
Gradle
implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
implementation 'io.r2dbc:r2dbc-postgresql'
Step 3: Configure Database
- Configure DB connection in application.properties
- Use R2DBC URL (not JDBC)
spring.r2dbc.url=r2dbc:postgresql://localhost:5432/testdb
spring.r2dbc.username=postgres
spring.r2dbc.password=admin
Step 4: Create Entity Class
- Represents table in database
- Use R2DBC annotations
Employee.java
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
@Table("employee")
public class Employee {
@Id
private Integer id;
private String name;
private Double salary;
// Getters & Setters
}
Step 5: Create Repository Interface
- Extends ReactiveCrudRepository
- Provides CRUD methods automatically
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
public interface EmployeeRepository
extends ReactiveCrudRepository<Employee, Integer> {
Flux<Employee> findByName(String name);
}
Step 6: Create Service Class
- Contains business logic
- Uses repository methods
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository repository;
public Mono<Employee> save(Employee emp) {
return repository.save(emp);
}
public Flux<Employee> getAll() {
return repository.findAll();
}
public Mono<Employee> getById(int id) {
return repository.findById(id);
}
public Flux<Employee> getByName(String name) {
return repository.findByName(name);
}
}
Step 7: Create Controller
- Exposes REST APIs
- Calls service methods
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeService service;
@PostMapping
public Mono<Employee> save(@RequestBody Employee emp) {
return service.save(emp);
}
@GetMapping
public Flux<Employee> getAll() {
return service.getAll();
}
}
Step 8: Run Application
- Spring Boot auto-configures R2DBC
- No manual connection handling required
- Application starts on default port (8080)