-->
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.  [ 9 posts ] 
Author Message
 Post subject: ThreadLocal pattern not viable with Jboss EJB3 and 3.1?
PostPosted: Thu Sep 01, 2005 11:44 am 
Newbie

Joined: Mon Mar 22, 2004 2:06 pm
Posts: 9
Location: USA
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
Hibernate 3.1 beta 2
Mapping documents:
Annotations w/ JBoss 4.03RC2 + EJB3

I currently have an app that uses Hibernate 2 and the ThreadLocal web filter pattern to keep the session open so that I can lazy load collections in the web tier (jsp) as desired. This works very well. (Yes, I realize detached objects are the "proper" way to communicate between the web and ejb tier)

When testing with the new versions of Hibernate and JBoss, the connection is always closed and I get lazy execeptions in the jsps. I tried turning the auto-close off using the hibernate properties. However, from reading the 3.1 docs I believe the problem stems from this comment:

... the SessionFactory provides the getCurrentSession() method, which returns a session that is bound to the JTA transaction context. This is the easiest way to integrate Hibernate into an application! The "current" session always has auto-flush, auto-close and auto-connection-release enabled (regardless of the above property settings).

So is the thread local pattern no longer viable with 3.1? Is there no way to turn off auto-close? I can work with this constraint; however, it's really nice not having to worry about initializing collections in the ejb tier, thus gaining the benefit of lazy loading only when a jsp requires it (not to mention filtering collections for paging).

Thanks.


Top
 Profile  
 
 Post subject: Re: ThreadLocal pattern not viable with Jboss EJB3 and 3.1?
PostPosted: Thu Sep 01, 2005 2:41 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
It's not the ThreadLocal pattern or H3.1 that's the issue here. I believe it's because using EJB, you're crossing an application boundry.

The same results would apply today if you used H2.8 or H3.0.x and EJB2 with BMP using Hibernate. The objects are detached when passed from the EJB container to the Web container.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 2:48 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I think he is simply confusing CMTTransactionFactory when he is actually looking for JTATransactionFactory (for BMT). Recommended: read the whole documentation chapter again.


Top
 Profile  
 
 Post subject: Re: ThreadLocal pattern not viable with Jboss EJB3 and 3.1?
PostPosted: Thu Sep 01, 2005 3:40 pm 
Newbie

Joined: Mon Mar 22, 2004 2:06 pm
Posts: 9
Location: USA
pksiv wrote:
It's not the ThreadLocal pattern or H3.1 that's the issue here. I believe it's because using EJB, you're crossing an application boundry.

The same results would apply today if you used H2.8 or H3.0.x and EJB2 with BMP using Hibernate. The objects are detached when passed from the EJB container to the Web container.


Actually, no. I have a functional (in production) application using H2 and EJB2 (not BMP - no entity beans at all with H2 jboss 3.x, I am using CMT however). I use a servlet filter to close the session so it remains open until the JSPs finish rendering. As long as you configure jboss to not automatically close the connection when leaving the EJB tier, this works. I have serveral JSPs which iterate over lazy loaded connections. Without the ThreadLocal + Web filter I would get lazy initialization exceptions. I think this was a well documented pattern back when H2 first became available and it works great. My problem is I can't stop the EJB3 tier from closing the connection. It's possible that since I am technically using Entity Beans (EJB3) not a separate Hibernate ORM solution, that the spec requires the connection be closed and there is no way to turn this off programatically. If so, then the threadlocal pattern (i.e. using a web filter to control the demaracation (open, close session) is not viable when using hibernate in the current EJB3 container.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 3:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
JTA connections are bound to a transaction. If you want a connection that spans the view, your transaction must also span the view. Hibernate has nothing to do with this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 3:48 pm 
Newbie

Joined: Mon Mar 22, 2004 2:06 pm
Posts: 9
Location: USA
christian wrote:
I think he is simply confusing CMTTransactionFactory when he is actually looking for JTATransactionFactory (for BMT). Recommended: read the whole documentation chapter again.


That's possible. I think I see where you are going with this. As I as said in the other reply, my current application does not use entity beans. It uses EJB/CMT combined with the H2 ORM solution. I basically want to do the same thing with the latest Hibernate. But if I understand you correctly, I should not try to use the Hibernate objects as EJB3 entity beans but as regular pojo/orm as I am doing now.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 3:59 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You should not use CMT if you don't want the Container to Manage the Transaction. Otherwise the container will end the transaction when your session bean methods complete, obviously.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 4:00 pm 
Newbie

Joined: Mon Mar 22, 2004 2:06 pm
Posts: 9
Location: USA
gavin wrote:
JTA connections are bound to a transaction. If you want a connection that spans the view, your transaction must also span the view. Hibernate has nothing to do with this.

Yes, I don't think of this as a Hibernate issue. It's more related to how I should configure Hibernate + JBoss + CMT.

BTW, after years of editing deployment descriptors and trying various persistence solutons and configuration tools (xdoclet, ide, etc), the current jdk1.5 annotation support for EJBs and Hibernate persistance is very nice indeed. I was able to create a foundation for a new functional application in 1 day, using the H3, Annotated reference guides. The only minor issue I ran into was trying to leave the session open for the jsp tier to lazy load collections.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 4:17 pm 
Newbie

Joined: Mon Mar 22, 2004 2:06 pm
Posts: 9
Location: USA
christian wrote:
You should not use CMT if you don't want the Container to Manage the Transaction. Otherwise the container will end the transaction when your session bean methods complete, obviously.


That is true. However, it is possible to allow CMT (jboss3.6 + H2) to commit or rollback changes and still allow the session to remain open until the web tier is complete (the trick is to configure jboss not to automatically close open jdbc connections). What happens is the factory.openSession call is not made until a request is made at the DAO layer (which is already involved in a CMT), so hibernate enrolls in the transaction. However, the session is not closed until the web filter closes the session. I admit this blurs the boundries between the ejb/web tier, but functially it works well. I get the advantages of CMT plus the performance improvements from lazy connections without having to decide at the EJB tier which collections to initialize before returning to the web tier. This may not be how the threadlocal pattern was intended to be used, but so far I have a very robust application that's been in production for almost a year so don't yell at me for abusing CMT boundries :)


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