Karma-yogi
Karma-yogi

Reputation: 148

Second level Cache in hibernate disabled by default

The session factory Cache needs to be explicitly configured in hibernate to fine tune the query performances.

Is there a relevant reason that can be agreed upon, why first level Cache(session specific) is enabled out of the box in hibernate while the second level Cache (session factory specific) is not ?

Upvotes: 1

Views: 1339

Answers (2)

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23562

First level cache is always enabled, that's how Hibernate works. You might use StatelessSession occasionally to disable the first level cache, but you loose almost all Hibernate features with it.

Second-level cache is disabled by default because the configuration and choice of which entities are cacheable is application specific. For example, if some entities are not good candidates for L2 cache (good candidates are in general entities that are updated rarely and read frequently), then enabling second-level caching for them would degrade the performance significantly.

Upvotes: 2

Ankit
Ankit

Reputation: 1105

It is as per the JPA specifications... Implementation of L2 cache is optional for the JPA providers.. Hibernate and Eclipse Link however implement it, but you need to manually configure that...

Here you can find everything about cache docs.oracle.com/javaee/6/tutorial/doc/gkjio.html

NOTE: One consequence of using a second-level cache in an application is that the underlying data may have changed in the database tables, while the value in the cache has not, a circumstance called a stale read.

Upvotes: 1

Related Questions