@Transactional is powerful… but using it incorrectly can break your data. Many developers simply put: @Transactional on every service method. It works… until it doesn’t. ⸻ ❌ Common Mistake Putting @Transactional everywhere: @Transactional public void createOrder() { saveOrder(); callExternalPaymentAPI(); } Problem: If the external API fails, your database transaction may behave unexpectedly. ⸻ 🧠 How Experts Use @Transactional Experts define clear transactional boundaries. Only wrap database operations: @Transactional public void createOrder() { saveOrder(); } Then call external services outside transaction. ⸻ ⚠️ Another Common Mistake Calling transactional method inside same class: public void process() { createOrder(); // @Transactional not applied } Spring proxy won’t apply transaction here. ⸻ ✅ Correct Approach Use separate service: orderService.createOrder(); Now transaction works correctly. ⸻ 💡 Lesson @Transactional is not just an annotation. It defines data consistency boundaries. Use it carefully. ⸻ Day 17 of becoming production-ready with Spring Boot. Question: Where do you usually put @Transactional? Service layer or repository? ⸻ #Java #SpringBoot #BackendEngineering #Transactions #SoftwareArchitecture
How u will handle it you want to call process payment external api after place save order ? In this situation how to use transaction
the self-invocation proxy bypass is the most common @Transactional bug. keeping external API calls outside the transaction boundary is critical advice. also REQUIRES_NEW for audit logging prevents audit failures from rolling back the main transaction