Announcement Announcement Module
Collapse
No announcement yet.
OpenSessionInView pattern, non-request-triggered data access Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInView pattern, non-request-triggered data access

    Hello everyone,

    I've developed a web application using Hibernate for data access. Everything is pretty straightforward - I am using the OpenSessionInView pattern (OpenSessionInViewFilter) in order to be able to lazy load data in the presentation layer. I've got a service layer (proxied using the TransactionProxyFactoryBean) and a data access layer (using HibernateDaoSupport/HibernateTemplate).

    My problem comes when data access is not triggered by a request but by some other event. This happens for example when a session times out. I'd like to write the time of the "automatic user logout" to the database.

    The servlet spec does not give any garuantees about when the HttpSessionListener's sessionDestroyed method is called. From my experience with Tomcat (5.0), this can happen in the middle of a request handler that calls session.getAttribute(), but sessionDestroyed is also called independently (apparantly by a background thread that checks for timeouts from time to time). I can't be sure that there is an open Hibernate Session at the time sessionDestroyed is called, so maybe I should aquire my own? I would need to access Springs LocalSessionFactoryBean to do so - is this possible from a HttpSessionListener?

    Does anyone know how to extend the OpenSessionInView pattern in order to safely handle cases like this? Will there be any problems regarding thread-safety or caches?

    Regards,

    Andreas

  • #2
    The HttpSessionListener gives you access to the HttpSession via the HttpSessionEvent object. Using the HttpSession, you can access the ServletContext.

    Using org.springframework.web.context.support.WebApplica tionContextUtils you can retrieve the context.

    I've just added a WebApplicationContextSessionListener to the sandbox (in org.springframework.web.servlet.support). Registering it in web.xml will result in all beans implementing interface HttpSessionListener defined in your webappctx being called (call is delegated to all of them). I'm not sure whether or not this will be up for addition since there are some issues with errorhandling here (the spec isn't entirely clear about this) when registering multiple listeners and stuff.

    I suggest you have a look at the class I just added instead of rolling your own. Delegating calls to the web app ctx has the advantage that you can make fully use of Spring's DI features.

    You won't have problems with Hibernate anymore if you're using this approach, since you will be going through the normal process where a session is created and such.

    Note, the listener I added is NOT tested... I've created a JIRA issue for others to have a look at it.

    Comment


    • #3
      Thanks for that useful class!

      The JIRA issue in question is:
      http://opensource.atlassian.com/proj...browse/SPR-437

      Comment

      Working...
      X