-->
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.  [ 2 posts ] 
Author Message
 Post subject: OSIV + LazyInitializationException:Session closed
PostPosted: Mon May 14, 2007 2:34 pm 
Newbie

Joined: Mon May 14, 2007 2:07 pm
Posts: 5
Hi there - please help me!

New to Hibernate and using in Spring container + Struts so the dependancies provide a steep learning curve.....

Getting session closed exceptions trying to us OSIV:

Want to use OSIV pattern to ensure that any Lazily loaded associated objects are available to my jsp's via Hibernate session before it's closed.

Have tried with the caveatemptor-native-061211 HibernateSessionRequestFilter with little luck - lots of LazyInitializationExceptions

Have also tried with Spring's OpenSessionInViewFilter helper filter.
This sets FlushMode to NEVER and, as I understand it, delegates or assumes to Tx boundaries will trigger management of FlushMode switching to allow save/updates etc.

We don't need Tx support yet so I've extended the Filter provided as such to a) set FlushMode.Auto on getSession() then b) flush and close session just before Http response is sent on closeSession():

Code:
public class HibernateOpenSessionInViewFilter extends OpenSessionInViewFilter {
   protected Session getSession(SessionFactory sessionFactory)
   throws DataAccessResourceFailureException {
   Session session = super.getSession(sessionFactory);
   session.setFlushMode(FlushMode.AUTO);
   return session;
   }

   protected void closeSession(Session session, SessionFactory sessionFactory) {
      session.flush();   
      SessionFactoryUtils.closeSession(session);
   }

Seems to work well but if I mark any of my mappings as as FetchType.LAZY I get LazyInitializationException when I try and navigate to the specified association. Specifically I am told that the session is closed. Not sure what is closing it.

We are using DAOs that use the HibernateTemplate to persist ie
Code:
getHibernateTemplate().saveOrUpdate(o);


If I switch to EAGER all seems well eg following causes problem:
Code:
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE}, fetch=FetchType.LAZY)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @JoinColumn(name="USER_ID",referencedColumnName="ID", nullable=false) //column on userprofile
   private Set<ProfileVO> getUserProfiles() {
      return userProfiles;
   }


Any idea what I might be doing wrong
ps I have the following hibernate property set

<prop key="current_session_context_class">thread</prop>

All the best and thanks in advance for any insite..




[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 11:19 pm 
Newbie

Joined: Thu May 03, 2007 6:24 pm
Posts: 14
Please check whether you are clearing(by calling clear()) the object before doing lazy initialization. If so, thats reason for the exeception.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.