Wednesday, December 30, 2015

Hibernate - Caching

Ref:- https://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch06.html

If you have queries that run over and over, with the same parameters, query caching provides performance gains.
Caching introduces overhead in the area of transactional processing. For example, if you cache results of a query against an object, Hibernate needs to keep track of whether any changes have been committed against the object, and invalidate the cache accordingly. In addition, the benefit from caching query results is limited, and highly dependent on the usage patterns of your application. For these reasons, Hibernate disables the query cache by default.
The query cache does not cache the state of the actual entities in the cache. It caches identifier values and results of value type. Therefore, always use the query cache in conjunction with the second-level cache for those entities which should be cached as part of a query result cache.
Hibernate is compatible with several second-level cache providers. None of the providers support all of Hibernate's possible caching strategies. Section 6.2.3, “Second-level cache providers for Hibernate” lists the providers, along with their interfaces and supported caching strategies. For definitions of caching strategies, see Section 6.2.2, “Caching strategies”.
You can configure your cache providers using either annotations or mapping files.
Entities

Set the global default cache concurrency strategy The cache concurrency strategy with thehibernate.cache.default_cache_concurrency_strategy configuration property. See Section 6.2.2, “Caching strategies” for possible values.

Note

When possible, define the cache concurrency strategy per entity rather than globally. Use the@org.hibernate.annotations.Cache annotation.

Example 6.3. Configuring cache providers using mapping files
<cache
    usage="transactional"
    region="RegionName"
    include="all"
/>
Just as in the Example 6.2, “Configuring cache providers using annotations”, you can provide attributes in the mapping file. There are some specific differences in the syntax for the attributes in a mapping file.
usage
The caching strategy. This attribute is required, and can be any of the following values.
  • transactional
  • read-write
  • nonstrict-read-write
  • read-only
region
The name of the second-level cache region. This optional attribute defaults to the class or collection role name.
include
Whether properties of the entity mapped with lazy=true can be cached when attribute-level lazy fetching is enabled. Defaults to all and can also be non-lazy.
Instead of <cache>, you can use <class-cache> and <collection-cache> elements in hibernate.cfg.xml.
read-only
nonstrict read-write
read-write
transactional
CacheInterfaceSupported strategies  
HashTable (testing only) 
  • read-only
  • nontrict read-write
  • read-write
  
EHCache 
  • read-only
  • nontrict read-write
  • read-write
  
OSCache 
  • read-only
  • nontrict read-write
  • read-write
  
SwarmCache 
  • read-only
  • nontrict read-write
  
JBoss Cache 1.x 
  • read-only
  • transactional
  
JBoss Cache 2.x 
  • read-only
  • transactional
  
Syncing or removing a cached item

Determining whether an item belongs to the Session cache
The Session provides a contains() method to determine if an instance belongs to the session cache.

The CacheMode controls how a particular session interacts with the second-level cache.
CacheMode.NORMALreads items from and writes them to the second-level cache.
CacheMode.GETreads items from the second-level cache, but does not write to the second-level cache except to update data.
CacheMode.PUTwrites items to the second-level cache. It does not read from the second-level cache. It bypasses the effect of hibernate.cache.use_minimal_puts and forces a refresh of the second-level cache for all items read from the database.

No comments:

Post a Comment