Skip to content
Ameen Abozaid
JNR-031

Transactions

  • 8 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • ACID
  • transaction boundaries
  • @Transactional
  • rollback rules

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Explain ACID with concrete failure scenarios.
  2. Place @Transactional at the service layer around a business operation.
  3. Explain default rollback rules (unchecked vs checked) and configure them.
  4. Explain the proxy mechanism and diagnose self-invocation failures.
  5. Use read-only transactions and keep transactions short (no remote calls inside).

Lessons

  1. 01ACID by example
  2. 02Transaction boundaries
  3. 03@Transactional and proxies
  4. 04Rollback rules
  5. 05Propagation basics
  6. 06Long transactions and remote calls
  7. 07Demo: making checkout atomic
  8. 08Demo: finding the self-invocation bug

Lab and exercises

Lab

JNR-031-LAB1 · Place order

Outcomes: 1–5 · Time: ~3 h

  1. Implement OrderService.placeOrder that reserves stock and creates the order in one transaction.
  2. Write failure tests: out-of-stock on the second line and a forced exception after saving the order; assert no stock change and no order row remains.
  3. Add readOnly = true on the order query method.
  4. Fix the provided money-transfer exercise that has a planted self-invocation bug; add a test that fails first.
  5. Keep any payment call outside the transaction (a stub is enough).

Acceptance: failure tests prove atomicity.

Exercises (assessment: fix a broken transaction implementation and explain why it failed)

IDOutcomeTask
JNR-031-EX011Give one concrete failure for each of A, C, I, and D using Shop examples.
JNR-031-EX022Move @Transactional from the controller to the correct service method.
JNR-031-EX033A method throws a checked PaymentException and data is still committed. Fix it in two ways.
JNR-031-EX044Explain in your own words why this.method() skips the transaction, and give two fixes.
JNR-031-EX055Restructure placeOrder so the payment API is called outside the transaction.
JNR-031-EX063, 4Fix a broken transaction implementation (provided) and write the explanation.
Show solutions
  • EX01 A: crash after debit, before credit. C: stock would become negative. I: another request reads half-updated stock. D: committed order disappears after a restart without durability.
  • EX02 The service method placeOrder gets @Transactional; controllers stay free of it.
  • EX03 @Transactional(rollbackFor = PaymentException.class) or make the exception extend RuntimeException.
  • EX04 The proxy intercepts only calls from other beans; this is the raw object. Fix: move the method to another bean, or put the annotation on the public entry method.
  • EX05 Call payment first (or after commit) in a non-transactional coordinator method; the transactional part only reserves stock and saves the order.
  • EX06 Typical faults: annotation on a private method, a swallowed exception in a catch, or self-invocation; fix and describe which proxy rule was broken.
  • Outcome 1 is also covered by the quiz.

Quiz

Outcomes: O1 ACID · O2 boundaries at the service layer · O3 rollback rules · O4 proxies and self-invocation · O5 short and read-only transactions.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Atomicity means:O1
  2. 2Durability means committed data:O1
  3. 3@Transactional usually belongs on:O2
  4. 4The transaction boundary should match:O2
  5. 5By default Spring rolls back on:O3
  6. 6Catching an exception inside the method without rethrowing:O3
  7. 7Self-invocation fails because:O4
  8. 8A reliable fix for self-invocation:O4
  9. 9A payment API call inside a transaction is bad because:O5
  10. 10readOnly = true is meant for:O5

Practical part

Six exercises JNR-031-EX01 to EX06 plus the lab; the assessment is fixing a broken transaction implementation and explaining why it failed (EX06). Pass: the fix works, the explanation names the proxy rule, and the lab failure tests prove atomicity.

Common mistakes

@Transactional on private methods or controllers; calling a payment API inside a transaction; swallowing exceptions so nothing rolls back.

Assessment

Fix a broken transaction implementation and explain why it failed.

Career relevance

Junior implementation; isolation and locking in MID-015/016.

Type to search levels, phases, modules and lessons.