Java Reference
In-Depth Information
makes retrying a transaction difficult. To work around this problem, you will need
to write extra presentation-tier code.
An even trickier problem to solve is undoing changes made to the
HttpSession
when the transaction is rolled back. For example, after calling
PlaceOrderSer-
vice.updateDeliveryInfo()
for the first time, the
UpdateDeliveryServlet
will
store the
ID
of the newly created
PendingOrder
in the
HttpSession
. If the transac-
tion is rolled back, the
HttpSession
will contain the
ID
of a nonexistent order,
which will then be passed to the
PlaceOrderService
when the transaction is
retried. You will have to write yet more code in either the presentation tier or busi-
ness tier to solve these kinds of problems.
It's certainly possible to work around these problems by writing extra code. But
can you be confident that it works? Rollbacks happen relatively infrequently and
writing tests for the rollback scenarios can be difficult, so there is a pretty good
chance that bugs will lurk in the code.
Benefits and drawbacks of managing transactions in the presentation tier
Using a servlet filter to manage transactions has these benefits:
Enables the presentation tier to have a consistent view of the database
—The servlets
and
JSP
pages execute with a single transaction, which can enable them to
have a consistent view of the database.
■
Supports both JTA and local transactions
—An application can use either local
transactions or
JTA
transactions by configuring filters appropriately.
■
It has these drawbacks:
Overhead of buffering the response
—In order to be able to roll back the transac-
tion, the output of the
JSP
pages must be buffered until the transaction
ends, which increases the application's memory usage.
■
Complexity of writing presentation tier code that supports transaction retries
—It can
be difficult to develop and test presentation-tier code that supports transac-
tion rollbacks and retries.
■
Because of these problems, my preference is to manage transactions in the busi-
ness tier. However, this approach also has its drawbacks, particularly in a Hiber-
nate application.
8.3.2
Managing transactions in the business tier
For many applications, a much better approach is to manage transactions in the
business tier by using a Spring
TransactionInterceptor
around the domain
Search WWH ::
Custom Search