Chapter 13: Using transactions in Spring apps
This introduction explains why transactions are needed to keep related data changes consistent when one step fails.
After this chapter you will be able to
- Transaction atomicity and consistency. Explain that a transaction treats several data-changing steps as one logical operation: either all succeed or none take effect. Use the wallet transfer to illustrate why a partial change would leave data inconsistent.
- Transaction atomicity and consistency. Explain why the withdrawal and deposit in a money transfer must succeed together or not at all, and how a transaction prevents inconsistent data if one step fails.
- Commit and rollback outcomes. Describe when the example transaction commits and when it rolls back, including what happens to the data changes in each case.
- Transactional method interception. Explain that @Transactional marks a method for Spring to wrap in a transaction, using a Spring-provided AOP aspect that intercepts the method and applies transaction logic.
- Runtime exception rollback. Explain why a runtime exception must be thrown out of the transactional method for the aspect to know to roll back the transaction.
- Checked exception rollback defaults. Distinguish Spring’s default treatment of checked exceptions from runtime exceptions, and describe how @Transactional can change the rollback behavior when needed.
- Money transfer transaction boundary. Explain how the transfer service reads two accounts, subtracts money from the source, and adds it to the destination, and why both updates need one transaction.
- Transactional annotation and aspect. Describe how marking the transfer method with @Transactional causes Spring to wrap its execution in a transaction and what happens on success or a runtime exception.
- Successful transfer and rollback test. Describe how the example checks both a successful transfer and a forced runtime failure by comparing account balances before and after calling the endpoints.
What it builds on
- How aspects work in Spring (chapter 6) · The idea that an aspect intercepts method execution.
- Using JdbcTemplate to work with persisted data (chapter 12) · Using JdbcTemplate to read and update persisted data
Section by section
Introduction
This introduction explains why transactions are needed to keep related data changes consistent when one step fails.
- Transaction atomicity and consistency: Explain that a transaction treats several data-changing steps as one logical operation: either all succeed or none take effect. Use the wallet transfer to illustrate why a partial change would leave data inconsistent.
Why it matters: It explains why a technically running application can still produce incorrect data after a partial failure.
Key terms: transaction, atomicity, data inconsistency
13.1 Transactions
This section uses a money transfer to explain why transactions group related data changes and what commit and rollback do.
- Transaction atomicity and consistency: Explain why the withdrawal and deposit in a money transfer must succeed together or not at all, and how a transaction prevents inconsistent data if one step fails.
Why it matters: Atomicity explains why a failed transfer must not leave money withdrawn but undelivered. - Commit and rollback outcomes: Describe when the example transaction commits and when it rolls back, including what happens to the data changes in each case.
Why it matters: Commit and rollback determine whether the transfer’s changes become permanent or are undone.
Key terms: transaction, mutable operations, atomicity, data consistency, commit, rollback
13.2 How transactions work in Spring
This section explains how Spring wraps @Transactional methods with an aspect and how exceptions affect transaction rollback.
- Transactional method interception: Explain that @Transactional marks a method for Spring to wrap in a transaction, using a Spring-provided AOP aspect that intercepts the method and applies transaction logic.
Why it matters: It explains what Spring does when you mark a method as transactional. - Runtime exception rollback: Explain why a runtime exception must be thrown out of the transactional method for the aspect to know to roll back the transaction.
Why it matters: Catching an exception inside the method can change the rollback outcome. - Checked exception rollback defaults: Distinguish Spring’s default treatment of checked exceptions from runtime exceptions, and describe how @Transactional can change the rollback behavior when needed.
Why it matters: It prevents assuming that every exception thrown from a transactional method has the same rollback effect.
Key terms: @Transactional, Spring AOP aspect, transaction logic, runtime exception, rollback, transactional method, aspect, checked exceptions, runtime exceptions
13.3 Using transactions in Spring apps
This section implements a transactional money transfer in Spring and tests that successful transfers persist while runtime exceptions roll back both account updates.
- Money transfer transaction boundary: Explain how the transfer service reads two accounts, subtracts money from the source, and adds it to the destination, and why both updates need one transaction.
Why it matters: A transfer must not leave the two account balances out of sync if a step fails. - Transactional annotation and aspect: Describe how marking the transfer method with @Transactional causes Spring to wrap its execution in a transaction and what happens on success or a runtime exception.
Why it matters: The annotation identifies the method whose database changes Spring should handle together. - Class and method annotation scope: Distinguish applying @Transactional to a service class from applying it to an individual method, including which configuration takes precedence.
Why it matters: Class-level annotation can cover service use cases without repeating the annotation on every method. - Successful transfer and rollback test: Describe how the example checks both a successful transfer and a forced runtime failure by comparing account balances before and after calling the endpoints.
Why it matters: Checking balances after a forced failure demonstrates rollback rather than merely assuming the annotation works.
Key terms: transferMoney(), TransferService, AccountRepository, transaction, @Transactional, aspect, commit, roll back, runtime exception, class
13.4 Questions
This section poses review questions about transactions and Spring transaction management, with answers in Appendix A.
13.5 Summary
This section summarizes why transactions protect data consistency and how Spring applies them with @Transactional.
Where this is used later
- Using Spring Data JDBC (chapter 14) · using @Transactional in a Spring application
- Writing correctly implemented tests (chapter 15) · The money-transfer use case used as the section’s testing example.
Concepts explained in this chapter
- database transactionA transaction is a unit of data processing whose changes are managed together so that either all required changes succeed or none are applied.
- Transactional annotationThe Spring @Transactional annotation declares that a method or class should execute within a managed transaction.
- interceptorAn aspect is a modular unit in aspect-oriented programming that intercepts selected program operations and applies cross-cutting behavior.
- atomicityAtomicity is the transaction property that requires all operations in a transaction to succeed together or for none of their changes to take effect.
- data inconsistencyA data inconsistency is a condition in which related stored or exchanged data does not agree or satisfy the application's consistency rules.
- rollbackA rollback is the operation that undoes a transaction's changes after the transaction cannot complete successfully.
- data transfer objectA data transfer object is an object that carries structured data between software applications or application boundaries.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
