How To Enable Second Level Caching In Hibernate
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
- 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
To enable second level cache in the hibernate, then the following 3 changes are required
- Add provider classin hibernate configuration file like…123<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
- Configure cache elementfor a class in hibernate mapping file…1<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