Companion code for the tutorial @Transactional Demystified: Every Attribute With Working Tests.
Every @Transactional attribute covered with working code and tests that prove the behavior.
propagation — all seven values, each demonstrated by calling OuterService into InnerService so Spring's proxy actually intercepts the call:
| Value | Behavior |
|---|---|
REQUIRED |
Joins the existing transaction or creates one |
REQUIRES_NEW |
Always creates a new transaction, suspends the caller's |
MANDATORY |
Throws if no transaction is active |
NEVER |
Throws if a transaction is active |
NOT_SUPPORTED |
Suspends the caller's transaction, runs without one |
NESTED |
Runs in a savepoint inside the current transaction |
SUPPORTS |
Joins if active, runs without one otherwise |
rollbackFor / noRollbackFor — the checked exception gotcha. By default, checked exceptions do not roll back. rollbackFor = Exception.class fixes that.
readOnly — disables Hibernate dirty checking on read-only paths.
timeout — kills the transaction if it runs past the deadline.
isolation — READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE each as a separate method.
Requires Docker (Testcontainers starts a PostgreSQL container automatically).
# Unit tests only
mvn test
# All tests including integration
mvn verifyAll 13 tests pass.
src/main/java/com/umurinan/transactional/
├── entity/Order.java
├── repository/OrderRepository.java
└── service/
├── InnerService.java -- propagation behaviors
├── OuterService.java -- callers that trigger each scenario
└── OrderService.java -- rollbackFor, readOnly, timeout, isolation
src/test/java/com/umurinan/transactional/
├── PropagationIT.java -- 7 tests, one per propagation value
├── RollbackIT.java -- 3 tests for rollbackFor / noRollbackFor
└── ReadOnlyAndTimeoutIT.java -- readOnly, timeout, isolation tests
- Java 21
- Spring Boot 4
- Spring Data JPA / Hibernate
- PostgreSQL (via Testcontainers)