การทำระบบด้วย Spring WebFlux ซึ่งเป็น reactive framework จำเป็นต้องคำนึงถึงทุกชั้นของแอปพลิเคชันให้ทำงานแบบ non-blocking รวมถึงการเชื่อมต่อกับฐานข้อมูล ซึ่งเป็นจุดสำคัญที่มักถูกมองข้าม บทความนี้จะแนะนำการใช้งาน R2DBC สำหรับฐานข้อมูลเชิง relational และ MongoDB Reactive สำหรับ NoSQL โดยใช้ Spring Boot
ทำไมต้องใช้ Reactive Database
- JDBC เป็น synchronous และ blocking
- WebFlux ต้อง non-blocking ตั้งแต่ Controller ถึง Database
- R2DBC และ MongoDB Reactive รองรับการทำงานกับ Mono และ Flux
R2DBC คืออะไร
R2DBC (Reactive Relational Database Connectivity) คือมาตรฐานใหม่ในการเชื่อมต่อฐานข้อมูล SQL แบบ non-blocking
Gradle dependencies
dependencies { implementation("org.springframework.boot:spring-boot-starter-data-r2dbc") implementation("io.r2dbc:r2dbc-postgresql") runtimeOnly("org.postgresql:postgresql") }
application.yml
spring: r2dbc: url: r2dbc:postgresql://localhost:5432/mydb username: user password: password sql: init: mode: always
Entity
@Table("users") public class User { @Id private Long id; private String name; }
Repository
public interface UserRepository extends ReactiveCrudRepository<User, Long> { Flux<User> findByName(String name); }
Service
@Service public class UserService { private final UserRepository repo; public UserService(UserRepository repo) { this.repo = repo; } public Flux<User> getAll() { return repo.findAll(); } }
MongoDB Reactive
MongoDB มี driver แบบ reactive ซึ่งสามารถใช้กับ Spring Boot ได้ทันทีผ่าน Spring Data MongoDB Reactive
dependencies
dependencies { implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive") }
application.yml
spring: data: mongodb: uri: mongodb://localhost:27017/mydb
Document
@Document("products") public class Product { @Id private String id; private String name; }
Repository
public interface ProductRepository extends ReactiveMongoRepository<Product, String> { Mono<Product> findByName(String name); }
Controller
@RestController @RequestMapping("/products") public class ProductController { private final ProductRepository repository; public ProductController(ProductRepository repository) { this.repository = repository; } @GetMapping("/{name}") public Mono<Product> getByName(@PathVariable String name) { return repository.findByName(name); } }
เปรียบเทียบ R2DBC และ MongoDB Reactive
หัวข้อ | R2DBC | MongoDB Reactive |
---|---|---|
ประเภทฐานข้อมูล | Relational (SQL) | NoSQL |
ความยืดหยุ่น schema | คงที่ | ยืดหยุ่น |
การสนับสนุน Reactive | ผ่าน R2DBC | Native Support |
ภาพประกอบระบบการทำงาน

ข้อควรระวัง
- ห้ามใช้ JDBC ร่วมกับ WebFlux
- ควรตรวจสอบว่า database driver รองรับ reactive หรือไม่
- อย่าลืมเขียน fallback หรือ onError สำหรับ Mono และ Flux เสมอ
สรุป
- R2DBC เหมาะกับระบบ SQL ที่ต้องการ performance แบบ non-blocking
- MongoDB Reactive เหมาะกับระบบ NoSQL ที่เน้น scalability และความยืดหยุ่น
- ทั้งสองสามารถใช้งานร่วมกับ Spring Boot WebFlux ได้อย่างสมบูรณ์
คำค้น SEO
Reactive Database Connectivity, R2DBC, MongoDB Reactive, spring webflux r2dbc, reactive crud repository, spring mongodb reactive, r2dbc vs jdbc, reactive persistence spring, non-blocking database spring boot