C
comp.lang.java.programmer
There are zillions of examples online of persons getting this error
I have been struggling with it now for a few hours and thought I would
share my pain.
I include the configuration and the logging below. I suspect the
session is being closed early and maybe someone might help me figure
out how that's happening. And how to avoid it.
My configuration
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>dto.XDto</value>
<value>dto.YDto</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.IngresDialect</
prop>
</props>
</property>
</bean>
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="daoTxTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="*">
PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED
</prop>
</props>
</property>
</bean>
<bean id="xDao" parent="daoTxTemplate">
<property name="target">
<bean class="dao.XDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</property>
</bean>
The DAO java looks like:
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(XDto.class);
List<XDto> list = criteria.list();
You will notice: I make no attempt to close the session or the
transaction.
The log output
2011-03-13 01:30:50,417 TRACE [org.hibernate.impl.SessionImpl] -
closing session
2011-03-13 01:30:50,417 TRACE [org.hibernate.jdbc.ConnectionManager] -
performing cleanup
2011-03-13 01:30:50,417 DEBUG [org.hibernate.jdbc.ConnectionManager] -
releasing JDBC connection [ (open PreparedStatements: 0, globally: 0)
(open ResultSets: 0, globally: 0)]
2011-03-13 01:30:50,418 TRACE [org.hibernate.jdbc.JDBCContext] - after
transaction completion
2011-03-13 01:30:50,418 DEBUG [org.hibernate.jdbc.ConnectionManager] -
transaction completed on session with on_close connection release
mode; be sure to close the session to release JDBC resources!
2011-03-13 01:30:50,418 TRACE [org.hibernate.impl.SessionImpl] - after
transaction completion
2011-03-13 01:30:50,421 DEBUG
[org.springframework.beans.factory.support.DefaultListableBeanFactory]
- Invoking afterPropertiesSet() on bean with name 'results/list'
2011-03-13 01:30:50,421 DEBUG
[org.springframework.web.servlet.view.InternalResourceViewResolver] -
Cached view [results/list]
2011-03-13 01:30:50,421 DEBUG
[org.springframework.web.servlet.DispatcherServlet] - Rendering view
[org.springframework.web.servlet.view.JstlView: name 'results/list';
URL [/WEB-INF/jsp/results/list.jsp]] in DispatcherServlet with name
'MY-servlet'
2011-03-13 01:30:50,424 DEBUG
[org.springframework.web.servlet.view.JstlView] - Rendering view with
name 'results/list' with model {
2011-03-13 01:30:50,426 DEBUG
[org.springframework.web.servlet.view.JstlView] - Added model object
'listResults' of type [java.util.ArrayList] to request in view with
name 'results/list'
2011-03-13 01:30:50,438 DEBUG
[org.springframework.web.servlet.view.JstlView] - Forwarding to
resource [/WEB-INF/jsp/results/list.jsp] in InternalResourceView
'results/list'
2011-03-13 01:30:50,489 ERROR
[org.hibernate.LazyInitializationException] - failed to lazily
initialize a collection of role: dto.RESULTS.matches, no session or
session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize
a collection of role: dto.XDto.matches, no session or session was
closed
I have been struggling with it now for a few hours and thought I would
share my pain.
I include the configuration and the logging below. I suspect the
session is being closed early and maybe someone might help me figure
out how that's happening. And how to avoid it.
My configuration
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>dto.XDto</value>
<value>dto.YDto</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.IngresDialect</
prop>
</props>
</property>
</bean>
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="daoTxTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="*">
PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED
</prop>
</props>
</property>
</bean>
<bean id="xDao" parent="daoTxTemplate">
<property name="target">
<bean class="dao.XDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</property>
</bean>
The DAO java looks like:
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(XDto.class);
List<XDto> list = criteria.list();
You will notice: I make no attempt to close the session or the
transaction.
The log output
2011-03-13 01:30:50,417 TRACE [org.hibernate.impl.SessionImpl] -
closing session
2011-03-13 01:30:50,417 TRACE [org.hibernate.jdbc.ConnectionManager] -
performing cleanup
2011-03-13 01:30:50,417 DEBUG [org.hibernate.jdbc.ConnectionManager] -
releasing JDBC connection [ (open PreparedStatements: 0, globally: 0)
(open ResultSets: 0, globally: 0)]
2011-03-13 01:30:50,418 TRACE [org.hibernate.jdbc.JDBCContext] - after
transaction completion
2011-03-13 01:30:50,418 DEBUG [org.hibernate.jdbc.ConnectionManager] -
transaction completed on session with on_close connection release
mode; be sure to close the session to release JDBC resources!
2011-03-13 01:30:50,418 TRACE [org.hibernate.impl.SessionImpl] - after
transaction completion
2011-03-13 01:30:50,421 DEBUG
[org.springframework.beans.factory.support.DefaultListableBeanFactory]
- Invoking afterPropertiesSet() on bean with name 'results/list'
2011-03-13 01:30:50,421 DEBUG
[org.springframework.web.servlet.view.InternalResourceViewResolver] -
Cached view [results/list]
2011-03-13 01:30:50,421 DEBUG
[org.springframework.web.servlet.DispatcherServlet] - Rendering view
[org.springframework.web.servlet.view.JstlView: name 'results/list';
URL [/WEB-INF/jsp/results/list.jsp]] in DispatcherServlet with name
'MY-servlet'
2011-03-13 01:30:50,424 DEBUG
[org.springframework.web.servlet.view.JstlView] - Rendering view with
name 'results/list' with model {
2011-03-13 01:30:50,426 DEBUG
[org.springframework.web.servlet.view.JstlView] - Added model object
'listResults' of type [java.util.ArrayList] to request in view with
name 'results/list'
2011-03-13 01:30:50,438 DEBUG
[org.springframework.web.servlet.view.JstlView] - Forwarding to
resource [/WEB-INF/jsp/results/list.jsp] in InternalResourceView
'results/list'
2011-03-13 01:30:50,489 ERROR
[org.hibernate.LazyInitializationException] - failed to lazily
initialize a collection of role: dto.RESULTS.matches, no session or
session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize
a collection of role: dto.XDto.matches, no session or session was
closed