-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: No session or session close on lazy loading. Please help.
PostPosted: Thu May 24, 2007 2:20 pm 
Newbie

Joined: Fri Oct 27, 2006 2:28 pm
Posts: 14
I dot the following error even though I already created the open session in view.

The error message:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ... no session or session was closed

My spring configuration file has this segment:

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>openSessionInViewInterceptor</param-value>
</init-param>
</filter>

What happen is that in a jsp page, I retrieve an object, then in that object I get a collection (a member of the object). When I start to iterate over this collection that is when I get the error.

Looking into the exception stack trace, it's clearly that the open session in view filter is used:

at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:183)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)


Top
 Profile  
 
 Post subject: hibernateTemplate's alwaysUseNewSession was set to true
PostPosted: Thu May 24, 2007 3:20 pm 
Newbie

Joined: Fri Oct 27, 2006 2:28 pm
Posts: 14
I found the problem. Set the flag alwaysUseNewSession to false and it works.


Top
 Profile  
 
 Post subject: Problem with threading
PostPosted: Thu May 24, 2007 5:09 pm 
Newbie

Joined: Fri Oct 27, 2006 2:28 pm
Posts: 14
After I found the solution above, I found more problem.

This is something to do with a background thread. I try to solve this problem by doing this:
Code:
try {
     session = sessionFactory().openSession();
     tx = session.beginTransaction();

     //Read some object here.

     //Update the object.

     tx.commit();

} catch (Exception e) {
    if (tx != null) tx.rollback();
} finally {
     if (session != null) session.close();
}

The problem is that the read object use hibernateTemplate.find to locate the object, and a similar exception is shown. So, I change the code to:
Code:

     Criteria criteria = sessionFactory.getCurrentSession().createCriteria(SomeClass.class);
        criteria.add(Expression.eq("someField", fieldValue));

        List l = criteria.list();
        if (l.size() > 0) {
      return (SomeClass) l.get(0);
   }


This code, give me the following error:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

How do we deal with background thread here?

Thanks.
Code:


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.