Tuesday, September 26, 2017

Types of Sessions in hibernate

Difference between getCurrentSession()openSession() and openStatelessSession() of SessionFactory class.

public class Database {
     static SessionFactory sessionFactory =
     new Configuration().configure().buildSessionFactory();
     public static Session getSession(){
          Session session1sessionFactory.getCurrentSession();

          Session session2=
          (Session) sessionFactory.openStatelessSession();

          Session session = sessionFactory.openSession();
          return session;
     }   
}

Hibernate SessionFactory getCurrentSession()
Hibernate SessionFactory getCurrentSession() method returns the session bound to the context. But for this to work, we need to configure it in hibernate configuration file like below.
<property name="hibernate.current_session_context_class">thread</property>

Since this session object belongs to the hibernate context, we don’t need to close it. Once the session factory is closed, this session object gets closed.
Hibernate Session objects are not thread safe, so we should not use it in multi-threaded environment. We can use it in single threaded environment because it’s relatively faster than opening a new session.

Hibernate SessionFactory openSession
Hibernate SessionFactory openSession() method always opens a new session. We should close this session object once we are done with all the database operations.
We should open a new session for each request in multi-threaded environment. For web application frameworks, we can choose to open a new session for each request or for each session based on the requirement.
Hibernate SessionFactory openStatelessSession
Hibernate SessionFactory openStatelessSession() method returns instance of StatelessSession. There is another overloaded method where we can pass java.sql.Connection object to get a stateless session object from hibernate.
StatelessSession in Hibernate does not implement first-level cache and it doesn’t interact with any second-level cache. Since it’s stateless, it doesn’t implement transactional write-behind or automatic dirty checking or do cascading operations to associated entities.
Collections are also ignored by a stateless session. Operations performed via a stateless session bypass Hibernate’s event model and interceptors. It’s more like a normal JDBC connection and doesn’t provide any benefits that come from using hibernate framework.
However, stateless session can be a good fit in certain situations. For example where we are loading bulk data into database and we don’t want hibernate session to hold huge data in first-level cache memory.

public class HibernateSesssionExample {
     public static void main(String[] args) {
          SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
         
          /* getCurrentSession()- no need to close
           * attached to hibernate context*/
         
          Session currentSession = sessionFactory.getCurrentSession();
         
          /*No need to close current session */
         
          /* Return New session everytime */
          Session session = sessionFactory.openSession();
         
          session.close();
         
          /* Return stateless session- To avoid cache*/
          Session statelessSession
          (Session) sessionFactory.openStatelessSession();
          statelessSession.close();
     }
}

References
https://www.journaldev.com/3522/hibernate-sessionfactory

No comments:

Post a Comment