Transaction boundaries
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
put the transaction around one business operation.
Planned topics
- The boundary is the business operation, for example "place order", not a single SQL statement.
- Put
@Transactionalon service methods, not on controllers or repositories. - Repository calls inside join the service's transaction.
- Without a boundary each repository call commits alone, and a failure leaves partial data.
- Keep the business operation as one public service method.
Draft outline details
Demonstration
@Service
public class OrderService {
@Transactional
public Order placeOrder(PlaceOrderCommand cmd) {
stockService.reserve(cmd.lines()); // same transaction
return orderRepository.save(Order.from(cmd));
}
}Check your understanding
Which layer owns the transaction?
Show answer
The service layer.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.