2. Example of Second Level Cache SecondFactory holds the second level cache data. It is global for all the session objects and not enabled bydefault. Different vendors have provided the implementation of Second Level Cache : EH Cache Swarm Cache OS Cache JBoss Cache Example of Second Level Cahce using EH Cache To understand the second level cache through example, we need to create following pages: 1. Employee.java 2. employee.hbm.xml 3. hibernate.cfg.xml 4. ehcache.xml 5. FetchTest.java Here, we are assuming, there is emp1012 table in the oracle database containing some records. File: Employee.java 1. package com.javatpoint; 2. 3. public class Employee { 4. private int id; 5. private String name; 6. private float salary; 7. 8. public Employee() {} 9. public Employee(String name, float salary) { 10. super(); 11. this.name = name; 12. this.salary = salary; 13. } 14. //setters and getters 15. }
File: employee.hbm.xml 1. <?xml version='1.0' encoding='UTF-8'?> 2. <!DOCTYPE hibernate-mapping PUBLIC 3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5. 6. <hibernate-mapping> 7. <class name="com.javatpoint.Employee" table="emp1012"> 8. <cache usage="read-only" /> 9. <id name="id"> 10. <generator class="native"></generator> 11. </id> 12. <property name="name"></property> 13. <property name="salary"></property> 14. </class> 15. 16. </hibernate-mapping> Here, we are using read-only cache usage for the class. The cache usage can also be used in collection.
File: hibernate.cfg.xml 1. <?xml version='1.0' encoding='UTF-8'?> 2. <!DOCTYPE hibernate-configuration PUBLIC 3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5. 6. <!-- Generated by MyEclipse Hibernate Tools. --> 7. <hibernate-configuration> 8. 9. <session-factory> 10. <property name="show_sql">true</property> 11. <property name="hbm2ddl.auto">update</property> 12. <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> 13. <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> 14. <property name="connection.username">system</property> 15. <property name="connection.password">oracle</property> 16. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property > 17. 18. <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</prop erty> 19. <property name="hibernate.cache.use_second_level_cache">true</property> 20. 21. <mapping resource="employee.hbm.xml"/> 22. </session-factory> 23. 24. </hibernate-configuration> To implement second level cache, we need to define cache.provider_class property in the configuration file.
File: ehcache.xml 1. <?xml version="1.0"?> 2. <ehcache> 3. <defaultCache 4. maxElementsInMemory="100" 5. eternal="false" 6. timeToIdleSeconds="120" 7. timeToLiveSeconds="200" /> 8. 9. <cache name="com.javatpoint.Employee" 10. maxElementsInMemory="100" 11. eternal="false" 12. timeToIdleSeconds="5" 13. timeToLiveSeconds="200" /> 14. </ehcache> You need to create ehcache.xml file to define the cache property. defaultCache will be used for all the persistent classes. We can also define persistent class explicitely by using the cache element. eternal If we specify eternal="true", we don't need to define timeToIdleSeconds and timeToLiveSeconds attributes because it will be handled by hibernate internally. Specifying eternal="false" gives control to the programmer, but we need to define timeToIdleSeconds and timeToLiveSeconds attributes. timeToIdleSeconds It defines that how many seconds object can be idle in the second level cache. timeToLiveSeconds It defines that how many seconds object can be stored in the second level cache whether it is idle or not.
As we can see here, hibernate does not fire query twice. If you don't use second level cache, hibernate will fire query twice because both query uses different session objects.