• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another LazyInitializationException: illegal access to loading collection

 
Deena Jeyachandran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I get the below exception trace when trying to iterate through the Collection retrieve through Hibernate one-to-many mapping

Exception:
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java(Compiled Code))
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java(Inlined Compiled Code))
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java(Compiled Code))

This is a intermittent problem and goes away after every server restart.

We are trying at least to find out the cause of the issue. Could you please give some suggestions of what could go wrong.

Here is what I have already checked.
Checked the AbstractPersistentCollection class source the reason for error.



From this I took that initializing variable is set to true before starting to read a row in the resultset/Collection and set to false after initialized.

So, there should have been a different thread that was accessing the collection at the same time or initializing was left at true even after initialization was complete.

I have googled my best, but could not find a reason.





JDK1.4, Websphere 6.0, Hibernate3

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying to use a lazy collection outside of a session. Either make it non-lazy, or load the collection inside the original session, or re-attach the objects and load it when you need it, or use something like an open session in view filter, or... which makes the most sense depends on your specific needs, application, etc.
 
Deena Jeyachandran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

Thanks for your reply.
I am confident, that the collection is accessed within the hibernate session. In our application the session is valid for an entire transaction. the code snippet I added was only one method in the transaction.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, *something* is attempting to be initialized lazily outside of a valid session for the object in question--that's the only reason you'd get that exception.
 
Ran Pleasant
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deena Jeyachandran wrote:Hi David,

Thanks for your reply.
I am confident, that the collection is accessed within the hibernate session. In our application the session is valid for an entire transaction. the code snippet I added was only one method in the transaction.


Deena

As David noted, *something* is clearly wrong. Please note that once you are outside of the session transaction you can never make a call to your Parent.getChildren() method. The Parent.class file keeps a reference to the session itself and when you call the getChildren() method it attempts to access the session to retrieve the children objects. Even if you successfully call Parent.getChildren() within the context of the session the Parent object will throw an exception if you call Parent.getChildren() in say a servlet. In other words, you Parent class is not storing a set of children, it is storing a reference to the session so that the session can return the set when the getChildren() method is called. Either turn Lazy off or within the context of the session put the data from Parent and the children into a data transfer object.

I also suggest just spending some thing thinking about what Lazy is really giving you. If you're dealing with a parent with say 5 children objects then just set Lazy to false, set fetch to join", and move forward.


 
John Tobey
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deena Jeyachandran wrote:Hi Guys,

This is incorrect use of synchronized{}. The object on which you synchronize must be the same in all threads, so using the result of a method call and comparing it to null raise red flags. Instead, define:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic