-->
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: Lazy loading of HttpSession-scoped objects - best practices?
PostPosted: Tue Apr 12, 2005 6:47 pm 
Newbie

Joined: Tue Apr 12, 2005 6:40 pm
Posts: 5
Hi,
I'm sure this is a pretty frequent topic on this list, so I'm sure I'll get an answer pretty quickly ;-)

In order for lazy loading to work (either collections or proxy) Hibernate requires the session in which these lazy objects were created to be open when when accessing them. Otherwise you'll get LazyInitializationException. To work around this issue the "Open session in view" pattern is used.

This works fine for request-scoped objects.

But how to deal with session- or application-scoped objects. Let's say you have an Account that has several Users. When a user logs into the application you put the reference to this user object into the session. The user's account (along with the list of its users) is rarely used and you don't want to fetch all account users for every logged-in user, so you declare it as lazy (proxy).

If you try to access the account outside the request in which the user was loaded you'll get LazyInitializationException.

Now Hibernate.initialize() doesn't really cut it. If I use it in my DAO layer then it means that I make assumption how my object will be used. Doing it in business layer is not good either as this would mean that the business layer knows what is the DAO implementation.

In addition Hibernate.initialize() would have to be called on any lazy property (proxy) or collection - one by one, which is not good either.

All this is a documented Hibernate behavior. I'm wondering why Hibernate couldn't open a session or pick one that is open and then do some magic to reassociate the object and have all these lazy loading always working...

What are the best practices to handle such longer-than-request-scoped objects? Is Hibernate.initialize() the only option?

Thanks in advance for any advice.
Regards,
/Slawek Zachcial


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 4:01 am 
Regular
Regular

Joined: Thu Nov 13, 2003 2:55 am
Posts: 71
Location: Sweden
I guess the solution would be to reattach the Account/User with the current session, using update(), merge() or lock(). You could reattach the object with the "view session" using a servlet filter (possibly the same one that opens the session).

You might want to read up on "application transactions".


Top
 Profile  
 
 Post subject: use session.lock
PostPosted: Tue Apr 26, 2005 4:37 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
You can use session.lock to reattach a httpsession object to the hibernate session

Best practice on Struts or TilesStruts:
* create a custom requestprocessor (extend RequestProcessor or TilesRequestProcessor)
* place this in the process function

try {
Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
session.lock(myHttpSessionObject, LockMode.NONE);
} catch (HibernateException he) {
logger.error(he);
}

Where HibernateUtil is a helper class handling some of the reqular plumbing.

If you use lazy properties in your objects and you don't want lazyinitialization exceptions when you access these in JSP:
* do not close the Hibernate session in your dao
* place this after the previous code

try {
super.process(req, res);
} catch (Exception e) {
logger.error(e);
try {
HibernateUtil.rollback();
} catch (HibernateException he) {
logger.warn(he);
}
throw new ServletException(e);
} finally {
try {
HibernateUtil.closeSession();
} catch (HibernateException he) {
logger.error(he);
throw new ServletException(he);
}
}

You can also do this inside a Filter, but filters get created and destroyed multiple times on most requests which imho basically makes them useless for Hibernate.

Marc


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 6:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://www.hibernate.org/168.html

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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.