In this you will learn about Hibernate Caching Strategies.
Important thing to be remembered while studying this one is, none of the cache providers support all of the cache concurrency strategies.
Useful for data that is read frequently but never updated.
It is Simple.
Best performer among the all.
Advantage of this one is, It is safe for using in a cluster. Here is an example for using the read-only cache strategy.
Used when our data needs to be updated.
It's having more overhead than read-only caches.
When Session.close() or Session.disconnect() is called the transaction should be completed in an environment where JTA is no used.
It is never used if serializable transaction isolation level is required.
In a JTA environment, for obtaining the JTA TransactionManager we must specify the property hibernate.transaction.manager_lookup_class.
To use it in a cluster the cache implementation must support locking.
Here is an example for using the read-write cache stringategy.
Needed if the application needs to update data rarely.
We must specify hibernate.transaction.manager_lookup_class to use this in a JTA environment.
The transaction is completed when Session.close() or Session.disconnect() is called In other environments (except JTA).
Here is an example for using the nonstrict read-write cache stringategy.
It supports only transactional cache providers such as JBoss TreeCache.
It is only used in JTA environment.
For configuring cache the hibernate.cfg.xml file is used. A typical configuration file is shown below.
The name in <property> tag must be hibernate.cache.provider_class for activating second-level cache. We can use hibernate.cache.use_second_level_cache property, which allows you to activate and deactivate the second-level cache. By default, the second-level cache is activated and uses the EHCache.
The <cache> element of a class has the following form:
Usage (mandatory) specifies the caching strategy: transactional, read-write, nonstringict-read-write or read-only.
region (optional) specifies the name of the second level cache region.
include (optional) non-lazy specifies that properties of the entity mapped with lazy="true" may not be cached when attribute-level lazy fetching is enabled.
The <cache> element of a class is also called as the collection mapping.