<code>
> try {
> HttpServletRequest httpRequest = (HttpServletRequest)
request;
> Object val = httpRequest.getAttribute(
INIT_REQ_ATTR_NAME );
> initialRequest = ( val == null );
> httpRequest.setAttribute( INIT_REQ_ATTR_NAME,
>INIT_REQ_ATTR_NAME );
> chain.doFilter(request, response);
> HibernateUtil.commitTransaction();
> }
> catch (RuntimeException ex) {
> log.debug("Rolling back the database transaction.");
> HibernateUtil.rollbackTransaction(); // Also closes the
session
> // Just rollback and let others handle the exception, e.g.
>for display
> throw ex;
> }
> finally {
> // No matter what happens, close the Session.
> if ( initialRequest ) {
> HibernateUtil.closeSession();
> }
> }
> }
> public void destroy() {}
>}
</code>
>For those using struts, you can filter on the action servlet and also
>jsp files at the same time and ensure that you will have only one
Session.
Now, here's one for the STRUTS guys out there, how do you catch
exceptions thrown from an Action class in the filter as STRUTS
captures it before it reaches the filter... thus making the rollback
unreachable?
Should you extend the RequestProcessor and take it from there?
Any ideas?
Regards
ace |