Question

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:

  1. 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?

  2. Does a query cache save a query text as HQL or as native SQL?

  3. Will 2nd level cache work the same using Hibernate through JPA and Hibernate directly?

  4. 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?

  5. 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!

Was it helpful?

Solution

  1. 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.

  2. 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.

  3. Yes.

  4. Only those that are not in the cache, AFAIK. Test it and see which SQL queries are executed.

  5. 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.

OTHER TIPS

A few more details:

[4]. Query cache works in conjunction to an update timestamps cache. If any instance of an entity type is inserted/deleted/updated, all queries for that entity type are invalidated. So, if any of the entities are gone, all the queries for that entity type are invalidated, so the query will be re-executed. Query cache works this way cos it'd be too expensive for Hibernate to figure out if a particular instance is touched by any of the queries, so it takes a safe, but less optimal, approach. Hence, query cache might only provide performance increase in mostly read-only scenarios.

[5]. Normally, the second level cache is updated in the afterCompletion() Transaction Synchronization callback of the transaction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top