Hibernate EHCache – Hibernate Second Level Cache

Filed Under: Hibernate

Welcome to the Hibernate Second Level Cache Example Tutorial. Today we will look into Hibernate EHCache that is the most popular Hibernate Second Level Cache provider.

Hibernate Second Level Cache

hibernate second level cache, hibernate ehcache example, hibernate cache, EHCache example

One of the major benefit of using Hibernate in large application is it’s support for cache, hence reducing database queries and better performance. In earlier example, we looked into the Hibernate First Level Cache and today we will look into Hibernate Second Level Cache using Hibernate EHCache implementation.

Hibernate Second Level cache providers include EHCache and Infinispan, but EHCache is more popular and we will use it for our example project. However before we move to our project, we should know different strategies for caching an object.

  1. Read Only: This caching strategy should be used for persistent objects that will always read but never updated. It’s good for reading and caching application configuration and other static data that are never updated. This is the simplest strategy with best performance because there is no overload to check if the object is updated in database or not.
  2. Read Write: It’s good for persistent objects that can be updated by the hibernate application. However if the data is updated either through backend or other applications, then there is no way hibernate will know about it and data might be stale. So while using this strategy, make sure you are using Hibernate API for updating the data.
  3. Nonrestricted Read Write: If the application only occasionally needs to update data and strict transaction isolation is not required, a nonstrict-read-write cache might be appropriate.
  4. Transactional: The transactional cache strategy provides support for fully transactional cache providers such as JBoss TreeCache. Such a cache can only be used in a JTA environment and you must specify hibernate.transaction.manager_lookup_class.

Hibernate EHCache

Since EHCache supports all the above cache strategies, it’s the best choice when you are looking for second level cache in hibernate. I would not go into much detail about EHCache, my main focus will be to get it working for hibernate application.

Create a maven project in the Eclipse or your favorite IDE, final implementation will look like below image.

Hibernate EHCache Example, Hibernate EHCache

Let’s look into each component of the application one by one.

Hibernate EHCache Maven Dependencies

For hibernate second level cache, we would need to add ehcache-core and hibernate-ehcache dependencies in our application. EHCache uses slf4j for logging, so I have also added slf4j-simple for logging purposes. I am using the latest versions of all these APIs, there is a slight chance that hibernate-ehcache APIs are not compatible with the ehcache-core API, in that case you need to check the pom.xml of hibernate-ehcache to find out the correct version to use. Our final pom.xml looks like below.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.journaldev.hibernate</groupId>
	<artifactId>HibernateEHCacheExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<description>Hibernate Secondary Level Cache Example using EHCache implementation</description>

	<dependencies>
		<!-- Hibernate Core API -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>4.3.5.Final</version>
		</dependency>
		<!-- MySQL Driver -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.0.5</version>
		</dependency>
		<!-- EHCache Core APIs -->
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache-core</artifactId>
			<version>2.6.9</version>
		</dependency>
		<!-- Hibernate EHCache API -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>4.3.5.Final</version>
		</dependency>
		<!-- EHCache uses slf4j for logging -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>1.7.5</version>
		</dependency>
	</dependencies>
</project>

Hibernate Second Level Cache – Hibernate EHCache Configuration

Hibernate Second level cache is disabled by default, so we would need to enable it and add some configurations to get it working. Our hibernate.cfg.xml file looks like below.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.password">pankaj123</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost/TestDB</property>
		<property name="hibernate.connection.username">pankaj</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

		<property name="hibernate.current_session_context_class">thread</property>
		<property name="hibernate.show_sql">true</property>
		
		<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
		
		<!-- For singleton factory -->
		<!-- <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>
		 -->
		 
		 <!-- enable second level cache and query cache -->
		 <property name="hibernate.cache.use_second_level_cache">true</property>
		 <property name="hibernate.cache.use_query_cache">true</property>
 		 <property name="net.sf.ehcache.configurationResourceName">/myehcache.xml</property>

		<mapping class="com.journaldev.hibernate.model.Employee" />
		<mapping class="com.journaldev.hibernate.model.Address" />
	</session-factory>
</hibernate-configuration>

Some important points about hibernate second level cache configurations are:

  1. hibernate.cache.region.factory_class is used to define the Factory class for Second level caching, I am using org.hibernate.cache.ehcache.EhCacheRegionFactory for this. If you want the factory class to be singleton, you should use org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory class.

    If you are using Hibernate 3, corresponding classes will be net.sf.ehcache.hibernate.EhCacheRegionFactory and net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory.

  2. hibernate.cache.use_second_level_cache is used to enable the second level cache.
  3. hibernate.cache.use_query_cache is used to enable the query cache, without it HQL queries results will not be cached.
  4. net.sf.ehcache.configurationResourceName is used to define the EHCache configuration file location, it’s an optional parameter and if it’s not present EHCache will try to locate ehcache.xml file in the application classpath.

Hibernate EHCache Configuration File

Our EHCache configuration file myehcache.xml looks like below.


<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
	monitoring="autodetect" dynamicConfig="true">

	<diskStore path="java.io.tmpdir/ehcache" />

	<defaultCache maxEntriesLocalHeap="10000" eternal="false"
		timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
		maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
		memoryStoreEvictionPolicy="LRU" statistics="true">
		<persistence strategy="localTempSwap" />
	</defaultCache>

	<cache name="employee" maxEntriesLocalHeap="10000" eternal="false"
		timeToIdleSeconds="5" timeToLiveSeconds="10">
		<persistence strategy="localTempSwap" />
	</cache>

	<cache name="org.hibernate.cache.internal.StandardQueryCache"
		maxEntriesLocalHeap="5" eternal="false" timeToLiveSeconds="120">
		<persistence strategy="localTempSwap" />
	</cache>

	<cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
		maxEntriesLocalHeap="5000" eternal="true">
		<persistence strategy="localTempSwap" />
	</cache>
</ehcache>

Hibernate EHCache provides a lot of options, I won’t go into much detail but some of the important configurations above are:

  1. diskStore: EHCache stores data into memory but when it starts overflowing, it start writing data into file system. We use this property to define the location where EHCache will write the overflown data.
  2. defaultCache: It’s a mandatory configuration, it is used when an Object need to be cached and there are no caching regions defined for that.
  3. cache name=”employee”: We use cache element to define the region and it’s configurations. We can define multiple regions and their properties, while defining model beans cache properties, we can also define region with caching strategies. The cache properties are easy to understand and clear with the name.
  4. Cache regions org.hibernate.cache.internal.StandardQueryCache and org.hibernate.cache.spi.UpdateTimestampsCache are defined because EHCache was giving warning to that.

Hibernate Second Level Cache – Model Bean Caching Strategy

We use org.hibernate.annotations.Cache annotation to provide the caching configuration. org.hibernate.annotations.CacheConcurrencyStrategy is used to define the caching strategy and we can also define the cache region to use for the model beans.


package com.journaldev.hibernate.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name = "ADDRESS")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY, region="employee")
public class Address {

	@Id
	@Column(name = "emp_id", unique = true, nullable = false)
	@GeneratedValue(generator = "gen")
	@GenericGenerator(name = "gen", strategy = "foreign", 
				parameters = { @Parameter(name = "property", value = "employee") })
	private long id;

	@Column(name = "address_line1")
	private String addressLine1;

	@Column(name = "zipcode")
	private String zipcode;

	@Column(name = "city")
	private String city;

	@OneToOne
	@PrimaryKeyJoinColumn
	private Employee employee;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getAddressLine1() {
		return addressLine1;
	}

	public void setAddressLine1(String addressLine1) {
		this.addressLine1 = addressLine1;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

}

package com.journaldev.hibernate.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;

@Entity
@Table(name = "EMPLOYEE")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY, region="employee")
public class Employee {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "emp_id")
	private long id;

	@Column(name = "emp_name")
	private String name;

	@Column(name = "emp_salary")
	private double salary;

	@OneToOne(mappedBy = "employee")
	@Cascade(value = org.hibernate.annotations.CascadeType.ALL)
	private Address address;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

}

Note that I am using the same database setup as in HQL example, you might want to check that to create the database tables and load sample data.

Hibernate SessionFactory Utility Class

We have a simple utility class to configure hibernate and get the SessionFactory singleton instance.


package com.journaldev.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

	private static SessionFactory sessionFactory;
	
	private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
        	Configuration configuration = new Configuration();
        	configuration.configure("hibernate.cfg.xml");
        	System.out.println("Hibernate Configuration loaded");
        	
        	ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        	System.out.println("Hibernate serviceRegistry created");
        	
        	SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        	
            return sessionFactory;
        }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            ex.printStackTrace();
            throw new ExceptionInInitializerError(ex);
        }
    }
	
	public static SessionFactory getSessionFactory() {
		if(sessionFactory == null) sessionFactory = buildSessionFactory();
        return sessionFactory;
    }
}

Our hibernate second level cache project using Hibernate EHCache is ready, let’s write a simple program to test it.

Hibernate EHCache Test Program


package com.journaldev.hibernate.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.stat.Statistics;

import com.journaldev.hibernate.model.Employee;
import com.journaldev.hibernate.util.HibernateUtil;

public class HibernateEHCacheMain {

	public static void main(String[] args) {
		
		System.out.println("Temp Dir:"+System.getProperty("java.io.tmpdir"));
		
		//Initialize Sessions
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Statistics stats = sessionFactory.getStatistics();
		System.out.println("Stats enabled="+stats.isStatisticsEnabled());
		stats.setStatisticsEnabled(true);
		System.out.println("Stats enabled="+stats.isStatisticsEnabled());
		
		Session session = sessionFactory.openSession();
		Session otherSession = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		Transaction otherTransaction = otherSession.beginTransaction();
		
		printStats(stats, 0);
		
		Employee emp = (Employee) session.load(Employee.class, 1L);
		printData(emp, stats, 1);
		
		emp = (Employee) session.load(Employee.class, 1L);
		printData(emp, stats, 2);
		
		//clear first level cache, so that second level cache is used
		session.evict(emp);
		emp = (Employee) session.load(Employee.class, 1L);
		printData(emp, stats, 3);
		
		emp = (Employee) session.load(Employee.class, 3L);
		printData(emp, stats, 4);
		
		emp = (Employee) otherSession.load(Employee.class, 1L);
		printData(emp, stats, 5);
		
		//Release resources
		transaction.commit();
		otherTransaction.commit();
		sessionFactory.close();
	}

	private static void printStats(Statistics stats, int i) {
		System.out.println("***** " + i + " *****");
		System.out.println("Fetch Count="
				+ stats.getEntityFetchCount());
		System.out.println("Second Level Hit Count="
				+ stats.getSecondLevelCacheHitCount());
		System.out
				.println("Second Level Miss Count="
						+ stats
								.getSecondLevelCacheMissCount());
		System.out.println("Second Level Put Count="
				+ stats.getSecondLevelCachePutCount());
	}

	private static void printData(Employee emp, Statistics stats, int count) {
		System.out.println(count+":: Name="+emp.getName()+", Zipcode="+emp.getAddress().getZipcode());
		printStats(stats, count);
	}

}

org.hibernate.stat.Statistics provides the statistics of Hibernate SessionFactory, we are using it to print the fetch count and second level cache hit, miss and put count. Statistics are disabled by default for better performance, that’s why I am enabling it at the start of the program.

When we run above program, we get a lot of output generated by Hibernate and EHCache APIs, but we are interested in the data that we are printing. A sample run prints following output.


Temp Dir:/var/folders/h4/q73jjy0902g51wkw0w69c0600000gn/T/
Hibernate Configuration loaded
Hibernate serviceRegistry created
Stats enabled=false
Stats enabled=true
***** 0 *****
Fetch Count=0
Second Level Hit Count=0
Second Level Miss Count=0
Second Level Put Count=0
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
1:: Name=Pankaj, Zipcode=95129
***** 1 *****
Fetch Count=1
Second Level Hit Count=0
Second Level Miss Count=1
Second Level Put Count=2
2:: Name=Pankaj, Zipcode=95129
***** 2 *****
Fetch Count=1
Second Level Hit Count=0
Second Level Miss Count=1
Second Level Put Count=2
3:: Name=Pankaj, Zipcode=95129
***** 3 *****
Fetch Count=1
Second Level Hit Count=2
Second Level Miss Count=1
Second Level Put Count=2
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
4:: Name=Lisa, Zipcode=560100
***** 4 *****
Fetch Count=2
Second Level Hit Count=2
Second Level Miss Count=2
Second Level Put Count=4
5:: Name=Pankaj, Zipcode=95129
***** 5 *****
Fetch Count=2
Second Level Hit Count=4
Second Level Miss Count=2
Second Level Put Count=4

As you can see from output, statistics were disabled at first but we enabled it for checking our hibernate second level cache.

Step by step explanation of the output is as follows:

  1. Before we load any data in our application, all the stats are 0 as expected.
  2. When we are loading the Employee with id=1 for the first time, it’s first searched into first level cache and then second level cache. If not found in cache, database query is executed and hence fetch count becomes 1. Once the object is loaded, it’s saved into first level cache and second level cache both. So secondary level hit count remains 0 and miss count becomes 1. Notice that put count is 2, that is because Employee object consists of Address too, so both the objects are saved into second level cache and count is increased to 2.
  3. Next, we are again loading the employee with id=1, this time it’s present in the first level cache. So you don’t see any database query and all other secondary level cache stats also remains same.
  4. Next we are using evict() method to remove the employee object from the first level cache, now when we are trying to load it, hibernate finds it in the second level cache. That’s why no database query is fired and fetch count remains 1. Notice that hit count goes from 0 to 2 because both Employee and Address objects are read from the second level cache. Second level miss and put count remains at the earlier value.
  5. Next we are loading an employee with id=3, database query is executed and fetch count increases to 2, miss count increases from 1 to 2 and put count increases from 2 to 4.
  6. Next we are trying to load employee with id=1 in another session, Since hibernate second level cache is shared across sessions, it’s found in the second level cache and no database query is executed. Fetch count, miss count and put count remains same whereas hit count increases from 2 to 4.

So it’s clear that our Hibernate second level cache; Hibernate EHCache; is working fine. Hibernate statistics are helpful in finding the bottleneck in the system and optimize it to reduce the fetch count and load more data from the cache.

That’s all for the Hibernate EHCache example, I hope it will help you in configuring EHCache in your hibernate applications and getting better performance through hibernate second level cache. You can download the sample project from below link and use other stats data to learn more.

Comments

  1. ShuhaoWen says:

    Thanks Pankaj ,it’s very helpful.

  2. Sergio says:

    Thank you Pankaj for sharing your knowlegde!

  3. anp says:

    Thanks, pankaj good explanation. Keep posting.
    Thanks once again.

  4. Ning says:

    you should set the value of timeToLiveSeconds a littler bigger.

  5. Arun SIngh says:

    this is very helpful

  6. Shashank says:

    too good. Worked for me. Great explanation.

  7. nithin says:

    i am using criteria query or HQL to retrieve a list of values and i am not using query level cache.will second level cache work for it as each time i am trying its hitting the database

  8. Ajay says:

    Hi,
    When I am executing this project I am getting the following error:

    Initial SessionFactory creation failed.java.lang.NoSuchMethodError: javax.persistence.OneToOne.orphanRemoval()Z
    java.lang.NoSuchMethodError: javax.persistence.OneToOne.orphanRemoval()Z
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1836)
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at com.journaldev.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
    at com.journaldev.hibernate.util.HibernateUtil.getSessionFactory(HibernateUtil.java:34)
    at com.journaldev.hibernate.main.HibernateEHCacheMain.main(HibernateEHCacheMain.java:18)
    Exception in thread “main” java.lang.ExceptionInInitializerError
    at com.journaldev.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:29)
    at com.journaldev.hibernate.util.HibernateUtil.getSessionFactory(HibernateUtil.java:34)
    at com.journaldev.hibernate.main.HibernateEHCacheMain.main(HibernateEHCacheMain.java:18)
    Caused by: java.lang.NoSuchMethodError: javax.persistence.OneToOne.orphanRemoval()Z
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1836)
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at com.journaldev.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
    … 2 more

  9. Shambhu says:

    I modified the CacheConcurrencyStrategy.READ_ONLY to CacheConcurrencyStrategy.READ_WRITE.

    But still it is not allowing to update the record.
    I am getting below error

    Exception in thread “main” java.lang.UnsupportedOperationException: Can’t write to a readonly object

  10. anonymous says:

    I wanted to implement caching to an existing project. In the project I can see pom.xml and persistence.xml but no hibernate.cfg.xml in the project. Could you assist on this as I am fairly new to caching and spring and hibernate. Thanks!

  11. RV says:

    very helpful article. I have a doubt. How we can update/flush the contents of second level cache. Suppose if a particular cached entry got updated.

    1. kartik says:

      there is not way u can update , although u can put to Time to live param in ehache cache and if query result doesnt find in cache , if will be loaded.

  12. bablu says:

    how we can Implement second level cache with spring Configuration

  13. Ajit Dandapat says:

    Hi Pankaj,

    Nice explanation regarding ehcache.

    Did you remember me. TCS office Apple project (GR Techpark).

  14. vnp says:

    Thanks for this tutorial.

    By following the tutorial i got cache working for criteria, but it is not working for HQL (though “use_query_cache” is set)

  15. Arvind Ojha says:

    It is nice explanation about second level cache …

  16. Chintan says:

    Hi Pankaj,

    This is really wonderful tutorial for 2nd level caching. It is now pretty clear for me. However when I tried to copy the code and execute it is showing different output:


    Stats Enable: false
    Stats Enable: true
    ********** 0 **********
    Fetch Count: 0
    Second level hit count: 0
    Second level miss count: 0
    Second level put count: 0
    Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.salary as salary3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.emp_address_line1 as emp_address_line2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from employee2 employee0_ left outer join address address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
    1 :: Name: Pankaj, Zipcode: 95129
    ********** 1 **********
    Fetch Count: 1
    Second level hit count: 0
    Second level miss count: 0
    Second level put count: 1
    2 :: Name: Pankaj, Zipcode: 95129
    ********** 2 **********
    Fetch Count: 1
    Second level hit count: 0
    Second level miss count: 0
    Second level put count: 1
    Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.salary as salary3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.emp_address_line1 as emp_address_line2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from employee2 employee0_ left outer join address address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
    3 :: Name: Pankaj, Zipcode: 95129
    ********** 3 **********
    Fetch Count: 2
    Second level hit count: 0
    Second level miss count: 0
    Second level put count: 1
    Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.salary as salary3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.emp_address_line1 as emp_address_line2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from employee2 employee0_ left outer join address address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
    4 :: Name: Lisa, Zipcode: 560100
    ********** 4 **********
    Fetch Count: 3
    Second level hit count: 0
    Second level miss count: 0
    Second level put count: 2
    Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.salary as salary3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.emp_address_line1 as emp_address_line2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from employee2 employee0_ left outer join address address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
    5 :: Name: Pankaj, Zipcode: 95129
    ********** 5 **********
    Fetch Count: 4
    Second level hit count: 0
    Second level miss count: 0
    Second level put count: 2

    Can you please explain me why it is.
    Note: I am using Oracle 11g database. All other things are same as this tutorial.

    1. Ning says:

      you can set the timeToLiveSeconds and timeToIdleSeconds bigger.

  17. Sriram says:

    Hi Pankaj,

    That is really a great piece of explanation and your code just works without any glitch.
    just take the code, put it in the IDE, set up the DB and I was good to go.

    Wonderful piece and just keep that up!

    Regards,
    Sriram

  18. Siva says:

    what if data is updated which is already been cached?
    Eg: There is an update operation on empid -1. Then what happens?

    1. vnp says:

      you should use apt querying technique

  19. Seetesh says:

    failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]

    Any clues on what is going wrong?

    1. Sriram says:

      Hi Seetesh,

      You may have to post the log more in detail so that we could analyse.

      Thanks!
      Sriram

  20. hanan mahmoud says:

    So clear,easy,straight forward example.and the test class really clarify the concept of 2nd level caching

  21. Abhinav says:

    what is region? how it works and how to define region through annotation?

  22. Abhinav says:

    What is region here:

    @Cache(usage=CacheConcurrencyStrategy.READ_ONLY, region=”employee”)

    Is it referring to :

    Why are we using that? if we dont use, then what will happen? and how to define that ‘region’ through annotation?

    Thanks.

  23. sankar says:

    HI Pankaj,

    I’m trying to configure clustered cache with hibernate. I’m able to run the program successfully, but I’m not able to see the cache in Terracotta(EHCache).
    could you please let me know if you have any thoughts on this?

  24. ravinder says:

    Hello bro!
    I am ravinder, working as a software engineer But I am beginner for webServices and Hibernate. So I would like to request to how can configure the webservices and hibernate with eclipse ide.So further what is required things can you send through “URL’S(like jar and war file) ” and along with the “screen shorts “.If you send with example also it’s great.
    Advance,
    Thanks and regards
    ravinder

  25. Vikas Gandham says:

    I have set up 2nd level cache in our application.For the first call of session.get for any object it fetches from the database and for the 2nd call it gets from cache(found out from postgres logs).The issue is if the object has boolean attributes these attributes has null when fetched from cache though its value is true/false.Does any one faced this issue please let me know. Strucked with this for almost 4 days and did not find the solution

  26. Avnish says:

    Thanks Pankaj for a very helpful doc and to understand EHCache.

  27. Ahmet says:

    Thank you so much. God bless you.

  28. sujeet says:

    Thanks Pankaj for detailed explanation

    1. Pankaj says:

      you are welcome sujeet.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages