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: One SessionFactory, multiple ClassLoaders?
PostPosted: Mon Jan 24, 2005 7:27 am 
Newbie

Joined: Mon Dec 13, 2004 6:30 pm
Posts: 5
Hibernate version: 2.1.7c

I have a situation where I have multiple classloaders over the lifetime of my application, each bringing its own set of mappings. Right now, I have multiple SessionFactories, one for each classloader. I would like to try sharing one SessionFactory over all classloaders.

My problem is that during the buildSessionFactory() stage, Configuration wants to find classes in the context classloader, but this is not possible.

Is there any way around this? Is there a way to bind the class as I supply the mappings?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 11:31 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't think you can do such things

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Need to set context classloader of thread containing Config.
PostPosted: Mon Jan 31, 2005 5:19 pm 
Newbie

Joined: Mon Jan 31, 2005 5:11 pm
Posts: 2
Solarsail:

You _might_ be able to accomplish what you want using the following general approach:

1. Instantiate your custom classloaders. I'm presuming you are chaining each one together using appropriate constructors AND that the first loader you created bound itself to the classloader of the current thread.

2. Set the context classloader of the current thread using:
Thread.curentThread.setContextClassLoader({refToYourClassLoader})

3. Create your Configuration object and bind definitions as you normally would.

This will not work if Hibernate and your client are operating in separate threads, of course. If you have written a controller that manages the Hibernate thread, you could use a similar philosophy to try and hook the Hibernate class loader.

Note: my only experience with the above has been while idly hacking the Hibernate code. Don't try this in a production environment until you've thoroughly tested it.

Good luck.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 31, 2005 7:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
btw. this how hibernate console works - but it is surely nothing i would advise you to do.

btw. if the classes is not available in context class loader nor on the classpath of hibernate how should hibernate code know how to find it ?
it would be insanely ugly to carry around a "userclassloader" to do this...and that is actually why there is such a thing as a context classloader.
/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 31, 2005 11:03 pm 
Newbie

Joined: Mon Jan 31, 2005 5:11 pm
Posts: 2
Max:

Quote:
it would be insanely ugly to carry around a "userclassloader" to do this...and that is actually why there is such a thing as a context classloader.


Don't know about the ugly part, but it's certainly not insane. There are a variety of problems that could use dynamically resolved/dynamically generated classes to solve. Remote code fetching, on-the-fly classfile generation--these things aren't going to fit nicely into a static context. Examples are everywhere--look at just about any J2EE server or, for that matter, at the Hibernate Console, as you point out.

The classloader mechanism is as extensible as it is for a reason--Sun never thought they'd have the be-all/end-all solution. The context loader works just great about 99% of the time. But that 1% really needs the ability to include it's own loader in the delegation chain.

And the beauty of the extensible classloader is that Hibernate doesn't need to care about finding the classes. As long as the config is supplied a valid classmapping and the classload chain-supplied definitions pass the bytecode verifiers, it doesn't have to give a hoot.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 01, 2005 4:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Hi Eric,

I don't think you read my answer ;)

I said *carrying* a userclassloader around (here i meant as arguments/fields across all hibernates code) is insanely ugly....

I then said the context classloader was there to solve this problem since it is a threadlocal and i even used it in hibernate console to work around some quirks with jdbc and user classes et.al.

but i would never advise one to do it, just because he could - messing with ctx classloaders are simple, but it can have nasty sideeffects people don't realize several months into a project where it is to late to fix/shift strategy.

_________________
Max
Don't forget to rate


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.