not again: hibernate OpenSessionInViewInterceptor lazyinitialization exception

  • Thread starter comp.lang.java.programmer
  • Start date

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
 
Ad

Advertisements

T

Tom Anderson

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.

That someone is your debugger. Breakpoint Hibernate's close methods, and
anything likely-looking they call internally, and run your code. You'll
see if, and if so how, the session is being closed.

Also, don't use Hibernate-specific configuration, use the JPA API. The
Hibernate native interface is basically legacy.

Also also, you might have more luck with this on a Spring/Hibernate forum.
There are people here who know Spring and Hibernate on this group, but the
general centre of gravity is closer to EJB, and indeed J2SE.

tom
 
Ad

Advertisements

L

Lew

LazyInitializationException happens from attempted access to a
lazily-initializated item after disconnection from entity management. In
other words, as a result of programmer error.
That someone is your debugger. Breakpoint Hibernate's close methods, and
anything likely-looking they call internally, and run your code. You'll see
if, and if so how, the session is being closed.

I also recommend looking at your own code and seeing when you close the
session. Don't underrate the value of code examination and careful thought
thereupon. Knowing where your sessions are is /de rigueur/ in Hibernate
programming. There is no escape from the responsibility.
Also, don't use Hibernate-specific configuration, use the JPA API. The
Hibernate native interface is basically legacy.
+1

Also also, you might have more luck with this on a Spring/Hibernate forum.
There are people here who know Spring and Hibernate on this group, but the
general centre of gravity is closer to EJB, and indeed J2SE.

This forum is fine. I want to know what happens, assuming the OP figures out
how to post to the right thread, which for some reason he absolutely refuses
to do. The subject is on topic and relevant to a plurality of Java practitioners.

OP: Stop breaking the thread. Also, don't use TAB characters in Usenet code
posts. Follow the rules of the road, please. Don't risk a shunning.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top