• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

whats the Diff between getHibernateTemplate().getSessionFactory().getCurrentSession() and getSession

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whats the Diff between




i want to get a session object to create Query.

example :


which one i can use to get the session. i have transaction management implemented in my code.

any help is appreciated.

Thanks,
Ramki.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A org.hibernate.Session begins when the first call to getCurrentSession() is made for the
current thread. It is then bound by Hibernate to the current thread. When the transaction ends,
either through commit or rollback, Hibernate automatically unbinds the org.hibernate.Session
from the thread and closes it for you. If you call getCurrentSession() again, you get a new
org.hibernate.Session and can start a new unit of work.Where as if you use getSession() ,it needs to be closed manually,which some times causes resourse leakage problem
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you call getCurrentSession() again, you get a new
org.hibernate.Session and can start a new unit of work.Where as if you use getSession() ,it needs to be closed manually,which some times causes resourse leakage problem



What he means is after the transaction is complete if you call getCurrentSession again you get a new one, but if you are still in the same transaction, each subsequent call to getCurrentSession will return the same Session object that is in ThreadLocal.

getSession Hibernate always creates a new Session each call.

If you are using Hibernate 3.1 or higher, you should not use the HibernateTemplate or HibernateDaoSupport classes in your code. You will not receive any benefits except coupling your code to Spring, which Spring does not want you to do.

Thanks

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic