Results 1 to 6 of 6

Thread: managing Hibernate Sessions across HTTP requests

  1. #1
    Join Date
    Jul 2005
    Location
    Edinburgh
    Posts
    16

    Default 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. #2
    Join Date
    Jul 2005
    Posts
    20

    Default

    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.

  3. #3
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

    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.

  4. #4
    Join Date
    Apr 2005
    Posts
    19

    Default

    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

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    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.

  6. #6
    Join Date
    Jul 2005
    Location
    Edinburgh
    Posts
    16

    Default 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

Similar Threads

  1. Replies: 5
    Last Post: Dec 27th, 2005, 07:00 AM
  2. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  3. Replies: 1
    Last Post: Jul 5th, 2005, 03:48 AM
  4. Replies: 8
    Last Post: Jan 22nd, 2005, 04:31 AM
  5. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •