Java Reference
In-Depth Information
The sequence of events is as follows:
The web container invokes the connection filter (
OpenSessionInViewFil-
ter
or
OpenPersistenceManagerInViewFilter
).
1
The connection filter, which manages connections, calls the persistence
framework to open the connection.
2
The connection filter calls the transaction management filter.
3
The transaction management filter begins a transaction.
4
The transaction management filter calls the servlet.
5
The servlet invokes the
PlaceOrderService
.
6
The
PlaceOrderService
is invoked and updates the pending order.
7
The servlet forwards the request to the
JSP
page.
8
The
JSP
uses the domain objects such as the
PendingOrder
to generate the
response.
9
The transaction management filter commits a transaction.
10
The connection filter calls the persistence framework to close the connec-
tion.
11
This design is extremely simple. The filters manage connections and ensure that
the servlet and
JSP
pages execute within a transaction. In addition, filters can be
used with both local transactions and
JTA
transactions. But one problem with
managing transactions in the presentation tier is that handling transaction roll-
backs and retries is quite tricky.
Handling transaction retries
In chapter 12, we will see that an application must sometimes roll back and retry a
transaction if, for example, a deadlock or an optimistic locking error occurs. The
problem with trying to roll back a transaction in the presentation tier is that it is
not always possible to undo the side effects of the servlets and
JSP
pages that were
executed as part of the transaction. For example, to roll back a transaction the
presentation tier must reset the
HttpServletResponse
and clear any output gener-
ated by any
JSP
pages. To guarantee that this can be done, the application must
buffer the output of the
JSP
pages to ensure that none of it is sent back to the
browser, which increases the application's memory usage.
You must also solve the problem of servlet
API
s that can only be called once.
For example, the input stream of a
ServletRequest
can be read only once, which
Search WWH ::
Custom Search