Transactions are pivotal parts of a reliable enterprise application. The common belief is that transactions only relate to database work. The simplistic view of a transaction is begin transaction—SQL updates, deletes, and so on—followed by commit/rollback, but there's much more to transactions than this! In this chapter, we show you how to use declarative transactions rather than manually coded transactions, and how to span transactions across multiple transactional resources. Finally, we give you some hints on how to test transactional code.
Spring offers excellent support for declarative transactions, which means you do not need to clutter your code with transaction management code. All you have to do is declare that a method must participate in a transaction and that Spring will take care of handling the transaction management. To be more specific, in this chapter we look at the following:
Spring transaction abstraction layer: We discuss the base components of Spring transaction abstraction classes and explain how to use these classes to control the properties of the transactions.
Declarative transaction management: We show you how to use Spring to implement declarative transactional management using just plain Java objects. We offer examples for declarative transaction management using the configuration files as well as source level metadata.
Programmatic transaction management: Even though programmatic transactional management is not used very often, we explain how to use the PlatformTransactionManager interface, which gives you ultimate control over the transaction management code.
Transactions over multiple transactional resources: We also cover and give examples of how to use the JTATransactionManager, which allows you to include multiple transactional resources such as a database and message queue in a single transaction.