Announcement Announcement Module
Collapse
No announcement yet.
managing Hibernate Sessions across HTTP requests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • managing Hibernate Sessions across HTTP requests

    Hi again,

    I would like to use lazy initialisation for my Hibernate objects (I'm not using Spring's Hibernate support at the moment but I don't think it solves the problem I'm about to explain in not too many words...). Problem is that for lazy initialisation to work, the Hibernate Session needs to remain open, yes? So, if I want to use, e.g., components with lazy initalisation in my JSPs then the Hibernate Session needs to be bound to the HttpSession (and synchronized on), yes? I have implemented a wrapper for Sessions that implements HttpSessionBindingListener and closes the Session when a user's HttpSession gets invalidated or expires. Is this the way to solve this problem, am I missing something???

    Cheers,

    Alex

  • #2
    I am not the expert, but I think you want something along the lines of the OpenSessionInViewInterceptor and OpenSessionInViewFilter provided by Spring for use with Hibernate.

    http://www.springframework.org/docs/...iewFilter.html
    http://www.springframework.org/docs/...terceptor.html

    But like I said, I'm not the expert... You might want a second opinion.

    Comment


    • #3
      OpenSessionInViewInterceptor/Filter keeps the Hibernate Session open for the duration of a single HTTP request. You don't need to muck about with session listeners, etc.

      You probably want to also check out EhCache or JBoss TreeCache for 2nd level caching -- which will cache data across HTTP requests. This is transparent to your application code.

      JBoss TreeCache is transactional and clustered, so good if you've got multiple server boxes.

      Comment


      • #4
        Hi there gmatthews,

        I included the following in my web.xml (Tomcat)

        Code:
            
        <!-- Hibernate Filters -->
            <filter>
                <filter-name>hibernateFilter</filter-name>
                <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
            </filter>
            <filter-mapping>
                <filter-name>hibernateFilter</filter-name>
                <url-pattern>*.htm</url-pattern>
            </filter-mapping>
            <filter-mapping>
                <filter-name>hibernateFilter</filter-name>
                <url-pattern>*.sql</url-pattern>
            </filter-mapping>
        to get rid of the lazy load exception.

        Hope it helps.

        Regards,
        Stefan

        Comment


        • #5
          avoss; The OSIV filters do not work on a session, they work on a HttpServletRequest.

          If you have hibernate objects stored in your HttpSession then they will not connected after the request that loaded them, thus, subsequent requests using hibernate objects from the HttpServletSession will thrown Exceptions when lazy loading.

          Basically, you are talking about binding the HibernateSession to the HttpServletSession. OSIV binds the HibernateSession to the HttpServletRequest.

          Comment


          • #6
            Re: managing Hibernate Sessions across HTTP requests

            Hi,

            thanks for your replies. I wondered a little while if I really needed to have sessions available across requests at all or if I was trying to do a pointless and silly thing here. (Otherwise, why would there not be a ready-made solution in Spring?) I guess that I was basically motivated by the Hibernate documentation saying things about wizards and at the same time saying things about potential problems with re-attaching an object to a session. The immediate problem I had was quite banal but I wanted to implement a solution that would be quite generic. So, I have implemented the wrapper object for Hibernate's Session that adds an implementation of HttpSessionBindingListener and so destroys the Session when the user's HttpSession is invalidated or expires. This seems to work fine so far.

            Can I assume that it is safe to keep the Sessions around in terms of resource usage? Not sure if there would be a problem with the things I'm developing at the moment which will only have a few dozen or so users but what about larger scale systems? Guess I should look at SessionImpl but I don't have the time at the moment, may investigate later.

            Cheers,

            Alex

            Comment

            Working...
            X