Second Level Cache:
First level cache
will be enabled by default, but for enable second level cache we need to follow
some settings, let us see few
points regarding this..
·
Second level cache was introduced in hibernate 3.0
·
Whenever we are loading any object from
the database, then
hibernate verify whether that object is available in the local cache memory of that
particular session [ means first
level cache ], if not available
then hibernate verify whether the object is available in global cache or factory cache [second level cache ], if not available
then hibernate will hit the database and loads the object from there, and then first stores in the local cache of
the session [ first level ] then in the global cache [ second level cache ]
·
When another session need to load the same
object from the database, then hibernate copies that object from global cache [ second level cache
] into the local cache of this new session
Second level cache in
the hibernate is of from 4 vendors…
·
Easy Hibernate [EHCache] Cache from hibernate framework.
·
Open Symphony [OS] cache from Open Symphony.
·
SwarmCache.
·
TreeCache from JBoss.
How to enable
second level cache in hibernate
We need one provider class, here we are going to see hibernate
provider class that is EHCache.
Example of Second Level Cache using EH Cache
To
understand the second level cache through example, we need to create following
pages:
- Employee.java
- employee.hbm.xml
- hibernate.cfg.xml
- ehcache.xml
- 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</property>
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.
File: FetchTest.java
1.
package com.javatpoint;
2.
3.
import org.hibernate.Session;
4.
import org.hibernate.SessionFactory;
5.
import org.hibernate.cfg.Configuration;
6.
7.
public class FetchTest {
8.
public static void main(String[] args) {
9.
Configuration cfg=new Configuration().configure("hibernate.cfg.xml");
10. SessionFactory factory=cfg.buildSessionFactory();
11.
12. Session session1=factory.openSession();
13. Employee emp1=(Employee)session1.load(Employee.class,121);
14. System.out.println(emp1.getId()+" "+emp1.getName()+" "+emp1.getSalary());
15. session1.close();
16.
17. Session session2=factory.openSession();
18. Employee emp2=(Employee)session2.load(Employee.class,121);
19. System.out.println(emp2.getId()+" "+emp2.getName()+" "+emp2.getSalary());
20. session2.close();
21.
22. }
23. }
Output:
As we
can see , 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.
1 comment:
I love this. Im always keeping this idea in mind. Thankyou for this blog.
Hibernate Training in Chennai
Spring Training in Chennai
Spring and Hibernate Training in Chennai
Core Spring Training
Spring source Training
Spring and Hibernate Training
Struts Training in Chennai
Wordpress Training in Chennai
Post a Comment