Complete free practice · 7 minutes
Spring transaction rollback: a free debugging drill
A transaction annotation is present, yet a failed import leaves rows behind.
Inspect the supplied evidence, write your hypothesis, then compare your choices with the reasoning. This educational simulation is free without an account or payment. Read the exercise here, or use the interactive lab to record your decisions.
The incident brief
A batch import inserts an invoice and throws a runtime exception. The invoice remains. The import entry point has no transaction; repository save methods use their usual transactions. The application uses Spring proxy mode, not AspectJ weaving.
Environment and evidence
Spring Framework 6.2 · proxy-based transactions · Spring Data JPA
class ImportService {
public void importBatch() { this.saveOne(); }
@Transactional
public void saveOne() {
repo.save(invoice);
throw new IllegalStateException("invalid line");
}
}
Call path: controller → proxy.importBatch() → this.saveOne()Decision 1: Diagnose
Which detail is the strongest lead?
- IllegalStateException is a checked exception.
- Hibernate never rolls back inserts.
- The annotated method is reached through self-invocation.
Reveal the reasoning after choosing
Option A: It is a runtime exception. With a correctly intercepted transaction it normally triggers rollback.
Option B: Inserts can roll back inside a database transaction.
Option C: The call stays inside the target object and bypasses transactional interception for saveOne in proxy mode.
Decision 2: Stabilize
How do you establish a reliable transaction boundary?
- Extract saveOne to a separate injected service and call it through its proxy.
- Add rollbackFor to the existing self-invoked method.
- Annotate importBatch, accepting a transaction around the whole batch.
Reveal the reasoning after choosing
Option A: An external call to the managed collaborator allows its transactional advice to run. Choose per-item or whole-batch atomicity deliberately.
Option B: Changing rollback rules does not repair the missing interception.
Option C: This can establish a boundary, but it changes the atomicity and transaction duration for every item. That trade-off must be intended.
Decision 3: Prevent
Which test would catch this regression?
- Construct ImportService with new and mock the repository.
- Call the proxied entry point in an integration test, force failure, then query outside any test transaction.
- Verify the annotation is present using reflection.
Reveal the reasoning after choosing
Option A: A plain instance has no Spring proxy or actual database rollback to test.
Option B: This checks the real path and committed state. A test-wide transaction can hide the defect.
Option C: Presence does not establish that the call was intercepted.
Read the debrief after all three decisions
The mechanism
Self-invocation bypasses the annotation on the inner method in Spring proxy mode.
Verify the fix
- Test one successful and one failed item through the application context.
- Check whether the intended atomicity is per invoice or per batch.
- Assert no partial database state after the chosen failure.
Explain it in your own words
I would trace the actual bean call path. The controller reaches a proxy, but the inner call uses this and has no transactional interception of its own. Repository-level transactions explain the retained row. I would introduce an intentional service boundary and test committed database state after failure, without a surrounding test transaction masking the result.
Continue with the same skill
Read the related free guide → · Choose a structured study path →
Is this practice format useful to you?
The full edition contains 1008 exercises and 3024 decisions across 18 topics. Practice testing, SQL, APIs, Java 8, Java 21, design patterns, system design, cybersecurity and cloud. Related exercises revisit mechanisms through different decision lenses. You receive authored reviews and five study paths; this is a decision workbook, not a code execution service.
Explore everything included for $28 →One-time payment. No subscription. Review the purchase and refund terms before buying.
Primary documentation
Original educational examples, prepared with AI assistance. No employer question, customer incident or hiring outcome is claimed. Editorial approach.