-->
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: cannot access loading collection problem across threads
PostPosted: Thu Dec 29, 2005 1:09 pm 
Newbie

Joined: Thu Dec 29, 2005 12:47 pm
Posts: 4
I have a problem with "cannot access loading collection" errors in the following scenario:

Thread 1
Code:
[...]
// get person by using Spring's HibernateTemplate
Person person = hibernateTemplate.get(Person.class, personId);
// now initialize the collection
Hibernate.initialize(person.getTokens());
[...]
// create and start Thread 2


Thread 2
Code:
[...]
// person object passed from Thread 1
Set tokens = person.getTokens();
// now read-access the set
tokens.iterator();
[...]


At the last line I sometimes get a LazyInitializationException with the message "cannot access loading collection".

The funny thing is that this code is executed about 100.000 times a day and it usually always works. Just about once a day I get this error.

Any ideas?

Hibernate version: 2.1.6

Mapping documents:
Code:
<class name="...Person">
[...]
<set name="tokens" lazy="true" cascade="all-delete-orphan" inverse="true">
            <key column="person_id"/>
            <one-to-many class="...Token"/>
        </set>
[...]
</class>


stack trace of any exception that occurs:
net.sf.hibernate.LazyInitializationException: cannot access loading collection
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:191)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
at net.sf.hibernate.collection.Set.iterator(Set.java:130)
[...]

Name and version of the database you are using: MySQL 4.1.14


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 7:22 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Does the token class have any lazily-loaded associations? Hibernate.initialize only forces loading of the objects in the collection, not objects in those objects. If there is a piece of code that is only rarely run but uses a non-initialized association, that would explain what you are seeing.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 30, 2005 3:52 am 
Newbie

Joined: Thu Dec 29, 2005 12:47 pm
Posts: 4
The Token class does not have any collections. The only noteworthy thing it has is maybe the inverse relationship back to Person.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 30, 2005 2:28 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
A quick check for the error: "cannot access loading collection" in the source (2.7) shows that the code that throws it is:

Code:
public abstract class PersistentCollection
[...]
   protected final void initialize(boolean writing) {
      if (!initialized) {
         if (initializing) throw new LazyInitializationException("cannot access loading collection");


So Hibernate thinks the collection is "!initialized" but is "initializing."

Have you tried getting the tokens themselves in thread 1 and passing the collection to thread 2?

Do you have any error handling code that might allow the initialize() call to bomb out, leaving the collection half-initialized, and then proceed to thread 2?


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.