Перейти к содержимому

Why @Transactional Fails in Spring Boot

Learn Build & Break

0:00 / 0:00

Why @Transactional Fails in Spring Boot

442 просмотра · 1 мес. назад
Learn Build & Break
22 подписчика
442 просмотра · 1 мес. назад
Your @Transactional annotation might be doing NOTHING — and Spring Boot won't even warn you. 😱 In this video, we break down the #1 Spring Boot interview trap: why @Transactional silently fails, especially with self-invocation. You'll see the exact code that looks correct but never rolls back, understand WHY it happens (Spring proxies explained simply), and learn 3 ways to fix it properly. Key Scenarios for @Transactional Failure: Self-Invocation (2:08 - 4:22): Calling a transactional method from within the same class bypasses the proxy. Fixes: Move the method to a separate service bean or use self-injection. Non-Public Methods (4:23 - 5:02): Proxies only apply to `public` methods. Annotating `private` or `protected` methods results in the annotation being ignored. Checked Exceptions (5:03 - 6:18): By default, Spring only rolls back on unchecked exceptions (RuntimeExceptions and Errors). For checked exceptions, you must explicitly configure `rollbackFor = Exception.class`. Exception Swallowing (6:19 - 7:53): If you catch an exception inside a transactional method and do not rethrow it, the proxy assumes success. Fix: Either rethrow the exception or manually set the transaction status to rollback-only. Unmanaged Beans (7:54 - 8:52): Manually instantiating a class with the `new` keyword creates a plain object, not a Spring-managed bean, meaning no proxy is attached. Fix: Use dependency injection (`@Autowired`). 🔥 What you'll learn: Why @Transactional is a no-op on self-invocation (internal method calls) How Spring's proxy mechanism actually works behind the scenes Why private methods break @Transactional Checked exceptions & rollback — the hidden default nobody reads 3 clean fixes: self-injection, separate bean, and TransactionTemplate How to answer this in interview This is Video 1 of the Spring Boot Interview Traps series — concepts explained in Hinglish, code in English, zero fluff. 🔔 Subscribe to Learn Build & Break — backend, built and broken down. New videos every week: Java/Spring interview prep, AI tools for developers, and real build-alongs. #SpringBoot #Java #Transactional #JavaInterview #SpringBootInterview #BackendDevelopment #JavaDeveloper #SpringFramework #InterviewPreparation #Hinglish