[Change] Hibernate caching mechanism

2010-03-31  来源:本站原创  分类:Java  人气:211 

Cache is located in the application and physical data sources, for temporary storage of memory copy of data, the purpose is to reduce the application of the visits of physical data sources to enhance the application's operating performance.
Hibernate query data, the first to go find the cache, if found to be used directly, can not find the time will be retrieved from a physical data source, so the frequent use of the data loaded into the cache, you can greatly reduce application of the physical data sources, making the program significantly improve operating performance.

Hibernate cache Category:

Session cache, a cache.

SessionFactory cache is divided into built-in cache, and external cache. Built-in cache is stored in some collection of attributes SessionFactory object contains data (mapping elements and, according to a predefined SQL statements, etc.), for the application, it is only Reading. external cache data stored in the database, a copy of its role and a similar cache. 2 cache in addition to memory as a storage medium, it can use hard drives and other external storage devices.

Hibernate cache scope

Hibernate in a cache and secondary cache are located are located in the persistence layer and database data are used to store a copy of, the biggest difference is the scope of the different cache.

The scope of the cache is divided into three categories:

1. Cache scope of services range of services can only be access to the current transaction, each transaction has its own cache, the cache data inside the object usually interrelated forms. Cache of the life cycle depends on the transaction life cycle, and only when the transaction the end of the life cycle of the cache will end. Service-wide use of memory as the storage media cache, a cache belonging to the scope of services.
2. Application cache application can be applied to all matters within the scope of shared access. Cached life cycle depends on the application life cycle, only when applied at the end of the life cycle before the end of the cache. Applications can use the cache memory or hard disk as a storage medium, the secondary cache on the fall application.
3. Cluster range of cluster environment, the cache is a machine or multiple machines in the process of sharing, the cache data is copied to the cluster environment, each process node, inter-process communication to ensure the remote data cache line , the cache data objects commonly used form of loose data.

Hibernate's cache management

Level cache management:

evit (Object obj) to the specified persistent object removed from a cache, the release of the memory object resources specified object from the persistent state into a detached state, thus becoming free object.
clear () to a cache of all persistent objects removed, release the memory resources of its
contains (Object obj) check whether the specified object exists in a cache.
flush () to refresh a cache of content to make it sync with the database data.

2 cache management:

evict (Class arg0, Serializable arg1) to a specified class ID of the persistent object cache from 2 to clear, release the resources occupied by the object.

Java code

  1. sessionFactory.evict (Customer. class, new Integer (1));

sessionFactory.evict(Customer.class, new Integer(1));

evict (Class arg0) all of the specified class of persistent objects removed from the secondary cache, the release of its occupation of the memory resources.

Java code

  1. sessionFactory.evict (Customer. class);

sessionFactory.evict(Customer.class);

evictCollection (String arg0) all of the specified class of the specified set of persistent objects removed from the secondary cache, the release of its occupation of the memory resources.

Java code

  1. sessionFactory.evictCollection ("Customer.orders");

sessionFactory.evictCollection("Customer.orders");

Hibernate second-level cache configuration

First, not all data fits in the secondary cache, look at what kind of data to fit on the secondary cache? What kind of data do not fit secondary to the cache?
The following is not suitable for these types of cases loaded into the secondary cache:
1. Frequently modified data
2. Absolutely does not allow for concurrent access to data
3. And other applications to share data in the following types of cases that have been loaded into the secondary cache in the right:
1. Low frequency of data update
2. To allow occasional non-critical data concurrency issues
3. Will not be concurrent access to data
4. Constant data
5. Will not be third-party modification of data

Hibernate second-level cache functionality is configured by plug-ins to achieve the secondary cache, Hibernate to integrate these plug-ins, Hibernate provides a org.hibernate. Cache.CacheProvider excuse, it acts as a buffer between the adapter plug and Hibernate.

Commonly used secondary cache plug-in
EHCache org.hibernate. Cache.EhCacheProvider
OSCache org.hibernate. Cache.OSCacheProvider
SwarmCahe org.hibernate. Cache.SwarmCacheProvider
JBossCache org.hibernate. Cache.TreeCacheProvider

The configuration of a brief EHCache
hibernate. cfg.xml

Xml Code

  1. <Hibernate-configuration>
  2. <Session-factory>
  3. <! - Set 2 cache plug-in EHCache the Provider class ->
  4. <Property name = "hibernate. Cache.provider_class">
  5. org.hibernate. cache.EhCacheProvider
  6. </ Property>
  7. <! - Start "query cache" ->
  8. <Property name = "hibernate. Cache.use_query_cache">
  9. true
  10. </ Property>
  11. </ Session-factory>
  12. </ Hibernate-configuration>

<hibernate
-configuration>
    <session-factory>
       <!--  Set the L2 cache plugin EHCache  Provider Class  -->
       <property name="hibernate
.cache.provider_class">
          org.hibernate
.cache.EhCacheProvider
       </property>
       <!--  Start the query cache  " -->
       <property name="hibernate
.cache.use_query_cache">
          true
       </property>
    </session-factory>
  </hibernate
-configuration>

ehcache.xml

Xml Code

  1. <Ehcache>
  2. <! - MaxElementsInMemory maximum number of objects to cache, eternal forever, but settings are on, timeToIdleSeconds object is idle for the most number of seconds, timeToLiveSeconds object in a state of maximum number of seconds the cache ->
  3. <DiskStore path = "java.io.tmpdir" />
  4. <DefaultCache maxElementsInMemory = "10000" eternal = "false" timeToIdleSeconds = "300" timeToLiveSeconds = "600" overflowToDisk = "true" />
  5. </ Ehcache>

<ehcache>
  <!-- maxElementsInMemory To cache an object, the maximum number of eternal settings are never expired  ,timeToIdleSeconds The object is in the idle state of the maximum number of seconds, the timeToLiveSeconds object is in the cache status of the maximum number of seconds   -->
  <diskStore path="java.io.tmpdir"/>
    <defaultCache maxElementsInMemory="10000" eternal="false"  timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true"/>
</ehcache>

****. Hbm.xml

Xml Code

  1. <? Xml version = "1.0" encoding = 'UTF-8'?>
  2. <! DOCTYPE hibernate-mapping PUBLIC
  3. "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN"
  4. "Http://hibernate .sourceforge.net / hibernate-mapping-3.0.dtd">
  5. <Hibernate-mapping>
  6. <Class>
  7. <! - Set the persistent classes of secondary cache concurrency strategy for read-only read-write nonstrict-read-write transactional ->
  8. <Cache usage = "read-write" />
  9. </ Class>
  10. </ Hibernate-mapping>

<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate
-mapping PUBLIC
                            "-//Hibernate
/Hibernate
 Mapping DTD 3.0//EN"
                            "http://hibernate
.sourceforge.net/hibernate
-mapping-3.0.dtd" >

<hibernate
-mapping>

   <class>
       <!--  Set the persistent classes of L2 cache concurrent access policy   read-only read-write nonstrict-read-write transactional-->
       <cache usage="read-write"/>
   </class>

</hibernate
-mapping>
相关文章
  • [Change] Hibernate caching mechanism 2010-03-31

    Cache is located in the application and physical data sources, for temporary storage of memory copy of data, the purpose is to reduce the application of the visits of physical data sources to enhance the application's operating performance. Hibernate

  • Hibernate caching mechanism 2010-03-29

    Cache is between applications and between the physical data sources, whose role is to reduce the application of the physical data source access frequency, thus improving application performance. Caching of data is the physical data source, data repli

  • Hibernate caching mechanism (reproduced) 2010-03-21

    Cache is between applications and between the physical data sources, whose role is to reduce the application of the physical data source access frequency, thereby improving application performance. Caching of data is the physical data source, data re

  • Hibernate caching mechanism (2) 2010-03-29

    Why hibernate 1, for JDBC operations to provide packaging for easy operation to simplify database access code (connection, eparedStatement, resultSet); 2, to simplify the amount of code DAO layer ORM from relational DB to object-oriented (java) chang

  • Hibernate --- caching mechanism 3 (2 cache configuration) 2010-03-27

    2.4, secondary cache configuration using the EhCache 2.4.1, configuration ready 1) the ehcache-1.2.3.jar to the classpath of the current application. 2) In the hibernate.cfg.xml file in the cache plug-in to provide added EhCache class. <!-- Configure

  • Hibernate --- caching mechanism 4 (1, 2 cache more) 2010-03-27

    1. A comparison of cache and 2 cache: Hibernate provides two Cache, first-level cache is a Session-level cache, which cache belongs to the scope of services. This level of cache by the hibernate managed without the need for intervention under normal

  • Hibernate caching mechanism (transfer) 2010-10-14

    Cache is located in the application and physical data sources for the copy data temporary storage area of memory that aims to reduce application access to the physical number of data sources to improve the operating performance of the application. Hi

  • Hibernate caching mechanism - Summary 2010-11-04

    Cache: a temporary storage area of memory copy of data (Hibernate persistent object and the ID sub-list) Cache role: query data, the first to the cache to find, to reduce access to SQL database > Cache, secondary cache and query cache are open when t

  • Hibernate caching mechanism work 2010-12-21

    Cache is located in the application and physical data sources for the copy data temporary storage area of memory that aims to reduce application access to the physical number of data sources to improve the operating performance of the application. Hi

  • [Change] Hibernate ---- pessimistic locking and optimistic locking. Hibernate locking mechanism 2009-09-26

    [Change] Hibernate ---- pessimistic locking and optimistic locking, Hibernate from the locking mechanism: http://www.blogjava.net/pengpenglin/archive/2010/03/15/315444.html 【1】 optimistic locking (Optimistic locking) version of the strategy adopted:

  • Understand Session caching mechanism to manipulate persistent objects 2011-07-13

    Hibernate provide us with the main interface to manipulate the database, Session is one that provides the basic add, delete, change, methods, but also has a caching mechanism, in accordance with a certain point in time, according to the persistent ca

  • IBatis the caching mechanism of the Select Query Cache 2010-03-29

    IBatis the caching mechanism of the Select Query Cache Can press the following code in your SqlMap.xml where configuration is as follows: <cacheModel imlementation="LRU" readOnly="true" serialize="true"> <flushInterv

  • ibatis caching mechanism of 2009-03-25

    ibatis caching mechanism of 1, IBATIS caching mechanism to use 1, sqlMapConfig.xml configured Xml Code 1.SqlMapConfig.xml in <settings cacheModelsEnabled = "true" / / set to true enhancementEnabled = "true" lazyLoadingEnabled = &quo

  • Created new caching mechanism to easily add cache support 2010-05-04

    Original Address: http://www.jroller.com/RickHigh/entry/created_new_caching_mechanism_to Created new caching mechanism to easily add cache support without writing a lot of additional code. You just annotated the methods that you want to cache with: @

  • Caching mechanism IBatis 2010-11-24

    Caching mechanism, also based on the Key-Value, identifying the Key to a good understanding of the circumstances surrounding the life cycle of the cache. Speaking from the configuration file parsing: parser.addNodelet ("/ sqlMap / cacheModel", n

  • uchome product analysis uchome caching mechanism (from http://www.woniu.us/) 2010-12-02

    Cache is divided into "template file cache", "Module Data Cache", "System Configuration Cache" and "other cache" Template file caching mechanism (using methods from the top-down analysis) 1: to display the page with

  • Product analysis uchome uchome caching mechanism (from http://www.woniu.us/) 2010-12-02

    Cache is divided into "template file cache", "Module Data Cache", "System Configuration Cache" and "other Cache" Template file caching mechanism (from top-down approach used to analyze) 1: To display the page includ

  • Hibernate's caching mechanism 2010-03-29

    Cache is between applications and between the physical data sources, whose role is to reduce the application of the physical data source access frequency, thereby enhancing the application Operating performance. Caching of data is the physical data s

  • Hibernate --- A caching mechanism 2010-03-27

    1. Persistence layer of the scope of the cache Determine the scope of the cache life cycle of the cache and can access whom. The scope of the cache is divided into three categories. 1: Service range: the cache can only be access to the current transa

  • [Change] hibernate a difference between the cache and secondary cache 2010-04-06

    Cache is between the application and physical data sources, whose role is to reduce the application of the physical data source access frequency, thereby improving the operating performance of the application. De data in the buffer is the physical da