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 2Code:
[...]
// 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