Chapter 14. Managing transactions
This chapter covers
- Transaction management overview
- Implementing transaction management aspect
- Implementing Spring’s transaction management
- Automatically retrying transactions
Consider the e-commerce example in appendix A. When you add an item to a cart, you remove it from the inventory. What would happen if the operation failed after you added an item to the cart but before it was removed from the inventory? The system would be in an inconsistent state, with the same item counted as being in the shopping cart and in the inventory. To prevent this undesirable situation, you need to execute both operations in a transaction. A transaction defines a unit of work that ensures that the system remains in a consistent state before and after its execution. If any operations within a transaction fail, then all of them fail, leaving the system as it was before the transaction started.
Transaction management has four properties: atomicity, consistency, isolation, and durability (ACID). I recommend Java Transaction Design Strategies by Mark Richards (Lulu.com, 2006; http://www.infoq.com/minibooks/JTDS) for more information. I also recommend “Distributed transactions in Spring, with and without XA,” by David Syer (http://www.javaworld.com/javaworld/jw-01-2009/jw-01-spring-transactions.html).