-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Rendering JSPs with Open Session in View pattern.
PostPosted: Fri Jun 10, 2005 5:48 pm 
Newbie

Joined: Fri Aug 13, 2004 11:41 am
Posts: 9
Hibernate version:

3.0.X

Servlet Filter

Code:
public class SessionManagerFilter implements Filter {
    /**
     * create a static reference to the logger.
     */
    private static final Logger logger = Logger.getLogger(SessionManagerFilter.class);

    private static final String FILTER_RUN = SessionManagerFilter.class.getName() + "FILTER_RUN";

    /**
     * Configures the filter.
     *
     * @param filterConfig the {@link javax.servlet.FilterConfig} to use.
     * @throws javax.servlet.ServletException {@link javax.servlet.Filter} related problems.
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.debug("init - start");
        logger.debug("init - end");
    }

    /**
     * Shuts down the filter.
     */
    public void destroy() {
        logger.debug("destroy - start");
        logger.debug("destroy - end");
    }

    /**
     * Executes the filter.
     *
     * @param servletRequest  the {@link ServletRequest} to use.
     * @param servletResponse the {@link ServletResponse} to use.
     * @param filterChain     the {@link FilterChain} to use.
     * @throws IOException      I/O related problems.
     * @throws ServletException {@link Servlet} related problems.
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {

        logger.debug("doFilter - start");

        try {
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            HttpServletResponse response = (HttpServletResponse) servletResponse;

            HttpSession session = request.getSession();

            Boolean filterRun = (Boolean) session.getAttribute(FILTER_RUN);

            if (filterRun != null && filterRun.booleanValue()) {
                filterChain.doFilter(request, response);

            } else {

                // do the rest of the filter chain
                session.setAttribute(FILTER_RUN, Boolean.TRUE);

                filterChain.doFilter(request, response);

                if (session != null && !session.isNew()) {
                    session.removeAttribute(FILTER_RUN);
                }

            }

        } catch (Exception e) {
            logger.fatal(e.getMessage(), e);
            throw new ServletException(e.getMessage(), e);

        } finally {
            try {
                HibernateHelper.closeSession();
            } catch (Exception e) {
                throw new ServletException(e.getMessage(), e);
            } finally {
                logger.debug("doFilter - end");
            }
        }
    }
}



Full stack trace of any exception that occurs:

[ERROR] [10 Jun 2005 16:42:44.468] [LazyInitializationException] [could not initialize proxy - the owning Session was closed]
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)

Name and version of the database you are using:

MySQL 4.1.X

The generated SQL (show_sql=true):

Problem:

When using the http://www.hibernate.org/43.html - Open Session in View pattern, is there a way to get around having not having an session open on the JSP?

When I attempt to modify objects in my JSP I am getting a session is disconnected exception.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 11, 2005 4:13 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Use integer "FILTER_RUN" counter to calculate "doFilter" stack depth and put "FILTER_RUN" to request/threadlocal not to a session attribute.
Call " HibernateHelper.closeSession()" if and only if this counter becomes zero.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 5:49 pm 
Newbie

Joined: Fri Aug 13, 2004 11:41 am
Posts: 9
baliukas wrote:
Use integer "FILTER_RUN" counter to calculate "doFilter" stack depth and put "FILTER_RUN" to request/threadlocal not to a session attribute.
Call " HibernateHelper.closeSession()" if and only if this counter becomes zero.


Is there an example anywhere in the documentation on reference counting with a servlet filter?

I've looked a bunch on google but have yet to find anything useful in terms of how to implement this approach.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 11:12 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Check out the org.springframework.orm.hibernate3.support.OpenSessionInViewFilter and the org.springframework.web.filter.OncePerRequestFilter it inherits from.

http://www.springframework.org


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.