Skip to content

umur/spring-transactional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

spring-transactional

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.

What's covered

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.

isolationREAD_COMMITTED, REPEATABLE_READ, SERIALIZABLE each as a separate method.

Running the tests

Requires Docker (Testcontainers starts a PostgreSQL container automatically).

# Unit tests only
mvn test

# All tests including integration
mvn verify

All 13 tests pass.

Project structure

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

Stack

  • Java 21
  • Spring Boot 4
  • Spring Data JPA / Hibernate
  • PostgreSQL (via Testcontainers)

About

@transactional demystified - every attribute explained with working tests

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages