-->
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.  [ 15 posts ] 
Author Message
 Post subject: Session-per-request, JSF and lazy initialization
PostPosted: Thu Jan 26, 2006 12:43 pm 
Newbie

Joined: Thu May 06, 2004 10:10 am
Posts: 14
Hi all,

JSF advocates session scoping of its managed beans as this gives the webapp stateful behaviour, which ofcourse would be nice, and save us from a lot plumbing that was needed in the Struts age.

To be able to get all these nice things, developers are required to be able to put Hibernate-managed pojo's in httpsession scope, without running into LazyInitialization problems.

So I went looking on the net for documentation. I found several suggestions, like using session-per-session but none striked me as the way I would want to go, at least not before posting this question.

Would it be possible to use some kind of hibernate/jsf integration tool (maybe a servlet filter) that would allow for the following:

1. Be able to keep using session-per-request and the open-session-in-view filter

2. Automagically reattach any hibernate managed pojo to the new session that is in use by a session-scoped JSF component (ofcourse this would have to happen very early in the lifetime of a request)


Would the above be possible and architecturally correct? If so, does such a piece of software already exist?

-- Hans


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 1:49 pm 
Newbie

Joined: Sun Jan 22, 2006 8:34 am
Posts: 2
If you dare to use something very advanced, but is still in beta I would take a look at seam.

http://www.jboss.com/products/seam

You get a lot for free, even workspace management, transactions,
a very simple to use bean management - just annotate the classes
and off you go, no xml bean definition necessary etc...

The only drawback is that you will have to use at least the jboss microcontainer or a different application server.


Top
 Profile  
 
 Post subject: Re: Session-per-request, JSF and lazy initialization
PostPosted: Thu Jan 26, 2006 2:29 pm 
Newbie

Joined: Mon Jan 23, 2006 1:11 pm
Posts: 15
Location: Leuven, Belgium
mirror303 wrote:
To be able to get all these nice things, developers are required to be able to put Hibernate-managed pojo's in httpsession scope, without running into LazyInitialization problems.


You don't really need to do that. You can simply let the entities go in a detached state (and managed by jsf) .. And afterwards merge them back into managed state...

I use a simple transaction filter per request and this also avoids lazyinitialization problems... Hibernate uses

web.xml
Code:
   <!-- wrap each request in a usertransaction -->
   <filter>
      <filter-name>transaction-filter</filter-name>
      <filter-class>com.caucho.filters.TransactionFilter</filter-class>
   </filter>

   <filter-mapping>
      <filter-name>transaction-filter</filter-name>
      <url-pattern>*</url-pattern>
   </filter-mapping>


persistence.xml
Code:
<persistence>
   <persistence-unit name="regapan" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:comp/env/jdbc/regapan</jta-data-source>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.jta.UserTransaction" value="java:comp/UserTransaction"/>
         <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
         <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.ResinTransactionManagerLookup"/>
      </properties>
   
   </persistence-unit>
</persistence>

_________________
Don't applaud, throw money (paypal to timvw@users.sourceforge.net) - http://www.timvw.be


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 3:45 pm 
Newbie

Joined: Thu May 06, 2004 10:10 am
Posts: 14
Hello Tim,

I'm affraid your solution is not applicable for me because I am using Spring to handle my transactions. I will take a look at that filters sourcecode for ideas though..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 5:12 pm 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
I just skimmed the posts but I think what you're describing is exactly Springs OpenSessionInViewInterceptor which will neatly bind a session per request. I notice your using AOP to manage your TX's - why not use it for your sessions too?

A good article on it here http://www.jroller.com/page/kbaum/20050406


Top
 Profile  
 
 Post subject: Re: Session-per-request, JSF and lazy initialization
PostPosted: Thu Jan 26, 2006 5:28 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
mirror303 wrote:
Hi all,

JSF advocates session scoping of its managed beans as this gives the webapp stateful behaviour, which ofcourse would be nice, and save us from a lot plumbing that was needed in the Struts age.



Try to test it using some load testing tool.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 6:46 pm 
Newbie

Joined: Thu May 06, 2004 10:10 am
Posts: 14
timrobertson100 wrote:
I just skimmed the posts but I think what you're describing is exactly Springs OpenSessionInViewInterceptor which will neatly bind a session per request. I notice your using AOP to manage your TX's - why not use it for your sessions too?

A good article on it here http://www.jroller.com/page/kbaum/20050406


Hi Tim R,

I am familiar with the OpenSessionInViewFilter and I am already using it. I think you did not understand my problem. The problem is that my domain pojo's get disconnected from their hibernate session after each request and I don't know of a way of reattaching them automatically at the beginning of each subsequent request in the http session.


Top
 Profile  
 
 Post subject: Re: Session-per-request, JSF and lazy initialization
PostPosted: Thu Jan 26, 2006 6:55 pm 
Newbie

Joined: Thu May 06, 2004 10:10 am
Posts: 14
Hello baliukas,

baliukas wrote:
mirror303 wrote:
Hi all,

JSF advocates session scoping of its managed beans as this gives the webapp stateful behaviour, which ofcourse would be nice, and save us from a lot plumbing that was needed in the Struts age.



Try to test it using some load testing tool.


You mean that the memory usage will be ridiculously high due to storing too many objects in the session? I absolutely agree that this is a problem. Storing state at the clientside is not an option either, i find it amazing that the JSF-people even specified this.

But do you still think that storing pojos in the httpsession is not an option even if I'm not expecting many (not more than let's say 5) concurrent sessions? Can you suggest an alternative?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 2:15 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It depends on scalability requarements. Seam must help too, BPM must manage state without problems (workflow stuff is designed for state management in mutitranasaction process, process can be multiuser too).
I have no test, but I am afraid plain JSF ( any stefull web framework) can not manage persistent state (stefull UI stuff and components must not be a problem).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 5:20 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
BTW, there is no problems to fake session scoping in http session wrapper


Object getAttrubute(String name){
return load(session.getAttrubute(name));
}

void setAttribute (String name,Object value){

session(name,getId(value));

}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 5:33 am 
Newbie

Joined: Thu May 06, 2004 10:10 am
Posts: 14
Hmm yes but I don't think I can hook that kind of code into JSF. Right now i'm thinking of writing a PhaseListener that looks for http session-scoped hibernate pojos in the JSF 'restore view' phase and reattaches them

I took some time to read some sourcecode from Seam, and from what I can make out of it right now, Seam uses a similar strategy.

regards,
hans


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 5:46 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
You can wrapp http session in filter for any framework (if framework doe's not cast wrappers ).

Reattached entity is stale (or you must reload it anyway), probably it works for specific use cases (non transactional data), but it is a wrong assumption.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 5:48 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
You must synchronize you code to store data in http session directly (it is not thread safe) and forget about clustering.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 6:53 am 
Newbie

Joined: Thu May 06, 2004 10:10 am
Posts: 14
Clustering - yes, but in a lot of situations I will just stick with a load balancer that simply assigns users sessions to a machine. If users lose a session, too bad.

Stale data worries me a lot more. Are you basically saying that I shouldn't try to put pojos in the http session at all, i.e. let every JSF managed-bean be request-scoped. That would be a pitty, but alas...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 8:59 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
"Optimistic locking" can be used in some cases to solve this issue (to detect write conflict)


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