OpenSessionInViewFilter - LazyInitializationException
Hey,
Our app is using OpenSessionInViewFilter, HibernateTransactionManager, and HibernateDAOSupport. However, we still get LazyInitializationException when attempted to access a lazy collection. The exception is obvious, however, how do I enforced the session to be closed at the end of the request. From the logs, it is obvously not doing that.
Below are my sample configurations:
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="lobHandler">
<ref local="oracleLobHandler"/>
</property>
<property name="mappingResources">
<list>
<value>....../A.hbm.xml</value> <value>......../B.hbm.xml</value>
.................
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.O racle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_outer_join">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">tru e</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager" autowire="byName" depends-on="sessionFactory"/>
<bean id="serviceTarget" class="com.sample.service.ServiceImpl">
<property name="dao1"><ref bean="dao1"/></property>
<property name="dao2"><ref bean="dao2"/></property>
</bean>
<bean id="service" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref local="serviceTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</property>
</bean>
All daos extens HibernateDAOSupport.
In my action, the sample codes are as followed:
service.insert(obj1);
service.delete(ob2);
Sample s = dao1.loadSample(new Long(121));
log.warn("child size = " + s.getChild().size());
When use the 'service' object to insert/update/delete, session is alway open and close by the end of the operation.
after dao1.loadSample....................the session is closed at the end of the operation as well....
[09/20/2004 10:25:12] DEBUG org.springframework.orm.hibernate.SessionFactoryUt ils: Closing Hibernate session
[09/20/2004 10:25:12] DEBUG net.sf.hibernate.impl.SessionImpl: closing session
[09/20/2004 10:25:12] DEBUG net.sf.hibernate.impl.SessionImpl: disconnecting session
[09/20/2004 10:25:12] DEBUG net.sf.hibernate.impl.SessionImpl: transaction completion
And therefore, when attempting to access a.getChild().size.....i got a LazyInitializationException.
How do I configure spring to only close the session at the end of each request? Which I thought it's what OpenSessionInViewFilter is doing anyway. I even set flush mode of HibernateTempate to NEVER, hoping it would skip the close the method. But still no luck.
Am I doing something wrong in configurationt to cause that error??
Any help is greatly appreciated.
Thanks,