Transaction boundary nên đặt quanh use-case cần atomic, thường ở service/application layer, không rải commit trong từng repository nhỏ. Repository chỉ thao tác dữ liệu; service quyết định commit/rollback cho toàn workflow.
Ví dụ ý tưởng:
async with session.begin():
order = await orders.create(session, payload)
await inventory.reserve(session, order.items)Nếu mỗi repository tự commit, workflow nhiều bước sẽ khó rollback khi bước sau lỗi, dễ tạo dữ liệu nửa vời.
Transaction boundaries should wrap the atomic use case, usually in the service/application layer, not scattered as commits inside small repositories. Repositories manipulate data; the service decides commit/rollback for the whole workflow.
Concept example:
async with session.begin():
order = await orders.create(session, payload)
await inventory.reserve(session, order.items)If every repository commits by itself, multi-step workflows become hard to roll back when a later step fails, creating partial data.