-->
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.  [ 6 posts ] 
Author Message
 Post subject: DAOs and lazy loading best practice
PostPosted: Wed Oct 04, 2006 4:21 am 
Newbie

Joined: Wed Oct 04, 2006 2:47 am
Posts: 12
What is the best practice for using lazy loading in a DAO?

I currently have a DAO which opens/closes a new session for each operation.
This works fine, but for performance reasons, we've now had to enable lazy loading for some collections.

To avoid LazyInitizationException, I've been experimenting with a modified OpenSessionInView pattern:

At the start of a request. a session is opened, and its flush mode set to FLUSH_NEVER.
This session is solely used for reads (call it 'reader'). Objects returned are flagged as read-only.

To save an object:
. the 'reader' session is cleared
. a 'writer' session is established
. the object is saved using 'writer' in a transaction
. the 'writer' session is closed.

The reader session is cleared so that:
. the object and any children aren't being managed by 2 sessions
. to force the 'reader' session to reload any modified objects subsequent to a save.

This approach seems to work, and retains the behaviour of the existing DAO but isn't very elegant. Is there a better approach?

-Tim


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 5:47 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://hibernate.org/42.html
http://hibernate.org/43.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 8:25 pm 
Newbie

Joined: Wed Oct 04, 2006 2:47 am
Posts: 12
christian wrote:
http://hibernate.org/42.html
http://hibernate.org/43.html


Thanks - I'd found both of those pages. I was looking for something more specific.
The DAO currently detaches objects from the session. I need to retain this behaviour (in the logical sense), to ensure that changes to objects are only persisted through calls to the DAO.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 3:34 am 
Newbie

Joined: Sun Sep 03, 2006 1:21 am
Posts: 9
have BaseHibernateDao with a method returning SessionFactory.getCurrentSession() and all your DAOs inherit it to acess the session. So you have 1 session/thread or request instead session/each operation.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 8:22 pm 
Newbie

Joined: Wed Oct 04, 2006 2:47 am
Posts: 12
Unfortunately a single session only works for the simple case.
Clearing the read session before each write causes LazyInitializationExceptions afterwards in more complex object graphs. Touching the objects to avoid this is not really an option.

It appears I need to:
1. use an OpenSessionInView pattern for reads, opening a new session for each read. Session closure is deferred to the end of the request.
2. create a new session for each write

However, the latter requires that I evict the object being written from the session that read it, or I get:
"Illegal attempt to associate a collection with two open sessions"

Is it possible to get the session that currently manages an object, or do I need to maintain a map of objects to sessions, in order to do the evict()?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 12:28 am 
Newbie

Joined: Wed Oct 04, 2006 2:47 am
Posts: 12
In the end, I did away with OpenSessionInView. It doesn't seem to be a workable solution where a DAO is used and the objects must be detached from the session (or at least appear to be).

The workaround was to add an API that provides support for partial object queries. This touches the requested object properties to avoid LazyInitializationException, prior to closing the session.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.