Search code examples
ormjpacachingsecond-level-cacheentitymanager

how to set limitation in EntityManager (JPA) L1 or L2 cache size


How to set L1 or L2 cache size-limitation. I concern of increasing the cache-size. One way is defining timeout for cache but i want to know is it possible to make a constraint for cache size or not?

RGDS Navid


Solution

  • How to set L1 cache size-limitation

    You can't. The only option is to clear the persistence context manually at regular intervals if you want to "control" (actually, clear is very aggressive, it removes all entities) its size.

    How to set L2 cache size-limitation

    This depends on the underlying cache provider. In other words, this is done by configuring the L2 cache implementation. For example, EHCache has a maxElementInMemory parameter.


    what happens in L1?! how much entities will be in the memory as time pass? w/o any constrains ?!

    As much as you put in it, until an eventual OutOfMemoryError, hence the need to clear explicitly:

    • on large batch jobs (even if they occur in a single transaction)
    • if a long-lived EntityManager is used

    But the usual pattern is to use a short-lived EntityManager and most use cases are not batch jobs so this is not a concern.

    See also