You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Hibernate Second Level Cache Example

Hibernate » On Aug 22, 2011 By Sivateja

Let us see the example on this hibernate second level cache.  please go through the concept on this second level cache, still if you have any doubt [ Click here ]

Files required….

  • Product.java  [ Pojo class]
  • ForOurLogic.java
  • Product.hbm.xml
  • ehcache.xml
  • hibernate.cfg.xml

Product.java

 

ForOurLogic.java

 

Product.hbm.xml

 

hibernate.cfg.xml

 

ehcache.xml

Regarding ehcache.xml

  • In ehcache.xml, if eternal=”true” then we should not write timeToIdealSeconds, timeToLiveSeconds,  hibernate will take care about those values
  • So if you want to give values manually better eternal=”false” always,  so that we can assign values into timeToIdealSeconds, timeToLiveSeconds manually, and play ;)
  • timeToIdealSeconds=”seconds” means, if the object in the global chche is ideal, means not using by any other class or object then it will be waited for some time we specified and deleted from the global cache if time is exceeds more than timeToIdealSeconds value
  • timeToLiveSeconds=”seconds” means, the other Session or class using this object or not, i mean may be it is using by other sessions or may not, what ever the situation might be, once it competed the time specified timeToLiveSeconds, then it will be removed from the global cache by hibernate
  • Actually <defaultCache … /> will reflects to all the pojo classes in our application,  and we can also assign the ehcache values to specified pojo class by <cache name=”– your pojo class name —” …….. />

 

 Regarding ForOurLogic.java

  • From line numbers 1622 we opened session1 and loaded object and closed session1, this time object will be loaded from the database as its the first time
  • Then from 2731 we have been waited for 6 seconds,  but in our ehcache.xml we have given timeToIdleSeconds=”5″ , i mean after 5 seconds object will be removed from the global cache
  • And again in ForOurLogic.java line numbers 3541 we opened second session and loaded the object, this time hibernate will loads the object from the database, and closed the session
  • Immediately from 4349 we opened session3 and loaded the object, this time hibernate will loads the object form the global session not from the database

We have existing record in the database

Output after run the ForOurLogic.java

And that’s it mates….!!!!!!!






By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja [at] java4s.com
Follow me on


Comments

45 Responses to “Hibernate Second Level Cache Example”
  1. Alejandra Gilding says:

    I always was interested in this topic and stock still am, appreciate it for posting.

  2. javaDeveloper says:

    very easy to understand

  3. sudheer says:

    man,
    your articles are just outstanding. I will tell you the author even can’t explain so clearly and crystal clear on any concepts. they are just easy to learn and covering every concept in a very simple way.

    Keep it up…

    This is a outstanding blog i had come across till now

    bye
    raj

  4. Java4s says:

    @Sudheer, @javaDeveloper, @Alejandra Gilding

    Thank you so much friends :-)

  5. Mahaboob says:

    Really it is vary easy to understand becuse u explined with arrows

    thats good-to-understand-the out-put

  6. Java4s says:

    @Mahaboob

    Glad to hear your feedback on this article :-)

  7. giri says:

    u r explanation is Fantastic

  8. Java4s says:

    @giri

    Thank you :-)

  9. Koushik says:

    Yes..this is the most easiest way to understand 2nd level caching example…. really appreciable

  10. mohanreddy says:

    i am very thankful to you.outstanding explanation, finally the way of showing output console is very good.

  11. Java4s says:

    @Koushik, @MohanReddy

    You welcome friends, glad to hear your feedback on this :-)

  12. Pavan says:

    this is realy easy to understand …realy nice..way of presentation is good.cover all the small small things.

  13. Vijay Kumar says:

    Very Nice Explanation. Thanks!

  14. Prashanth says:

    Awesome blog!!!! Loved it… Thanks a lot and keep posting various topics on java….

  15. Nagaraj says:

    Awesome man:) i heard about another cache called Query cache,can you provide explanation to it? Have you ever posted on Struts or struts with hibernate configuration?if yes please provide me a link….Thanks a lot,will follow your posts…:)

  16. Malathi says:

    Hey i wanted to know where to store thie ehcache.xml and how is it called in the application.. Thanks in advance. Ur explanations are crystal clear

  17. Java4s says:

    @Malathi

    Please read this article, before you execute the example

    http://www.java4s.com/hibernate/how-to-enable-second-level-caching-in-hibernate/

    We have to store ehcache.xml at class path location.

  18. krish says:

    Super……Explanation…Super…………..

  19. Thabks a lot…This tutorial explains brilliantly and it is easy to understand. You have done a tremendous work. Thank again.

  20. Hats off to this article.

  21. Jamil says:

    Super way to explaining…. Thanks Man.
    You have touched all the point and clear explanation.
    Keep up great job… :)

  22. raju says:

    I like it , its really good

  23. Kumar says:

    This example for only 1 project, what will be if having more than 1 project in my workspace. How i am going to write ehcache.xml file, to saparate the diskStore path.

  24. pradeep381 says:

    Hi,
    How to preLoad few tables during application start up into hibernate cache?

  25. Mahaboob says:

    This is what i am looking for……..

  26. sammy says:

    great way of explanation. Thanks

  27. Anand Singh says:

    Hey superb yar. Awesome work done.

  28. Damo says:

    Nice explanation. keep it up.

  29. Ruchi says:

    Excellent Work.. really appreciate your work. keep on posting !

  30. Ashwin says:

    Can You provide me the example of web application using ehcache…?

  31. Anitha says:

    Anitha
    really awesome explanation

  32. anitha says:

    Awesome blog!!!! Loved it… Thanks a lot and keep posting various topics on java….

  33. prasad says:

    please mention where to put the encache.xml file. I mean in which directory/path.

  34. Dibyendu says:

    This is simply great.You have explained so nicely and cleanly.

  35. Sandeep says:

    Can we rename ehCache.xml and then how hibernate reads it??

  36. Ankur says:

    The best way to explain the Hibernate caching…Cheers.!!!

  37. abc says:

    Very simple and easy to understand example. Thanks a bunch!

  38. prabhakar says:

    please write examples using annotations..its more easy to understand

  39. PRahsant says:

    Awesome thanks example

  40. neeraja says:

    Explanation is very nice.

  41. Narasimha says:

    superb blog

  42. Kiruthiga says:

    Very Nice Blog…

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.