I am using Hibernate and Spring in my project. My DAO has several methods like getEntity() getRelatedEntities() etc.
Since the related entities are loaded lazily, I want to reuse the session across multiple method calls. I want to make sure that getHibernateTemplate() in all the methods in my DAO use the same session instead of creating one for each method call. I have tried using "HibernateTransactionManager" with "LocalSessionFactory"; but whenever I make a call to the DAO, it throws an exception that says "no session is bound or session closed". Do I have to explicitly get a session in the DAO and bind it to the current thread of DAO, so that the session is retained across all method calls in that DAO ?
Also, the use of SessionFactoryUtils.getSession() method is not very clear to me. When and where can it be used ?
Responses are much appreciated.
Joined: Jan 23, 2002
Are you closing the Hibernate Session object somewhere manually? Could you post a code snippet from your DAO implementation class?
I am not explicitly opening or closing sessions. I just use getHibernateTemplate(). I think Spring opens and closes session for every method call.
A typical method in my DAO would look like:
I am interested in reusing the same session object across multiple doInHibernate() calls.
I also tried using a HibernateTemplate object as a instance variable in my DAO and use the instance variable (and hence the same template) to carry out multiple methods; that didnt seem to help either. A new session is used in every method call. [ February 22, 2005: Message edited by: Kaarthik Sivashanmugam ]
subject: reusing sessions across multiple method calls