Hibernate Second Level Cache

Hibernate second dimension cache utilizes a regular reserve for all the session object of a session factory. It is helpful in the event that you have various session objects from a session factory.

SessionFactory holds the second dimension store information. It is worldwide for all the session objects and not empowered as a matter of course.

Different merchants have given the usage of Second Level Cache.

  1. EH Cache
  2. OS Cache
  3. Swarm Cache
  4. JBoss Cache

Each execution gives diverse store utilization usefulness. There are four different ways to utilize second dimension cache.

  1. read-only: reserving will work for read just operation.
  2. nonstrict-read-write: storing will work for read and compose however one at a time.
  3. read-write: storing will work for read and compose, can be utilized simultaneously.
  4. transactional: storing will work for transaction.

The store utilization property can be connected to class or gathering level in hbm.xml document. The guide to characterize store utilization is given below:

  1. <cache usage="read-only" />  

Let's see the second dimension reserve execution and store usage.

Implementationread-onlynonstrict-read-writeread-writetransactional
EH CacheYesYesYesNo
OS CacheYesYesYesNo
Swarm CacheYesYesNoNo
JBoss CacheNoNoNoYes

Hibernate Second Level Cache Example

To comprehend the second dimension store through precedent, we have to pursue the accompanying steps:

  1. Create the constant class utilizing Maven
  2. Add venture data and design in pom.xml file
  3. Create the Configuration file
  4. Create the class that recovers the persevering object.

1) Create the persistent class using Maven.

File: Employee.java
package com.javacodegeeks.net;    
  • import javax.persistence.*;  
  • import org.hibernate.annotations.Cache;  
  • import org.hibernate.annotations.CacheConcurrencyStrategy;  
  • @Entity  
  • @Table(name="emp1012")  
  • @Cacheable  
  • @Cache(usage=CacheConcurrencyStrategy.READ_ONLY)  
  • public class Employee {    
  •     @Id  
  • private int id;    
  • private String name;    
  • private float salary;    
  •     
  • public Employee() {}    
  • public Employee(String name, float salary) {    
  •     super();    
  •     this.name = name;    
  •     this.salary = salary;    
  • }  
  • public int getId() {  
  •     return id;  
  • }  
  • public void setId(int id) {  
  •     this.id = id;  
  • }  
  • public String getName() {  
  •     return name;  
  • }  
  • public void setName(String name) {  
  •     this.name = name;  
  • }  
  • public float getSalary() {  
  •     return salary;  
  • }  
  • public void setSalary(float salary) {  
  •     this.salary = salary;  
  • }    
  • }    

  • 2) Add project information and configuration in pom.xml file.

    Open pom.xml file and click source. Now, add the below dependencies between <dependencies>....</dependencies> tag.

    1.   <dependency>  
    2.     <groupId>org.hibernate</groupId>  
    3.     <artifactId>hibernate-core</artifactId>  
    4.     <version>5.2.16.Final</version>  
    5. </dependency>  
    6.       
    7. <dependency>  
    8.     <groupId>com.oracle</groupId>  
    9.     <artifactId>ojdbc14</artifactId>  
    10.     <version>10.2.0.4.0</version>  
    11. </dependency>  
    12.       
    13. <dependency>  
    14.     <groupId>net.sf.ehcache</groupId>  
    15.     <artifactId>ehcache</artifactId>  
    16.     <version>2.10.3</version>  
    17. </dependency>  
    18.       
    19.      
    20. <dependency>  
    21.     <groupId>org.hibernate</groupId>  
    22.     <artifactId>hibernate-ehcache</artifactId>  
    23.     <version>5.2.16.Final</version>  
    24. </dependency>  

    3) Create the Configuration file

    File: hibernate.cfg.xml
    1. <?xml version='1.0' encoding='UTF-8'?>    
    2. <!DOCTYPE hibernate-configuration PUBLIC    
    3.           "-//Hibernate/Hibernate Configuration DTD 5.2.0//EN"    
    4.           "http://hibernate.sourceforge.net/hibernate-configuration-5.2.0.dtd">    
    5.     
    6. <hibernate-configuration>    
    7.     
    8.     <session-factory>    
    9.         <property name="show_sql">true</property>    
    10.         <property name="hbm2ddl.auto">update</property>    
    11.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>    
    12.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>    
    13.         <property name="connection.username">system</property>    
    14.         <property name="connection.password">jtp</property>    
    15.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>    
    16.          
    17.          <property name="cache.use_second_level_cache">true</property>   
    18.          <property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>  
    19.          <mapping class="com.javacodegeeks.net.Employee"/>  
    20.     </session-factory>    
    21. </hibernate-configuration>    

    To execute second dimension store, we have to characterize cache.provider_class property in the arrangement file.


    4) Create the class that retrieves the persistent object.

    File: FetchTest.java
    1. package com.javacodegeeks.net;    
    2.     
    3. import org.hibernate.Session;    
    4. import org.hibernate.SessionFactory;  
    5. import org.hibernate.boot.Metadata;  
    6. import org.hibernate.boot.MetadataSources;  
    7. import org.hibernate.boot.registry.StandardServiceRegistry;  
    8. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
    9.     
    10. public class FetchTest {    
    11. public static void main(String[] args) {    
    12.     StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
    13.     Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();  
    14.       
    15.     SessionFactory factory=meta.getSessionFactoryBuilder().build();  
    16.         
    17.     Session session1=factory.openSession();    
    18.     Employee emp1=(Employee)session1.load(Employee.class,121);    
    19.     System.out.println(emp1.getId()+" "+emp1.getName()+" "+emp1.getSalary());    
    20.     session1.close();    
    21.         
    22.     Session session2=factory.openSession();    
    23.     Employee emp2=(Employee)session2.load(Employee.class,121);    
    24.     System.out.println(emp2.getId()+" "+emp2.getName()+" "+emp2.getSalary());    
    25.     session2.close();    
    26.         
    27. }    
    28. }    






    javacodegeeks is optimized for learning.© javacodegeeks .
    All Right Reserved and you agree to have read and accepted our term and condition