-->
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: "illegal access to loading collection" in multi-th
PostPosted: Wed Jul 25, 2007 11:02 pm 
Newbie

Joined: Wed Jul 25, 2007 9:07 pm
Posts: 2
I'm using Hibernate 3.2 cr2, EntityManager and Annotations 3.2.0.CR1 with J2SE 5, to check the behaviour of the Java Persistence API using both TopLink Essentials and Hibernate as persistence providers, in J2SE as its easier to drive the code. The database is MySQL Community version 5.

I have a test of multi-threaded reads, where there are two or more threads, each with its own EntityManager created from an initial thread which does the EntityManagerFactory sign-on using a persistence.xml file etc.

There are annotated entity classes including a root class with @OneToMany Collection<X> getAllX, and class X has equivalent @OneToMany Collection<Y> getAllY.

The read code uses an instance of the root class to read allX via a for loop, then x.allY and so on. The root instance is common to each thread.
When two threads do this, the second gets a "org.hibernate.LazyInitializationException : illegal access to loading collection"
and I am unable to get it to recover by catching this in a try .. catch ... and creating a new EntityManager and trying again. It keeps getting the same exception, or a "SessionException: Session is closed".

The specific code is:
for (CM_TerminalArea ta : terminal.getAllTerminalAreas())
{
for(CM_LocationsController lc : ta.getAllLocationsControllers())
{
for (CM_LocationsForTrackedItem lti : lc.getAllTrackedItems())
{
CM_TrackedItem ti = lti.getMyTrackedItem();
if (ti.getMyVoyage() == null)
{
continue;
}

... etc ...

'terminal' is the root object

Firstly, is this a valid sort of thing to do?
Is Hibernate as a JPA provider supposed to handle this?
(TopLink Essentials doesn't get an exception in this case, for at least 5 simultaneous threads)


It seems to me that this sort of activity may well occur in user code in a multi-threaded Java application server for example.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 1:43 am 
Newbie

Joined: Sat Oct 28, 2006 6:16 am
Posts: 17
If by "The root instance is common to each thread" you mean that the same root instance is shared by all threads then this would explain the exceptions you are getting. I suspect that the EntityManager that loaded this root instance is being closed by one of the threads before the other threads have finished using it. When you access an instance and the EntityManager that loaded the instance is closed you can get the exceptions that you encounted.

To fix this issue you could have each thread load its own root instance using its own EntityManager. Another way is to turn off lazy loading for the associations you are accessing (see http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#performance-fetching-lazy) or eagerly fetch the associated objects (read about "fetch" joins at http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#queryhql-joins).

I'm not sure whether what you have done is a valid use of JPA or not.

Hope that helps


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 1:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Using the same EntityManager (or any of its associated object) in a concurrent manner is not a safe use of JPA.

Also, please use the latest versions of the Hibernate libs. At least don't run CR versions when newer final versions are out there.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 6:02 pm 
Newbie

Joined: Wed Jul 25, 2007 9:07 pm
Posts: 2
Thanks for the quick responses.
Changing each thread to get its own root object rather than sharing the same one fixed the problem, which is obvious in hindsight.


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.