View Full Version : Call Hibernate Session from init method?
smozely
Feb 13th, 2006, 11:27 PM
I have some manager beans that need to get information from our DB when they are init'ed. I thought the easiset way to do this would be to call the DAS beans from the init method of the manager bean.
But this always gets the no hibernate session bound to thread exception ... I have tested and my hibernate interceptor ensures that sessions are started when all the delegate methods are called... except when any of the methods is defined as a spring init method.... I can see why this probably shouldn't work, because the init method will be running against the real object, not the proxy created for session management ...
I was just wondering if there was a way around this so I can effectivly access the hibernate session when the object is starting up?
Costin Leau
Feb 14th, 2006, 07:20 AM
I can see why this probably shouldn't work, because the init method will be running against the real object, not the proxy created for session management ...
Can you explain your problem in details with some code and configuration? I assume the problem occurs because there is no session bound to the thread - you can overcome this by either using a HibernateTemplate with allowCreate set to true or by using the HibernateInterceptor (as you did) and wrapping it around your object.
You can tell Spring the order of initialization by using depends-on attribute.
smozely
Feb 14th, 2006, 01:31 PM
Cool, thanks for a reply
So what I have is a manager layer of objects, and these access data access layer objects which do the hibernate work.
public class SomeManager{
private SomeHibernateWorker shw;
public void init(){
shw.doSomeWork();
}
public void setShw(SomehibernateWorker shw) ...
}
public class SomeHibernateWorker{
public Stuff doSomeWork(){
... Access hibernate stuff using SessionFactoryUtils...
}
public void someOtherMethod{
...
}
}
That sample code is about the guts of what my stuff is trying to achive, the bean definition for SomeManager will be set with init-method="init" and a hibernate interceptor is setup so that all methods on SomeManager will use a hibernate session... I thought what would happen is the init-method would run against the target object not the proxy, and thus would miss the hibernate session stuff... and I get the no hibernate session bound to thread error .... Is there a way around this or am I wrong and this should work?
Costin Leau
Feb 15th, 2006, 04:03 AM
I thought what would happen is the init-method would run against the target object not the proxy, and thus would miss the hibernate session stuff... and I get the no hibernate session bound to thread error .... Is there a way around this or am I wrong and this should work?
Why don't you simply try? :) Make a simple test with a very basic application context and run it and see how it behaves.
smozely
Feb 15th, 2006, 04:13 AM
Dont get me wrong, I have tried and im posting here because it didn't work... Im wondering if I need to use the MethodInvokingFactoryBean to get this thing running properly ... maybe?
Costin Leau
Feb 15th, 2006, 04:37 AM
HibernateInterceptor will be applied only after the bean has been initialized and your methods called. Even if you use MethodInvokingFactoryBean you need to intercept the method which complicates your configuration even more.
Why don't you use HibernateTemplate with allowCreate set to true? If you are using transactions, the thread bound session will be started by your template if there isn't one found.
If you don't then the session will be opened only during your execute() block.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.