Saturday, November 7, 2015

Hibernate openSession VS openStatelessSession


We mostly use hibernate ‘s sessionFactory.openSession() method which keeps track of all events of hibernate along with managing first level cache, and also interacts with second level cache of hibernate. It also performs operations cascade on associated entities.

In openSession() method dirty checking is done automatically,  means when we fetch data from DB and do some changes it automatically check updated record and update the database accordingly.Many of things are not possible to do with org.hibernate.Session interface.

However, Hibernate gives alternative interface when you want to work with your database like executing simple JDBC Statements. This interface is org.hibernate.StatelessSession.
Hibernate sessionFactory.openStatelessSession() method works line simple JDBC. It neither listens any of hibernate events nor does any things by own.

A StatelessSession does not maintain first level cache or never interacts with second level cache, nor does it automatically do dirty checking, nor do any operation cascade on related associations. In StatelessSession you have to explicitly trigger a query or event for each update or modification on an Object.  

 For more details click here

2 comments:

Spring Boot Config Server and Config Client.

 In Spring cloud config we can externalise our configuration files to some repository like GIT HUT, Amazon S3 etc. Benefit of externalising ...