Hibernate automatically Session level apart from the affairs of a cache, the secondary cache are required org.hibernate.cache.CacheProvider interface implementation, Hibernate has a number of cache implementation, developers can configure the use of direct and at the same time to activate the secondary cache, configuration hibernate.cache.use_second_level_cache for true.

Optional values:
  1. org.hibernate.cache.HashtableCacheProvide
  2. org.hibernate.cache.EhCacheProvider
  3. org.hibernate.cache.OSCacheProvider
  4. org.hibernate.cache.SwarmCacheProvider
  5. org.hibernate.cache.TreeCacheProvider ...

For example:
(1). Hibernate.cfg.xml
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property> 

(2) spring
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop> 


Hibernate by default all the entities will not object cache, so, we need to specify what the cache object, the object in the entity mapping file (the corresponding internal tag <class>), add the following configuration:
hibernate.cfg.xml at <cache usage="read-only"/> or add the following configuration:
<class-cache usage="read-only"/>

usage = "read-only" is "read-only" cache strategy.

Note that this tag can be placed only in <cache> tag <class> internal, but must be in front of <id> tag.

Hibernate cache are cached SessionFactory class, which allows multiple Session sharing, use when necessary using a third-party cache components, the new version of Hibernate will EHcache as the default cache implementation.

Cache synchronization strategy: Cache synchronization strategy determines the data objects in the cache access rules, we must specify for each entity class corresponding cache synchronization strategy. Hibernate provides 4 different types of cache synchronization strategy:
1.read-only: read-only. For there will be no change in the data may be used.
2.nonstrict-read-write: If the program is under the concurrent accesses to the data synchronization request is not strict, and data update frequency low, the use of the cache synchronization strategy will be a better performance.
3.read-write: a strict read-write cache. Based on the timestamp to determine the mechanism, implementation of the "read committed" isolation Level. Used for data synchronization requirements, but do not support distributed cache, the practical application of the use of Most of the cache synchronization strategy.
4.transactional: transactional cache, must be running at JTA Service environment. This cache, the cache-related operations are added to the affairs (which is similar to a cache memory database), such as affairs fail, then the buffer pool The data will be rolled back together before the start of the affairs of state. transactional cache implementation of the "Repeatable read" isolation level, would effectively guarantee the legitimacy of data to meet the critical data on the cache, Hibernate built-in cache Only JBossCache Supporting transactional cache.

Implementation of various cache
  1. hibernate.cache.use_minimal_puts: Whether or not optimized to minimize the secondary cache read and write operation, when the cache cluster optimization. Optional values: true (default): Enable minimize read-write operation. false: disable minimize to read and write operations.
  2. hibernate.cache.use_query_cache: Whether or not cache query results (when the conditions of inquiry). Optional values: true: caching query results. false: do not cache query results
  3. hibernate.cache.use_second_level_cache: whether the opening of the secondary cache. Optional values: true: the opening of the secondary cache. false: do not use the secondary cache.
  4. hibernate.cache.query_cache_factory: set up a custom query cache type the full name, the cache must implement the interface org.hibernate.cache.QueryCache. Optional value: (1) org.hibernate.cache.StandardQueryCache (default). (2) custom cache implementation category.
  5. hibernate.cache.region_prefix: prefix the name of the secondary cache.
  6. hibernate.cache.use_structured_entries: Whether or not the use of a structured way the object cache. Optional values: true: a structured way of the cache object. false: do not use a structured way of the cache object.

By-laws: echcache.xml
<?xml version="1.0" encoding="UTF-8"?>   
<ehcache>
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"/>
        
     <cache  name="org.hibernate.cache.StandardQueryCache"       
		  maxElementsInMemory="10000"       
		  eternal="false"       
		  timeToIdleSeconds="200"       
		  timeToLiveSeconds="500"       
		  overflowToDisk="true"/>
		     
     <cache name ="org.hibernate.cache.UpdateTimestampsCache" 
		 maxElementsInMemory ="5000"
		 eternal ="true" 
		 overflowToDisk ="true"/> 
     
</ehcache>

  • maxElementsInMemory property used to specify the cache can hold a maximum number of objects.
  • Whether or not eternal property designated permanent cache.
  • property designated timeToIdleSeconds How long has it been the use of idle cache will remove.
  • property designated cache timeToLiveSeconds length of life.
  • Whether or not the specified property diskPersistent been persistent cache to hard disk, save the path designated by the <diskStore> tag.

Test, log4j.properties
log4j.logger.org.hibernate=warn   
log4j.logger.org.hibernate.cache=debug