12

I just want some general info about standard purpose of using L1 cache and L2 cache.

I'm curious because I'm investigating the system with terracotta as 2nd level cache and I've found that it also has 1st-level cache.

Roman
  • 64,384
  • 92
  • 238
  • 332

7 Answers7

18

L1 Cache is the cache that exists per Hibernate session, and this cache is not shared among threads. This cache makes use of Hibernate's own caching.

L2 Cache is a cache that survives beyond a Hibernate session, and can be shared among threads. For this cache you can use either a caching implementation that comes with Hibernate like EHCache or something else like JBossCache2

Chandan
  • 3,349
  • 2
  • 22
  • 18
4

In JPA/Hibernate (and other similar ORM tools), the L1 cache is the transactional cache i.e. the entities stored from when you open a transaction to when you close it. This is almost never a shared cache (other threads can't make use of it). In JPA, this would usually be held by the EntityManager.

The L2 cache is a full (typically) shared cache. If you have multiple threads/queries pulling in data, then they can make use of entities that have already been retrieved by other threads that are still live in the cache. In JPA, this would usually be held by the EntityManagerFactory.

GaryF
  • 23,950
  • 10
  • 60
  • 73
3

GaryF is not wrong, but is not technically right :-) Anton is more correct on this, but to complement his answer:

First Level Cache: this is a "cache" which stores all the entities known by a specific session. So, if you have 3 transactions inside this session, it'll hold all entities touched by all three transactions. It gets cleared when you close the session or when you perform the "clear" method.

Second Level Cache: this is a "real" cache and is delegated to an external provider, such as Infinispan. In this cache, you have full control over the contents of the cache, meaning that you are able to specify which entries should be evicted, which ones should be retained longer and so on.

jpkroehling
  • 13,881
  • 1
  • 37
  • 39
  • For the first level cache, what happens when the cache is full? You mentioned two cases where the cache gets cleared but I'm pretty sure it is automatically cleared according to a "last used" or similar algorithm when the cache gets filled up. Otherwise an out of memory exception would occur, or nothing would be storable in cache after that point. – KyleM Mar 28 '13 at 16:56
  • 1
    You might want to check this with the documentation or by experimenting yourself, but as far as I remember, entries on the first-level cache are never evicted. So, if you have a transaction affecting a huge amount of data, you might indeed face an Out of Memory exception (like in Batch operations). For this "edge" case, you might want to use a StatelessSession (http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/StatelessSession.html). – jpkroehling Apr 05 '13 at 06:42
  • `"if you have 3 transactions inside this session"` It is impossible for the separate (not nested transactions) because `Session` is not intend to be used with multiple threads. – v.ladynev Jul 05 '19 at 12:48
0

If Hibernate is anything similar to NHibernate (which it is, except the other way round), the Session is the first-level cache. Except that it is not cache in a general sense, but rather an identity map.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
0

L1 By default enabled, you have to add some third party library like EH cache, Redis for L2.

You can't disable L1 in hibernate.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0

L1:The first-level cache is the cache per Hibernate Session cache and is a mandatory cache through which all requests must pass and this cache is not shared among threads.

L2:The second-level cache can be configured on a per-class and per-collection basis and mainly responsible for caching objects across sessions.L2 Cache is a cache that survives beyond a Hibernate session and can be shared among threads.

A_Gour
  • 21
  • 4
0

As mentioned in this article these are important differences :

First level Cache vs. Second-level Cache in Hibernate Now that we have got some basic understanding of the first level and second level cache, here are some differences between them:

  1. The primary difference is that the first level cache is maintained at the Session level while the second level cache is maintained at the SessionFactory level.

  2. The data stored in the first level cache is accessible to the only Session that maintains it, while the second level cache is accessible to all.

  3. The first level cache is by default enabled while the second level cache is by default disabled.

A couple of things to know about Hibernate's first level cache:

  1. You can use the Session.evict() to remove the loaded entity from the first level cache, can use the refresh() method to refresh the cache, and can use the clear() method to remove all entities in cache.

  2. You cannot disable the first level cache, it is always enabled.

  3. Hibernate entities or database rows remain in cache only until Session is open, once Session is closed, all associated cached data is lost.

Read more: https://www.java67.com/2017/10/difference-between-first-level-and-second-level-cache-in-Hibernate.html#ixzz7B8pzzLzL

mi_mo
  • 135
  • 1
  • 8