-->
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.  [ 4 posts ] 
Author Message
 Post subject: LazyInitializationException. Session is open.
PostPosted: Sun Feb 12, 2006 9:15 pm 
Newbie

Joined: Sun Feb 12, 2006 8:16 pm
Posts: 6
I've got a one-to-many relationship and every time I try to access the collection I get "org.hibernate.LazyInitializationException failed to lazily initialize a collection of role: ... no session or session was closed". The trick is that session IS open. I use ServletFilter to handle session management and have explicitly checked the state of the session anyway.

Here is the piece of code which throws the exception:

Code:
public void addNomination(Nomination nomination) {
Session session = Init.getSessionFactory().getCurrentSession();
logger.debug("Session connected: " + session.isConnected()); //prints true
logger.debug("Session open: " + session.isOpen()); // prints true
logger.debug("Object initialized: " + Hibernate.isInitialized(this.nominations)); // prints false
//Hibernate.initialize(this.nominations); // throws the "session disconnected" exception if left uncommented
nomination.setCycle(this);
this.getNominations().add(nomination); // throws the LazyInitializationException  exception
}



Hibernate version:
Hibernate 3.1

Mapping documents:
<class name="Cycle" table="cycle">
<cache usage="read-write"/>
<id name="id">
<generator class="native"/>
</id>
<set name="nominations" table="nomination" cascade="all-delete-orphan" inverse="true" lazy="true">
<cache usage="read-write"/>
<key column="cycle_id" not-null="true"/>
<one-to-many class="Nomination"/>
</set>
</class>

<class name="Nomination" table="nomination" lazy="true">
<cache usage="read-write"/>
<id name="id"><generator class="native"/></id>
<many-to-one name="cycle" column="cycle_id" cascade="none" not-null="true"></many-to-one>
</class>


Full stack trace of any exception that occurs:
[13/02/06 12:04:11:152 EST] 2f03744f LazyInitializ E org.hibernate.LazyInitializationException failed to lazily initialize a collection of role: Cycle.nominations, no session or session was closed

[13/02/06 12:04:11:167 EST] 2f03744f LazyInitializ E org.hibernate.LazyInitializationException TRAS0014I: The following exception was logged org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Cycle.nominations, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:183)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:165)
at Cycle.addNomination(Cycle.java:94)
...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 12, 2006 11:10 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It looks like the session that loaded this.nominations isn't the session that's being returned from Init.getSessionFactory().getCurrentSession(). Can you check that? If you find that that's the case, find out where that session is being closed, and don't close it. Alternatively, you could put this.nominations in the current session, using Init.getSessionFactory().getCurrentSession().lock(this.nominations, LockMode.NONE);


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 12, 2006 11:29 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Also, check the value of
hibernate.transaction.auto_close_session
since Hibernate 3.1 introduces Contextual Sessions and Session closing depends on what type of contextual session has been chosen for the application. Refer to section 2.5 of the Reference manual.
If you dont specify any for the context_class, jta is taken as the default and if you had closen your transaction anytime after you retrieved the first set of results, the session would have gotten closed as the JTA Transaction is closed.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 7:34 pm 
Newbie

Joined: Sun Feb 12, 2006 8:16 pm
Posts: 6
tenwit wrote:
It looks like the session that loaded this.nominations isn't the session that's being returned from Init.getSessionFactory().getCurrentSession(). Can you check that? If you find that that's the case, find out where that session is being closed, and don't close it. Alternatively, you could put this.nominations in the current session, using Init.getSessionFactory().getCurrentSession().lock(this.nominations, LockMode.NONE);


Yes! Stupid, I was "caching" reference to a Cycle object in a static variable. Of course it would be the different session.

Thank you!


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