Radu Toader
Radu Toader

Reputation: 1561

Does Hibernate enable L2 cache by default?

I'm using JPA with Hibernate impl.

Does Hibernate enable L2 cache by default? Or does it need to be explicitly enabled?

Upvotes: 0

Views: 290

Answers (2)

Debojit Saikia
Debojit Saikia

Reputation: 10632

JPA has 2 levels of caching:

The first level of caching is the persistence context. The JPA EntityManager maintains a set of Managed Entities in the Persistence Context. The first-level cache is mandatory and can’t be turned off; it also guarantees object identity inside a transaction.

JPA second level (L2) caching shares entity state across various persistence contexts. Use of the second-level cache is optional. When enabled, entities that are not found in persistence context, will be loaded from L2 cache, if found.

If you are using Hibernate JPA persistence provider, the L2 caching is not configured by default. But the support for second level cache in EclipseLink is turned on by default. Below statements are copied from HERE:

By default EclipseLink uses a shared object cache, that caches a subset of all objects read and persisted for the persistence unit. The EclipseLink shared cache differs from the local EntityManager cache. The shared cache exists for the duration of the persistence unit (EntityManagerFactory, or server) and is shared by all EntityManagers and users of the persistence unit. The local EntityManager cache is not shared, and only exists for the duration of the EntityManager or transaction.

The shared cache can also be disabled. This can be done using the persistence unit property:

<property name="eclipselink.cache.shared.default" value="false"/>

So, if you are using Hibernate persistence provider, you don't need any special configuration because the L2 caching is not configured by default. And if you are using EclipseLink, you can disable it as above.

Upvotes: 1

Adi
Adi

Reputation: 2394

you can control level 2 caching in hibernate, however level 1 caching is mandatory.

follow http://www.tutorialspoint.com/hibernate/hibernate_caching.htm

Upvotes: 0

Related Questions