I've found references to people who experienced the "illegal access to loading collection" error when using reflection to calculate persistable Object's hashCodes. We are using a combination of the base Object hashCode and unique id for hashCode, so we should not fall into this bucket. I'm aware of the discussion of using natural, immutable properties vs ie for hashCode generation, and we've taken them into consideration in our design. Since the page that I'm testing with is "read only" the downsides of using an Id based hash should not come into play. Is this a safe assumption?
So, here is the big question... how are two threads apparently calling AbstractPersistentCollection.initialize() at the same time? Here is the method from the Hibernate source code.
Code:
protected final void initialize(boolean writing) {
if (!initialized) {
if (initializing) {
throw new LazyInitializationException("illegal access to loading collection");
}
throwLazyInitializationExceptionIfNotConnected();
session.initializeCollection(this, writing);
}
}
This is the only place I can find where a "illegal access to loading collection" Exception is thrown, and unless the session.initializeCollection() method spawns a new thread then two threads must be calling this method simultaneously for the error to occur.
If the same Object is retreived using get() in two separate sessions will they both be instances of exactly the same Object. Ie, pointing to the same place in memory?
The strange occurrence is that this only seems to happen with collections or Objects related to the User we store in http session. Here is the general workflow for session User management.
1. User logs into the application and a Hibernate-attached User Object is put in http session
2. On subsequent page views, our AuthenticationFilter takes the now Hibernate-unattached User Object out of the http session, gets a copy of the User from the database using Hibernate's session.get(), and puts this Hibernate-attached User back into the http session. In this manner we always have a Hibernate-attached User in the http session while the request is being processed.
All of the errors I'm seeing are from this User Object or Objects it is related to (ex. user.getUserViews()).
We're making sure that during a request the User in http session is always attached to the Hibernate session to prevent LazyInitializationExceptions due to session. Is it possible that in doing so we have opened up issues with LazyInitializationExceptions because of "illegal access to loading collection" when under load?