14 · Hibernate and Spring Data JPA in depth / LESSON 70 OF 102
Propagation, isolation, and rollback rules
A transaction annotation is a policy applied at an intercepted service boundary.
Before this lesson: JPQL, native queries, projections, and specifications
Understand the topic
REQUIRED joins an existing transaction or starts one. REQUIRES_NEW uses an independent transaction and usually another connection while the outer one is suspended. Excessive nested independent transactions can exhaust a pool. readOnly is a hint and does not replace authorization.
Default declarative rollback covers unchecked exceptions and Errors; checked exceptions need an explicit rule when rollback is desired. Isolation controls visibility and concurrency anomalies subject to database behavior. Keep transactional work short and test outcomes from another transaction.
EXAMPLE · JAVA
Service boundary fragment
@org.springframework.transaction.annotation.Transactional(rollbackFor = java.io.IOException.class)
public void importTasks() throws java.io.IOException {
// Parse bounded input before this boundary when possible.
// Apply related database changes here.
// An IOException crossing the proxy now marks rollback.
}What to expect
This signature illustrates rollback policy, not a complete importer. Call it through an injected managed service and verify actual database state.
YOUR TURN
Put it into practice
- 01Test a checked and an unchecked failure after a database write.
- 02Add an explicit checked-exception rollback rule and compare REQUIRED with REQUIRES_NEW using separate service beans.
- 03Verify: Tests verify final committed rows and document whether an inner failure affects outer work.
Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.
UNDER THE HOOD
Build the mental model
A transaction annotation is a policy applied at an intercepted service boundary.
- 01REQUIRED joins an existing transaction or starts one. REQUIRES_NEW uses an independent transaction and usually another connection while the outer one is suspended. Excessive nested independent transactions can exhaust a pool. readOnly is a hint and does not replace authorization.
- 02Default declarative rollback covers unchecked exceptions and Errors; checked exceptions need an explicit rule when rollback is desired. Isolation controls visibility and concurrency anomalies subject to database behavior. Keep transactional work short and test outcomes from another transaction.
- 03Trace the example: This signature illustrates rollback policy, not a complete importer. Call it through an injected managed service and verify actual database state.
A mistake worth avoiding
A checked exception escapes but the earlier database write commits.
DEBUGGING ROOM
Something broke. Where do you look?
A checked exception escapes but the earlier database write commits.
Before revealing the diagnosis, write down your first investigation step.
CHECK YOUR UNDERSTANDING
Does readOnly=true enforce that a user is allowed to read a row?
BUILD IT YOURSELF
Test a checked and an unchecked failure after a database write.
- 1.Test a checked and an unchecked failure after a database write.
- 2.Add an explicit checked-exception rollback rule and compare REQUIRED with REQUIRES_NEW using separate service beans.
- 3.Reproduce this failure, then repair it: A checked exception escapes but the earlier database write commits.
Acceptance checklist
Lab completion is your self-report. Knowledge checks do not certify project quality.
RETRIEVE IT FROM MEMORY
Does readOnly=true enforce that a user is allowed to read a row?
Revisit this lesson in three days after a successful check or lab.
Ready for the next step?
Tests verify final committed rows and document whether an inner failure affects outer work.
Reading is separate from passing a knowledge check or completing a lab.