CODEXE

openSession vs getCurrentSession

First, hibernate session is not thread safe.

1.openSession will create a new session. getCurrentSession will create a new session bound to the current thread.

2.session created by getCurrentSession will be closed automatically when the transaction rollback or commit. session created by openSession need to be closed manually.

3. If a session is already created in the context, getCurrentSession will not create a new session and return the existing session.

Enable getCurrentSession
add this line in the hibernate.cfg.xml file < property name="hibernate.current_session_context_class">thread

For the web aplication, we should remember One session per request. The basic idea is to create a filter for hibernate and calling getCurrentSession to create or get a session. Then use getCurrentSession in the service layer to comunicate with database.