-->
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.  [ 11 posts ] 
Author Message
 Post subject: HibernateFilter without static sessionFactory
PostPosted: Sun Aug 22, 2004 8:45 am 
Beginner
Beginner

Joined: Wed Oct 29, 2003 11:52 am
Posts: 37
Location: Gothenburg, Sweden
Hi all!

I've failed to find anyone commenting on the fact that the commonly used Hibernate Filter (called Persistance on http://www.hibernate.org/43.html) is insufficient when you're deploying several web applications within the same JVM, and they (need to) use separate SessionFactory instances.

The fix I'm looking at is to set up a static Map that maps request.getServletContext() to SessionFactory objects, instead of a single static reference to a SessionFactory object.

Any suggestions, recomendations or comments is highly appreciated.

(Using Hibernate 2.1.4 and Tomcat 4.1.30.)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 22, 2004 9:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
It should be trivial to just put the SessionFactory into the ServletContext instead of into a static variable.


Top
 Profile  
 
 Post subject: Available to the filter?
PostPosted: Sun Aug 22, 2004 9:58 am 
Beginner
Beginner

Joined: Wed Oct 29, 2003 11:52 am
Posts: 37
Location: Gothenburg, Sweden
Thanks for your suggestion, but I don't see how the filter can get a hold on the ServletContext, all I see is Servlet.getServletConfig().getServletContext() and HttpServlet.getServletContext(). What am I missing? (Or did you suggest that we should skip the filter in the web applications?)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 22, 2004 10:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
in the init method of a Filter, you can do

filterConfig.getServletContext()


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 22, 2004 10:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Wait, shouldn't each webapp have its own classloader, therefore each static class variable should be able to point to a different SessionFactory?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 22, 2004 11:01 am 
Beginner
Beginner

Joined: Wed Oct 29, 2003 11:52 am
Posts: 37
Location: Gothenburg, Sweden
Uhm, I know nothing about how the class loader works in either applications (Tomcat, Hibernate) but basic Idea is that there's two (soon more) applications that all need to use the filter approach to ensure sessions are properly set up and closed.

Calls to HibernateFilter.getSession() must not return sessions from the same SessionFactory. With the current implementation, even though there are two filters set up (one for each application/web.xml), only one factory will be used in BOTH applications since they run within the same JVM.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 2:51 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
if it can help: we're running many hibernate's webapp in the same tomcat... it works.

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 8:13 pm 
Beginner
Beginner

Joined: Wed Oct 29, 2003 11:52 am
Posts: 37
Location: Gothenburg, Sweden
anthony: I'd appreciate it greatly if you could explain how the protected static SessionFactory factory class field/variable is UNIQUE among the web applications, because I don't see how that's possible.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 8:20 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Did you even read Michael's response?

http://www.google.com.au/search?q=singl ... assloaders

Read the first article in the list.

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 3:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Nice link, stolley, I will add it to my collection :)

You just have to make sure your HibernateUtil class or whatever is in two different classloaders. That means you can't share it in something like /common/lib which is bad practice anyways.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 8:08 am 
Beginner
Beginner

Joined: Wed Oct 29, 2003 11:52 am
Posts: 37
Location: Gothenburg, Sweden
stolley wrote:
Did you even read Michael's response?[...]

Of course, but I wasn't aware of the fact that you could make a "common mistake" as the article you pointed me to calls the ability to allow more than one instance to be created. I simply didn't think beyond what I already knew, my bad.

Thanks all for enlightening me :)


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