-->
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.  [ 8 posts ] 
Author Message
 Post subject: Lazy loading usage in a JSF/Hibernate home made framework
PostPosted: Fri Jan 14, 2005 11:28 am 
Newbie

Joined: Wed Apr 28, 2004 5:32 am
Posts: 12
Hibernate version:3.0 Beta 1


Hi,

we are currently building a framework with hibernate and JSF.
Both give us satisfaction.

But, we have a problem with hibernate lazy loading.

We use session-per-request-with-detached-objects.

Lazy loading works fine when all associations are loaded in the same http request.


1) Incoming Http Request
2) HttpFilter creates a session
3) An action is excecuted : an objet is loaded from database with hibernate
4) The JSF view display the object
5) HttpFilter destroy the session

Now my object is detached. This object is complex and is associated in many ways to various objects (one-to-many, many-to-one, ...)

My JSF view is a form with tabbed panels but does not load the entire object graph.

The user switch between panels and change properties but the object is detached.

Now the user wants to save the object.

My object owns a validate method which walk through my object graph.

I get errors if i try to reach some object from the graph:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection - no session or session was closed

This behaviour is quite normal and well explained in documentation.

In some use cases, i can use session.update() to re-attach my object graph to the current session, i do it in our framework like in the SaveAction class.

But, i'am currently discovering new uses cases everyday and i don't want to put a call to my persistence layer everywhere.

I look inside Hibernate code and i find where the exception is raised in AbstractPersistentCollection :


protected final void initialize(boolean writing) {
if (!initialized) {
if (initializing) throw new LazyInitializationException("cannot access loading collection");
if ( isConnectedToSession() ) {
if ( session.isConnected() ) {
session.initializeCollection(this, writing);
}
else {
throw new LazyInitializationException("failed to lazily initialize a collection - session is disconnected");
}
}
else {
throw new LazyInitializationException("failed to lazily initialize a collection - no session or session was closed");
}
}
}

I would like to intercept such behaviour, there is DefaultInitializeCollectionEventListener but the exception is raised before.

My idea is to re-attach object if needed to my current session, but in a AOP way, without any pain for developer. This solution is quite intrusive but will be inserted in the framework once for all.

On the other side there is the session-per-user-session pattern we use first.

After doing some load tests it seems this pattern consumes too much memory.

New load test with session-per-request-with-detached-objects are much better.

How can i handle this ???


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 2:19 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If you have to do this, your transactional application behavior is questionable. I'd check the design of the transaction demarcation aspect again, a small change might make this issue go away and still give you explicit transaction demarcation. We never encourage what you are trying to do, which would be implicit transaction demarcation, happening almost randomly.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 3:55 pm 
Newbie

Joined: Wed Apr 28, 2004 5:32 am
Posts: 12
If you have to do this, your transactional application behavior is questionable. I'd check the design of the transaction demarcation aspect again

I see ...
Maybe i should not close the session for every http request.
In the use case i give, maybe the session must live until the JSF form view is closed, because my object is being edited and many http request are necessary to achieve this.

The user can play with tabbed panes and can update some properties, maybe the session must still alive.

1) the user request a form view to edit an object
2) a session is opened
2 ) the user plays with the object inside my form view (many http request)
3) he decides to save the object
4) begin transaction
5) the validate method is invoked and walks through the object graph.
6) commit
7) session is closed

Am i right ? (or not so wrong ... ;o)) )


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 5:41 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
I don't think you'll want to keep the session actually connected between HttpRequests, but one thing you could try is to disconnect your session and then reconnect it on the return HttpRequest. That way you can keep your session alive. I briefly tried that approach on a Tapestry appliation and had it working well. I discarded it because I believed that a Tapestry session per user seemed like an unscalable operation, but if your object model is small enough it would probably work out well.

See section 8.2.4 in the book "Hibernate in Action" for an excellent discussion of this strategy, as well as a servlet filter that implements it. BTW, don't even think about attempting that kind of strategy without reading that book first! It's seriously a must read.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 6:25 pm 
Newbie

Joined: Wed Apr 28, 2004 5:32 am
Posts: 12
I discarded it because I believed that a Tapestry session per user seemed like an unscalable operation, but if your object model is small enough it would probably work out well.

Our framework will have to deal with complex models.
We have models that almost reach database server limits in term of volume.

So we can't use session-per-user-session pattern.

The session must die before the http session.

I use a filter to connect and reconnect the session to database but that's not the point.

Lazy loading doesn't work on detached objects : that's the point.
open and close a session for every http request doesn't fit all needs,
it's doesn't work at all for some very common use cases.

Anyway, i think i've solved my problem (until the next tricky use case ;o))).

the session life is associated to the current view life :

1) a new view is opened i open a session
2) if the view is reloaded (change the tab pane for instance in a form) the same session is alive
3) i quit the view i close the session

So i can walk through my object graph even if some objects of the graph
are not displayed in the view, the session is open and can lazy load the datas.


Top
 Profile  
 
 Post subject: Re: Lazy loading usage in a JSF/Hibernate home made framework
PostPosted: Wed Mar 24, 2010 8:23 am 
Newbie

Joined: Wed Aug 31, 2005 7:51 am
Posts: 3
Hi,

I am interest in your solution code!

Quote:
Anyway, i think i've solved my problem (until the next tricky use case ;o))).

the session life is associated to the current view life :

1) a new view is opened i open a session
2) if the view is reloaded (change the tab pane for instance in a form) the same session is alive
3) i quit the view i close the session

So i can walk through my object graph even if some objects of the graph
are not displayed in the view, the session is open and can lazy load the datas.


What you used? Listener? Can you post, please?


Top
 Profile  
 
 Post subject: Re: Lazy loading usage in a JSF/Hibernate home made framework
PostPosted: Wed Mar 24, 2010 11:29 am 
Newbie

Joined: Thu Aug 13, 2009 4:39 am
Posts: 2
Hi,

wow my post is 6 years old !!!
During all these years we have used a simple pattern : session-per-view.

We use JSF : a view is a single JSP.

The session is opened when the view is initialized for the first time.
Then the user clicks on panned tab or any fragment of the view.

If the user exit from view the session is closed.

This pattern works well with proxy objects initialized when a JSF component try to display them.


Top
 Profile  
 
 Post subject: Re: Lazy loading usage in a JSF/Hibernate home made framework
PostPosted: Sun Nov 04, 2012 5:21 pm 
Newbie

Joined: Sun Nov 04, 2012 5:17 pm
Posts: 1
I'm curious about how you solved this issue. It was programatically in the the view layer? or did you use some kind of filter/interceptor?
I would really appreciate if you can throw some light on how you managed to solve this issue because I'm facing the excatly same problem using this framework setup (JSF / Hibernate).
Thanks in advance.


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