Second Level OSCache example in Hibernate

This tutorial will sow how we can configure second level cache using OSCache in Hibernate step by step. We know that there are three types of caching mechanism in Hibernate such as First Level – Session, Second Level – SessionFactory and Query Level – SessionFactory.

For more information on First Level, Second Level and Query Level please go through Hibernate Caching strategy.

MySQL Database 5.5
JDK 1.6
MySQL Connector jar
Hibernate jars
Hibernate OSCache jar files

Create a MySQL table called cd


Insert some data into the table


Create POJO and hibernate mapping file for the database table


The above pojo file is simple and easy to use. It just declares the attributes same as in the database table.

mapping file – CD.hbm.xml


The mapping file is simple and easy to understand. In the above mapping file I have used


use this strategy for read-mostly data where it is critical to prevent stale data in concurrent transactions,in the rare case of an update.

Now we need to configure hibernate to connect the database table as well as to use the second level cache for OSCache. Below is the hibernate.cfg.xml file


If you need the description for each of the properties in hibernate.cfg.xml file then you can go through EHCache in Hibernate.

Notice in the above configuration file, the cache provider class is different from what we used for EHCache in Hibernate.

Now we will create file which will give us the Singleton SessionFactory.


The above class is simple and easy to understand.

Next we write the for testing our work.


Now we can configure OSCache in two waysIn Memory and Physical Cache.

In Memory Cache

This is used when we don’t use any file for cache configuration. So in this case the Hibernate gets shut down everytime, memory is flushed and second level cache is built on next run again. Hence you will get the same output again and again while you run the application.


At first run


At Second run


Physical Cache

In case of Physical Cache we configure the caching mechanism using file. So if we have a large dataset to cache then Physical Cache comes into the picture and we can save the cache physically into the disk space of the computer. For example we can save into C or D or any other drive in the Windows OS. So in this case we won’t get the same result into the console output. First time only the data are retrieved from the database, second time onwards the data are retrieved from the cache for the same query.


In the above file there are few properties which we will see what are these properties

cache.memory : whether cache should be saved into memory, possible values are true/false.
cache.persistence.class : persist the cache data to disk
cache.path : where to store the cache in disk
cache.algorithm : which cache algorithm should be used. For more info


At First run


At Second run


You can find the generated cache file at C:\temp\cache\application.

For more information on output Fetch count, Miss count or Put count you can refer to the output of EHCache in Hibernate.

If you want to use maven based project then here is the pom.xml file


Thanks for your reading. Please do not forget to leave a comment.

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.