Java Reference
In-Depth Information
A value of
PROPAGATION_REQUIRED
indicates this method must be executed in a
transaction. The
TransactionInterceptor
will start a transaction if one is not
already in progress.
The
-net.chrisrichardson.bankingExample.facade.MoneyTransferException
entry is an example of a Spring rollback rule and tells the
TransactionInterceptor
to roll back the transaction if the
MoneyTransferException
is thrown. By default,
Spring behaves like
EJB
and only rolls back a transaction if an unchecked excep-
tion is thrown, but you can override this behavior by configuring rollback rules.
This example uses a rollback rule that tells the
TransactionInterceptor
to roll
back when a checked exception is thrown, but you can also write rollback rules that
commit transactions when unchecked exceptions are thrown. An important bene-
fit of rollback rules is that they enable the application to roll back a transaction
without calling a Spring
API
. This is yet another example of how Spring does not
intrude on your application's code.
The transaction attributes for the
getBalance()
method, which matches the
get*
wildcard, are
PROPAGATION_SUPPORTS, readOnly
The
PROPAGATION_SUPPORTS
value indicates that this method can be executed in a
transaction but does not require one. The
readOnly
value indicates that this
method does not update the database, which allows some database systems, to
optimize the transaction.
In addition to specifying the transaction attributes of each method, you must
specify the
PlatformTransactionManager
used by the
TransactionInterceptor
.
The
PlatformTransactionManager
is a Strategy (as in the Strategy pattern) that is
used by the
TransactionInterceptor
to begin, commit, and roll back transac-
tions. Figure 7.2 shows some of the different implementations of
PlatformTrans-
actionManager
.
The
PlatformTransactionManager
interface specifies three methods:
get-
Transaction()
, which begins a transaction;
commit()
, which commits a transaction;
and
rollback()
, which roll backs a transaction. Which kind of
Platform-
TransactionManager
you use depends on whether you are using local transactions
or
JTA
transactions. All of the examples in this topic use local transactions but can
easily be enhanced to use
JTA
transactions by simply reconfiguring the
Trans-
actionInterceptor
.
Search WWH ::
Custom Search