whats the Diff between getHibernateTemplate().getSessionFactory().getCurrentSession() and getSession
Ramki Dirisala
Greenhorn
Joined: Mar 25, 2011
Posts: 7
posted
0
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
posted
0
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
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.