Session Management with Java Hibernate

I have a Hibernate-based platform built from stateless servlets (one is used to register a user, and the rest is used to query db).

I use Hibernate sessions as follows:

Session session = HibernateUtil.getSessionFactory().getCurrentSession(); if ((null == session) || (session.isOpen() == false)) { session = HibernateUtil.getSessionFactory().openSession(); } 

I currently do not close the session at the end of the servlet to avoid calling openSession() (when trying to use open sessions, if possible).

What is the best practice? when should i close these sessions?

Can you give an example?

Thanks in advance!

+7
source share
4 answers

Best practice is in most cases a session per request. That is, open a session at the beginning of request processing and close it at the end. You can do this in the Filter servlet, for example.

Having one session for the entire application is bad, because it will accumulate many objects in the cache of the 1st level, which is a memory leak. It can also give vague results when several clients use it simultaneously.

However, your code does not use a single session for the entire application - it uses the concept of the "current session", which opens a session and stores it in context (for example, ThreadLocal ). But if you do not close it, he will remain there forever. In addition, this will cause the same problems as described above, because the threads are reused in the web application, and the new request will receive the old unclosed session at some point.

+13
source

It is always best to open a new session for each request and close the session after processing the request. how

 Session session = HibernateUtil.getSessionFactory().openSession(); 

instead

 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 

If we use the getCurrentSession () method, tansaction.commit () / rollback () closes the connection.

+3
source

The best way to manage your hibernate session is to open a new session for each request. It all depends on how you get the session.

  • if you use sessionFactory.getCurrentSession (), you will get a "current session" associated with the transaction life cycle and will be automatically cleared and closed when the transaction (commit or rollback)
  • if you decide to use sessionFactory.openSession (), you will have to manage the session yourself and hide and close it manually.
 if (!session.isOpen()) { session = session.getSessionFactory().openSession(); session.beginTransaction(); } 
0
source

I recommend you use the spring framework. The reason for spring is that you can use @Transactional at the method level and the session will be automatically created and closed by the transaction manager (if you are not using any open session as an interceptor) using AOP , which is internally handled by the infrastructure.

 @Autowired EntityManager em; @Transactinal void save(User user){ em.persist(user); } 

thats all.spring is fun: D

0
source

All Articles