How To Enable Second Level Caching In Hibernate

Hibernate » on Aug 18, 2011 { 26 Comments } By Sivateja

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
  • When ever 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


Changes required

To enable second level cache in the hibernate, then the following 3 changes are required

  • Add provider classin hibernate configuration file like…
    <property name="hibernate.cache.provider_class">
  • Configure cache elementfor a class in hibernate mapping file…
    <cache usage="read-only" />

    Note: this must write soon after <class>

  • create xml file called ehcache.xml and store in at class path location [ no confusions, i mean in the place where you have mapping and configuration XML’s ] in web application.


Important points on this second level cache

Lets take an example, we have 2 pojo classes in our application like Student, Employee.

  • If we load student object from the database, then as its the first time hibernate will hits the database and fetch this student object data and stores in the session1 cache memory [ First level cache ], then in the global cache [ second level cache ] provided if we write <cache usage=”read-only” /> in the student mapping file
  • I mean hibernate will stores in the local session memory by default, but it only stores in the global cache [ second level cache ] only if we write <cache usage=”read-only” /> in the student mapping file, if not so hibernate wont stores in the global cache
  • Now take another session like session 2 for example, if session 2 also load the student object then hibernate will loads from the global cache [ second level cache ] as student object is available at global [Actually when ever we want to load any object hibernate first will checks at local, then global then database right hope you remembered this ], now if session 3 modify that student object then hibernate will thorows an error because we have written <cache usage=”read-only” /> in student mapping file
  • We can avoid this by writing <cache usage=”read-write” />
  • so remember <cache /> element has that much importance


Mates, still i have not explained about ehcache.xml did you observe ๐Ÿ™‚ย  i will explain about this by taking one example on this hibernate second level cache in the next session

โ€‹ โ€‹โ€‹

You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


26 Responses to “How To Enable Second Level Caching In Hibernate”
  1. Mohammed Vaseem says:

    Please provide the hierarchy of files…
    i mean to say where & where the ehcache.xml and mapping.hbm.xml files will be placed…


  2. manjesh says:

    spoon feeding explanation…4me it is vry helpful .tq

  3. krish says:

    Super Explanation….i never saw such type of Explanation…I can’t say in my words…Super……

  4. kiran says:

    superrrr i just cant express my words……….

  5. thanks very much yar ,super site i like it

  6. hi sir ,
    really its very very……..excellent website to learn java technologies as easier. Thank you team .

  7. Gootam says:

    TOO GOOD. Very very nice example….

  8. Ramjeevan says:

    Boss ur explanation is mind blowing…all ma doubts regarding cache are got clarified.

  9. Muniswamy says:

    1) if i use element in mapping file then only i can perform update operation on Student object right
    2) i can perform update operation using read_only

    which is correct.

  10. Kathiravan J says:

    Really Good Spoon feed……………….

  11. Rahul says:

    Why should we store in Global cache(second level), if first level cache does the work?

  12. Priyank says:

    Very good explanation.

  13. brijesh says:

    Hello sir,

    can you please explain how to use 2-level cache of hibernate in spring +

    hibernate application.

  14. Nageswara Rao says:

    Nice and very simple language ,can easy understand ,Thanks a Lot

  15. Gaurav says:


    I really appreciate your explanation.

    Thanks for making cache clear. ๐Ÿ™‚

  16. pradip patil says:

    Nice explanation ..

  17. Vikas says:

    Hi java4s,
    Please tell me during caching mechanism if I have alter a record from the table that is also present in local cache and now I have been making some modifications on that record from sql developer and my application is running so what should be the behaviour when I load the object using load() method. Do it return the old object or it will check into the database first.


  18. sanjeev says:

    Such wonderful information about caching mechanism. thanks and more thanks ..

  19. Lalitha says:

    Nice Explation.Simple and clear

  20. Ramakrisnna says:

    Its very helpfull to me.Thank you so much..

  21. Maruti says:

    Nice Explanation

  22. Sandeep says:

    Nice and very good Explanation …….Thnks

  23. veerendar says:

    If our application is using annotations then where we need to write <cache usage="read-only" />

  24. bhargavi vasamsetti says:

    Very very useful.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.