I believe that by using Hibernate's 2nd level cache wisely will make a good job regarding the performance of my application, and to do that I've started studying it from the internet and a Hibernate course. Although there are pretty good explanations on 2nd level cache and basically of the way it works, my goal is to figure out exactly how things work, starting with specific questions I did not find, hence I will ask a few question on Hibernate's cache in general and on 2nd level cache in particular.
Notes about answering:
A. I would appreciate answering questions even if some seem obvious or irrelevant.
B. If a question depends upon cache provider, I'd like to hear the answer which tells about Ehcache
C. Answering part of the questions due to uncertainty will be welcome
Questions:
Once 2nd level cache is configured, does the 1st level cache get disabled? If not then how the process of events occur when trying to get an entity, which cache level gets hit first?
Does a query cache save a query text as HQL or as native SQL?
Will 2nd level cache work the same using Hibernate through JPA and Hibernate directly?
I understood query cache participates with 2nd level cache by hitting the 2nd level cache with ID's which are located in the query cache. What if some of the ID's, from some reason no longer sit in the 2nd level cache, will all the entities be fetched again, or just the part which doesn't exist?
Regarding synchronization – By updating an entity, which is stored in the 2nd level cache, in a certain transaction – When will the entity be updated in the 2nd level cache if at all? Will appreciate further details of how this act may influence on both 2nd level cache and query cache.
Thank you!
No. The first-level cache continues being used. The only differences are that the entities might come from the 2nd-level cache rather than the database, and that they're saved to the 2nd-level cache in addition to the database.
Not as HQL, since Criteria queries can also be cached. I think SQL is used. But this is not the only thing that must be cached: parameters of the query are also cached. You shouldn't care about that though: the cache cache your queries, and whatever it uses doesn't matter as long as executing the same query twice will hit the cache, and executing a non-cached query won't.
Yes.
Only those that are not in the cache, AFAIK. Test it and see which SQL queries are executed.
This depends on the cache concurrency strategy and on the capabilities of the cache. The second-level cache is mainly useful when the entities are read-only, or almost read-only.