I know how first and second level cache work in hibernate.
But I would like to know
1.when will be the first cache used and when will be the second
The 1st level cache is maintained by the Session or EntityManager, and it's only used during the life of that object. That ensures that if you get/find/retrieve a specific entity more than once during the lifetime of a Session, you'll get the same instance back (or at least a proxy to the same instance).
The 2nd level cache is maintained outside the Session/EntityManager and usually there's a copy of the object, but that is not directly linked (as with objects in the Session).
A word of caution. If you use hibernate in an application that has several instances, the 2nd level cache is not shared amongst instances. For that you need to use a distributed cache (such as terracotta)... if you want consistency amongst the instances of the app. This is not a problem if you are caching static data.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments