Hi,
I've been experiencing lots of problems with lazy collections, as already stated in many posts in this forum.
So I tried to isolate the focus of the problem, and arrived to this class, which has a method:
Code:
protected final void initialize(boolean writing) {
if (!initialized) {
if (initializing) {
throw new LazyInitializationException("illegal access to loading collection");
}
if ( isConnectedToSession() ) {
if ( session.isConnected() ) {
session.initializeCollection(this, writing);
}
else {
throwLazyInitializationException("session is disconnected");
}
}
else {
throwLazyInitializationException("no session or session was closed");
}
}
}
This method calls the isConnectedToSession() method to determine whether the actual collection can be initialized using the session binded to the collection:
Code:
private final boolean isConnectedToSession() {
return session!=null && session.isOpen();
}
As many of you will have experienced, the problem with this piece of code is that using a pattern like the OpenSessionInViewFilter from Spring, does not work, as different sessions are created in each request, so a lazy initialization exception is always thrown.
So I was wondering whether it could be a suitable solution to give a different implementation of the AbstractPersistentCollection that would try to find a valid session in the current ThreadLocal in case the binded session to the actual collection wasn't valid, instead of throwing an exception.
In this sense, the fact that the initialize() method is final, seems to be saying that no extension should be allowed to do this :-(
What do you think about this approach? Does it make sense?
Looking forward to hearing your comments.
[/code]