I'm trying to migrate my app to use a Session at the UI level, and to pass
this session down to my services and persistence layers.  In my Filter, I'm
basically calling a HibernateSession class which looks like the ThreadLocal
pattern from the design patterns on the wiki.  Below is my doFilter method.

The basic problem I'm experiencing can be seen from the following log snip:

DEBUG [Ajp13Processor[11009][4]] HibernateFilter.doFilter(86) | Closed
hibernate session.
DEBUG [Ajp13Processor[11009][3]] HibernateFilter.doFilter(62) | Opened
hibernate session.
DEBUG [Ajp13Processor[11009][3]] HibernateFilter.doFilter(62) | Opened
hibernate session.
DEBUG [Ajp13Processor[11009][3]] CMCFAction.save(117) | Entering 'save'
method
com.comcast.cable.dmc.itd.cct.persistence.DAOException:
cirrus.hibernate.HibernateException: Session
 is currently disconnected

Is the code below the best way to implement the UI Filter pattern?  It was
fairly easy for me to refactor, but I think I'm probably doing something
wrong.  Part of the refactoring process was to remove all the finally blocks
from my methods where I used to be calling HibernateSession.closeSession();
Should I be calling HibernateSession.closeSession() in the finally block of
this method.

Thanks for any advice,

Matt

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
      throws IOException, ServletException {
        Session ses = null;
        boolean sessionCreated = false;

        try {
            // Get the http session id from the request, then we will 
            // try to get  the Hiberate Session from the request.  If 
            // it doesn't exist, then we will create it, otherwise
            // we will use the one that already exists.
            ses = (Session) request.getAttribute(Constants.SESSION_KEY);

            if (ses == null) {
                ses = HibernateSession.currentSession();

                if (log.isDebugEnabled()) {
                    log.debug("Opened hibernate session.");
                }

                request.setAttribute(Constants.SESSION_KEY, ses);
                sessionCreated = true;
            }
        } catch (Exception exc) {
            log.error("Error opening Hibernate session.", exc);
                        exc.printStackTrace();
        }

        try {
            chain.doFilter(request, response);
        } finally {
            try {
                if (sessionCreated) {
                    if (ses != null) {
                        // Only try to close the connection if it is open, 
                        // since it might have been closed somewhere else 
                        // by mistake.
                        if (ses.isOpen()) {
                            ses.close();

                            if (log.isDebugEnabled()) {
                                log.debug("Closed hibernate session.");
                            }
                        }
                    }
                }
            } catch (Exception exc) {
                log.error("Error closing hibernate session.", exc);
                exc.printStackTrace();
            }
        }
    }



-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to