Hibernate caching mechanisms
Hibernate in the query data, the first to go search the cache, if found on the direct use, can not find the time will be from the physical data source retrieval, therefore, put frequently used data loaded into the buffer zone, on the can significantly reduce application to visit physical data sources, making the procedure significantly improved operating performance.
Hibernate cache Category:
Session cache, a cache.
SessionFactory cache built into the cache and external cache. Built-in cache SessionFactory objects are stored in a collection of some property that contains the data (mapping elements and the pre-defined SQL statements, etc.), for applications for It is read-only. external cache are stored in a copy of database data, its role and a similar cache. apart from the secondary cache to memory as a storage medium, but also can choose to use external hard disk storage devices.
Hibernate cache scope
Hibernate second level cache and cache are located are located in persistence layer and database data are used to store a copy of The biggest difference is the scope of the cache not the same.
The scope of the cache is divided into 3 categories:
1. Service scope
Affairs of the cache can only be the scope of current affairs to visit, each Service has its own cache, data cache usually the object of interrelated forms. Cached life cycle depends on the services life cycle, only when the Service At the end of the cache until the end of the life cycle. Service-wide use of cache memory as a storage medium, a cache on the scope of Belong to Affairs.
2. Applications
Application cache can be applied in all matters within the scope of shared access. Cached life cycle depends on the application life cycle, only when the application of the end of the cache until the end of the life cycle. Application of the cache can be the use of memory or hard disk as a storage medium, secondary cache on belong to the scope of application.
3. Cluster scope
At the cluster environment, the cache by a machine or multiple machines in the process of sharing the data in the cache is copied to the cluster environment, each process node, inter-process communication through remote to ensure that the data in the cache consistency, cache the data in the usual form of loose objects.
Hibernate's cache management
Level cache management:
evit (Object obj) will be designated a persistent object from the cache to clear, release the memory occupied by the target resource, the specified object from the persistent state into a detached state, thus becoming the object free.
clear () will be a cache of all the persistent object removal, the release of its occupied memory resources
contains (Object obj) to determine whether the specified object exists in a cache.
flush () to refresh the contents of a buffer zone to keep pace with the database data.
Secondary cache management:
evict (Class arg0, Serializable arg1) will be designated a class of persistent object ID from the secondary cache to clear, release the resources occupied by objects.
1.sessionFactory.evict(Customer.class, new Integer(1));
evict (Class arg0) category will be assigned all the persistent object to remove from the secondary cache, the release of its occupied memory resources.
sessionFactory.evict(Customer.class);
evictCollection (String arg0) will be assigned to all categories of persistent object designated collection removed from the secondary cache, the release of its occupied memory resources.
sessionFactory.evictCollection("Customer.orders"); Hibernate configuration of the secondary cache
First of all, not all data fit on the secondary cache, look at what kind of data fit on the secondary cache come from? What kind of data does not fit on the secondary cache come from?
The following situations do not fit into the secondary cache is loaded:
1. Frequently modified data
2. Absolutely does not allow concurrent access of the data appear
3. With other applications to share data
The following kinds of situations has been loaded into the appropriate secondary cache:
1. Data Update frequency low
2. Permit occasional problem complicated by the non-essential data
3. Will not be concurrent accesses data
4. Constant data
5. Will not be third-party modified data
Hibernate cache secondary function is to rely on plug-in to configure the secondary cache of the implementation, Hibernate for integration of these plug-ins, Hibernate provides a org.hibernate.cache.CacheProvider an excuse, it acts as a buffer between the plug and Hibernate adapter.
Secondary cache commonly used plug-ins
EHCache org.hibernate.cache.EhCacheProvider
OSCache org.hibernate.cache.OSCacheProvider
SwarmCahe org.hibernate.cache.SwarmCacheProvider
JBossCache org.hibernate.cache.TreeCacheProvider
EHCache a brief profile
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Set the L2 cache EHCache Provider class of plug-ins -->
<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
# <ehcache>
<!-- maxElementsInMemory To cache the maximum number of objects , eternal Settings are never expired ,timeToIdleSeconds The object is in the idle state of the maximum number of seconds ,timeToLiveSeconds The 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 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 read concurrent access policy -only read-write nonstrict-read-write transactional-->
<cache usage="read-write"/>
</class>
</hibernate-mapping>
Related Posts of Hibernate caching mechanisms
-
Hibernate easy extract
Simple terms the following excerpt from hibernate: DAO mode, the database access layer implementation is hidden to the Data Accessor, the front said, DAO mode are in fact a combination of two modes, namely Data Accessor and domain Object mode. What i ...
-
What is the JPA
Same, JDO, also started compatible JPA. At the field of ORM, it seems that JPA is a benevolent government, is the normative specification. At the support of major manufacturers, JPA use became widespread. 2 Spring Spring + Hibernate often referred to ...
-
Choose Hibernate or iBATIS has its reasons
Hibernate features: Hibernate powerful database has nothing to do with good, O / R mapping ability, and if you are very proficient in Hibernate, but also for Hibernate to conduct an appropriate package, then your project will be the entire persistenc ...
-
Hibernate primary key strategy-sequence
Today, the use of hibernate in the company encountered a troublesome problem, the use of hibernate when the primary key generation strategy set sequence, but always reported in the implementation could not get next sequence value of the error, then o ...
-
Hibernate pessimistic locking mechanism for locking and optimistic locking
hibernate lock mechanism 1. Pessimistic lock It refers to the modification of data by outsiders hold a conservative attitude. The assumption that at any time access to data, may also have another client to access the same data, in order to maintain t ...
-
What is the appfuse
First, Appfuse brief introduction Matt Raible are Appfuse developed a guiding entry-level J2EE framework, how to integrate its popular Spring, Hibernate, ibatis, struts, Xdcolet, junit, etc. give the basic framework of the model, the latest version 1 ...
-
Nan-Jing 5: When IBatis.Hibernate mixed affairs, pay attention to your SQL
[Problem] Now, in the development of the so-called multi-storey JavaEE applications, data persistence layer is always essential, and "Automatic" of ORM - Hibernate, and "all-manual-type" of SqlMap - IBatis, equivalent data are Per ...
-
Hibernate secondary cache
Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...
-
The level Hibernate cache
Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...













Leave a Reply