Two Laptop Bag
The moose likes Spring and the fly likes whats the Diff between getHibernateTemplate().getSessionFactory().getCurrentSession() and getSession Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Reply Bookmark "whats the Diff between getHibernateTemplate().getSessionFactory().getCurrentSession() and getSession" Watch "whats the Diff between getHibernateTemplate().getSessionFactory().getCurrentSession() and getSession" New topic
Author

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

Ramki Dirisala
Greenhorn

Joined: Mar 25, 2011
Posts: 7
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.
rakesh shetty
Greenhorn

Joined: Jan 20, 2009
Posts: 2
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
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17225
    
    1

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: whats the Diff between getHibernateTemplate().getSessionFactory().getCurrentSession() and getSession
 
Similar Threads
CRUD + Hibernate + GWT
HibernateTemplate batch Update
parameter not set!
Criteria across multiple tables
Abstract Generic Class for DAO with spring SessionFactory injection setup help needed.