Declarative — @Transactional annotation handles transaction boundaries. Programmatic — explicit transaction management in code.
1// Declarative2@Service3public class OrderService {4 @Transactional5 public void createOrder(Order order) {6 orderRepo.save(order);7 inventoryService.reserve(order);8 }9}1011// Programmatic12@Service13public class OrderService {14 @Autowired15 private TransactionTemplate txTemplate;1617 public void createOrder(Order order) {18 txTemplate.execute(status -> {19 orderRepo.save(order);20 inventoryService.reserve(order);21 return order;22 });23 }24}
@Transactional attributes:
When to use programmatic: