On 10/10/06, Craig L Russell <[EMAIL PROTECTED] > wrote:

I'd like to propose a solution to a problem that we have with usability in
the web tier. When using a servlet, each method that needs access to a
PersistenceManager needs to figure out where the current PersistenceManager
is, and if it is even active. There are many ways around this issue, but
they are not general. Among the workarounds are putting the active
PersistenceManager into the servlet context as a request or session
attribute, passing the PersistenceManager explicitly as a parameter, and
putting the PersistenceManager into a ThreadLocal field.

Of these workarounds, the one with the most appeal is the ThreadLocal
solution. So I'd like to propose that we formalize this by adding a method
to return a thread-safe PersistenceManager proxy associated with a
PersistenceManagerFactory that can be implemented as a singleton, stored in
a component's static field, and that dynamically binds to the
PersistenceManager that is currently active on the thread.


One issue that I can see with this is that servlet engines are nowhere
forced to use thread-per-request designs (unless that is something new in
JavaEE 5).  While this design is admittedly common, its days are few.  Take
a quick look at Greg Wilkins' writeup of Jetty 6's support for
'continuations' - http://docs.codehaus.org/display/JETTY/Continuations - to
get a feel for where application servers are headed, especially as so-called
'AJAX' becomes more common (and super-threaded CPU's rush to the scene).

The correct approach to acquiring a PM in web-components is to associate it
with the HttpServletRequest object.  This can be done easily in the
ServletRequestListener.requestInitialised() method as follows:

public class PMManager implements ServletRequestListener {

   public void requestInitialized(ServletRequestEvent e) {
       e.getRequest().setAttribute(PM_ATTR, getFreshPm());
   }

   public void requestDestroyed(ServletRequestEvent e) {
       PersistenceManager pm = acquire(e.getRequest());
       // close it, etc.
   }

   public static PersistenceManager acquire(HttpServletRequest req) {
       return req.getAtrribute(PM_ATTR);
   }

}

... notice how web-component code can simply:

   PMManager.acquire(req);

to get the PM.



That isn't to say that acquiring a thread-local PM proxy might not be a
useful feature.  I just wouldn't push it as a best-practice in
web-applications.



IMHO,
David Bullock.

Reply via email to