Newsletter |
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"> org.hibernate.cache.EhCacheProvider </property>
- 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 .:: | ||
Comments
26 Responses to “How To Enable Second Level Caching In Hibernate”
Please provide the hierarchy of files…
i mean to say where & where the ehcache.xml and mapping.hbm.xml files will be placed…
thanks..
spoon feeding explanation…4me it is vry helpful .tq
Super Explanation….i never saw such type of Explanation…I can’t say in my words…Super……
superrrr i just cant express my words……….
thanks very much yar ,super site i like it
hi sir ,
really java4s.com its very very……..excellent website to learn java technologies as easier. Thank you Java4s.com team .
TOO GOOD. Very very nice example….
Boss ur explanation is mind blowing…all ma doubts regarding cache are got clarified.
1) if i use element in mapping file then only i can perform update operation on Student object right
(or)
2) i can perform update operation using read_only
which is correct.
Really Good Spoon feed……………….
Good Explanation…
Why should we store in Global cache(second level), if first level cache does the work?
Becoz first level cache expires after closing the session.
Very good explanation.
Hello sir,
can you please explain how to use 2-level cache of hibernate in spring +
hibernate application.
Nice and very simple language ,can easy understand ,Thanks a Lot
Hi,
I really appreciate your explanation.
Thanks for making cache clear. ๐
Nice explanation ..
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.
Thanks
Vikas
Such wonderful information about caching mechanism. thanks and more thanks ..
Nice Explation.Simple and clear
Its very helpfull to me.Thank you so much..
Nice Explanation
Nice and very good Explanation …….Thnks
If our application is using annotations then where we need to write <cache usage="read-only" />
Very very useful.