From what I have read you create a session per request and close the session after the request has finished.
Would this be right usage of sessions in hibernate?
Session userSession = MyHibernateClass.getFactory().openSession();
session.close();
When do I use getCurrentSession()
and when openSession()
?
SessionFactory.openSession()
always opens a new session that you have to close once you are done with the operations. SessionFactory.getCurrentSession() returns a session bound to a context - you don't need to close this. For more detailed information you can follow given link openSession() v/s getCurrentSession()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.