Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Spring
Springmedium

How does `@Transactional` work (rollback + common gotcha)?

Tags
#transaction#transactional#rollback
Back to categoryPractice quiz

Answer

It runs the method inside a transaction; by default Spring rolls back on unchecked (`RuntimeException`) errors. A common gotcha is self-invocation: calling a `@Transactional` method from the same class bypasses the proxy, so the transaction may not start.

@Service
class PaymentService {
  @Transactional
  public void pay() {
    // DB writes here
  }
}

Advanced answer

Deep dive

In most Spring apps, `@Transactional` is implemented via an AOP **proxy**. The proxy starts a transaction before your method runs and commits/rolls back after it finishes.

Rollback rules (default)

Spring rolls back on:

  • `RuntimeException`
  • `Error`

Checked exceptions do **not** trigger rollback unless configured:

@Transactional(rollbackFor = Exception.class)

Why self-invocation is a problem

Only calls that go **through the proxy** get transactional behavior. `this.someTransactionalMethod()` stays inside the bean and bypasses the proxy.

Practical guidance

  • Place transaction boundaries on service-layer methods (use-case boundaries).
  • Don’t swallow exceptions if you want rollback.
  • Use propagation/isolation explicitly only when needed.

Common pitfalls

  • Catching exceptions and returning “success” (transaction commits).
  • `@Transactional` on private/final methods (proxy limitations depending on setup).
  • Returning lazy JPA entities outside the transaction (leads to `LazyInitializationException`).

Related questions

Spring
Transaction propagation in Spring — what does `REQUIRED` mean?
#transaction#propagation#spring
Spring
Spring AOP proxies — what is the self-invocation problem?
#aop#proxy#transactional
Spring
How does @Transactional work?
#transaction
#aop
#database
Monoliths
How do you introduce a breaking database change safely in a large monolith?
#db-migration#expand-contract#deployment
Databases
What is a transaction and why do we use it?
#transaction#acid#consistency