I'm using Hibernate 2.1 with Tapestry 3.0.1. I've had reasonably good success if I avoid lazy collection initialization, but I have struggled to find a reliable method of dealing with lazy initialization.
I started with the recommended ThreadLocal session implementation (ie - the HibernateUtil class), which worked in some (simple) cases. I repeatedly found recommendations to implement Spring's OpenSessionInViewFilter, so I have spent the last few days ramping up on Spring. The lazy issues I had are now improved, but I'm still running into certain cases where it crops up. Since I'm using Tapestry, I will try to illustrate a simple scenario (I can give more details if needed):
Load an Incident, which has a lazy one-to-many with IncidentLine.
setIncident(incident) in a view page.
Option is selected to edit the incident.
EditIncident page "editPage" is instantiated.
editPage.setIncident(incident)
cycle.activate(editPage) to activate the EditIncident page.
From pageBeginRender() of my EditIncident page:
Incident incident = getIncident();
incident.getIncidentLineList() fails
I get Failed to lazily initialize a collection - no session or session was closed. I've confirmed in my logs that a session is open (managed by Spring), but I think my problem is that my Incident instance has not been reassociated with the open session.
If I walk through the above steps in a debugger, everything works fine since my debugger initially touched and forced the loading of the IncidentLines.
The sequence above is typical for Tapestry-based applications, and I really want to use Hibernate, but I am stuck for a general-purpose solution. I can provide more details of my Hibernate and Spring configuration if that would be helpful.
Here are a few log entries where the exception is thrown:
Code:
DEBUG 2005-02-24 11:11:37,258 [SocketListener0-0] net.sf.hibernate.transaction.JDBCTransaction - commit
DEBUG 2005-02-24 11:11:37,258 [SocketListener0-0] net.sf.hibernate.impl.SessionImpl - transaction completion
DEBUG 2005-02-24 11:11:37,258 [SocketListener0-0] net.sf.hibernate.transaction.JDBCTransaction - re-enabling autocommit
DEBUG 2005-02-24 11:11:37,258 [SocketListener0-0] org.springframework.orm.hibernate.HibernateTransactionManager - Triggering afterCompletion synchronization
DEBUG 2005-02-24 11:11:37,258 [SocketListener0-0] org.springframework.transaction.support.TransactionSynchronizationManager - Clearing transaction synchronization
DEBUG 2005-02-24 11:11:37,258 [SocketListener0-0] org.springframework.transaction.support.TransactionSynchronizationManager - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@1071537] for key [com.mchange.v2.c3p0.ComboPooledDataSource@7103af] from thread [SocketListener0-0]
DEBUG 2005-02-24 11:11:37,258 [SocketListener0-0] org.springframework.jdbc.datasource.DataSourceUtils - Resetting read-only flag of connection [com.mchange.v2.c3p0.impl.NewProxyConnection@6a086a]
DEBUG 2005-02-24 11:11:37,258 [SocketListener0-0] org.springframework.orm.hibernate.HibernateTransactionManager - Not closing pre-bound Hibernate session [net.sf.hibernate.impl.SessionImpl@24c3aa] after transaction
ERROR 2005-02-24 11:11:37,258 [SocketListener0-0] net.sf.hibernate.LazyInitializationException - Failed to lazily initialize a collection - no session or session was closed
Can anyone see anything unusual about these messages?
I would tremendously appreciate any pointers.