-->
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.  [ 1 post ] 
Author Message
 Post subject: AbstractPersistentCollection & Lazy Initialization
PostPosted: Tue Jun 21, 2005 10:33 am 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
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]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.