• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reusing sessions across multiple method calls

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you closing the Hibernate Session object somewhere manually?
Could you post a code snippet from your DAO implementation class?
 
Kaarthik Sivashanmugam
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick response.

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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic