cachingjpasecond-level-cachefirst-level-cache

Configuration of Level 1 and Level 2 cache in JPA


I have read the following pages and I have several doubts.

About the persistence context type for Level 1 cache What is the difference between Transaction-scoped Persistence context and Extended Persistence context?

About the Level 2 cache http://www.objectdb.com/java/jpa/persistence/cache

Now, my questions are:

  1. In a normal situation what is the best PersistenceContextType to choose for L1 cache, TRANSACTION or EXTENDED? I suppose the answer is TRANSACTION as it is the default. However I would like to know when should I use EXTENDED.
  2. In a normal situation what are the best values to choose for the following porperties of L2 cache?:
    • javax.persistence.sharedCache.mode (I suppose the answer is ALL as it is the default and caches all the entities)
    • javax.persistence.cache.retrieveMode (I suppose the answer is USE as it is the default and uses the cache on retrieval)
    • javax.persistence.cache.storeMode (I suppose the answer is USE as it is the default, however I still don't understand the difference with REFRESH which seems better for me)

Can someone explain how to correctly put these properties of L1 and L2 correctly and explain when to use some values or others?


Solution

  • NOTE: this answer is not yet complete, I will update with details WRT cache modes

    When working with Java EE, the default persistence context (PC) setting is TRANSACTION. This is also the optimal mode for almost all tasks. Because of it's relatively short lifespan, it has the benefit of being low or zero maintenance.

    I can think of primarily two reasons to prefer an extended EM over a transactional one:

    There are some downsides to using an extended EM

    So if using an extended EM, use it for a single purpose, so you can reason about the contents of the cache more easily.

    I am not sure about the appropriate storeMode and retrieveMode settings yet. As for the storeMode, I have some doubts about their exact function