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?