简体   繁体   中英

How to use hibernate-sessions properly?

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?

  1. start server and create SessionFactory
  2. user want to query data from database 2.1 Session userSession = MyHibernateClass.getFactory().openSession();
  3. do one query
  4. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM