how to enable second level cache by R4R Team

We knows that the First level cache will be enabled by default in Hibernate, but for enable second level cache we need to follow some settings, let us see few points regarding this:

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:

1. Easy Hibernate [EHCache] Cache from hibernate framework
2. Open Symphony [OS] cache from Open Symphony
3. SwarmCache
4. TreeCache from JBoss
 
How to enable second level cache in hibernate:

For enabling the 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:

org.hibernate.cache.EhCacheProvider

Configure cache elementfor a class in hibernate mapping file:

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

1. Lets take an example, we have 2 pojo classes in our application like Student, Employee.
2. 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 in the student mapping file
3. 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 in the student mapping file, if not so hibernate wont stores in the global cache
4. 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 in student mapping file
5. We can avoid this by writing

so remember element has that much importance
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!