Java Reference
In-Depth Information
The sequence of events is as follows:
The web container begins the handling of a request by calling a servlet filter.
1
The servlet filter calls a persistence framework API to open a connection.
2
The servlet filter invokes the servlet such as the PlaceOrderService.
3
The servlet invokes the domain objects.
4
The servlet forwards the request to a JSP page.
5
The JSP page generates the response using the domain objects passed by
the servlet.
6
The servlet filter closes the connection.
7
Using the servlet filter to manage connections has a number of benefits. The serv-
let filter provides a robust way of managing connections because it uses a try/
finally block to ensure that the connection is closed. Also, it is reusable because
the same filter can be used by multiple applications. Finally, the servlet filter is
used declaratively by specifying the requests that it is applied to in the web appli-
cations deployment descriptor. The developer doesn't have to remember to write
code in order to use it.
You could implement the filter yourself, but it's a lot easier to use the filters
provided by the Spring framework. It provides an OpenSessionInViewFilter ,
which is a servlet filter that manages a Hibernate Session , and an OpenPersis-
tenceManagerInViewFilter , which manages a JDO PersistenceManager . Each filter
binds the connection object to the executing thread, which makes it available to
the HibernateTemplate and JdoTemplate classes used by the repositories.
The servlet filter is a generic way to manage persistence framework connec-
tions, which works in any servlet container. Some web application frameworks
have other ways of implementing the same mechanism. For example, Spring Web
MVC , which is a web application framework for developing presentation tiers, pro-
vides the OpenSessionInViewInterceptor , which is an AOP interceptor that wraps
Spring's web components. Even though they differ in the details, it is important to
remember that the goal is to keep the Session or PersistenceManager open while
the view components generate the response.
8.3 Managing transactions
In addition to managing the persistence framework connection, an application
must manage transactions in order to ensure atomic and consistent updates. An
EJB -based session façade would most likely use container-managed transactions,
 
 
 
 
 
Search WWH ::




Custom Search