Java Reference
In-Depth Information
C H A P T E R 4
■ ■ ■
Transaction Management in Spring
In this chapter, you will learn about the basic concept of transactions and Spring's capabilities in the
area of transaction management. Transaction management is an essential technique in enterprise
applications to ensure data integrity and consistency. Spring, as an enterprise application framework,
provides an abstract layer on top of different transaction management APIs. As an application developer,
you can use Spring's transaction management facilities without having to know much about the
underlying transaction management APIs.
Like the bean-managed transaction (BMT) and container-managed transaction (CMT) approaches
in EJB, Spring supports both programmatic and declarative transaction management. The aim of
Spring's transaction support is to provide an alternative to EJB transactions by adding transaction
capabilities to POJOs.
Programmatic transaction management is achieved by embedding transaction management code
in your business methods to control the commit and rollback of transactions. You usually commit a
transaction if a method completes normally and roll back a transaction if a method throws certain types
of exceptions. With programmatic transaction management, you can define your own rules to commit
and roll back transactions.
However, when managing transactions programmatically, you have to include transaction
management code in each transactional operation. As a result, the boilerplate transaction code is
repeated in each of these operations. Moreover, it's hard for you to enable and disable transaction
management for different applications. If you have a solid understanding of AOP, you may already have
noticed that transaction management is a kind of crosscutting concern.
Declarative transaction management is preferable to programmatic transaction management in
most cases. It's achieved by separating transaction management code from your business methods
via declarations. Transaction management, as a kind of crosscutting concern, can be modularized
with the AOP approach. Spring supports declarative transaction management through the Spring AOP
framework. This can help you to enable transactions for your applications more easily and define a
consistent transaction policy. Declarative transaction management is less flexible than programmatic
transaction management. Programmatic transaction management allows you to control transactions
through your code_explicitly starting, committing, and joining them as you see fit. You can specify
a set of transaction attributes to define your transactions at a fine level of granularity. The transaction
attributes supported by Spring include the propagation behavior, isolation level, rollback rules,
transaction timeout, and whether or not the transaction is read-only. These attributes allow you to
further customize the behavior of your transactions.
Upon finishing this chapter, you will be able to apply different transaction management strategies
in your application. Moreover, you will be familiar with different transaction attributes to finely define
your transactions.
Search WWH ::

Custom Search