First, hibernate why provide cache?
What is a cache?
In memory to open a space to the original should have the data inside the hard disk, there is this space inside, in the future, the need for this piece of data directly in memory. This can be simply understood as caching.
Hibernate is a persistent layer framework that accesses physical databases frequently to reduce the frequency of application access to physical data sources, thereby improving application performance.
The data in the cache is a copy of the data in the physical data source, and the application reads and writes data from the cache at run time, at a specific moment or event, synchronizing the cache and the data from the physical data source.
So, let's take a look at Hibernate's caching principle.
Second, Hibernate cache
First, hibernate has a buffer of two levels: primary cache and level two cache. Hibernate cache is also known as session-level caching, and the second-level cache is also known as "Sessionfactory cache." As can be seen from the session, and Sessionfactory, the scope of the buffer is different. Look at the use of both.
1. Buffer level
Hibernate cache is also known as session level caching. The session cache is a transaction-scoped cache, so the life cycle of the primary buffer and the session life cycle are synchronized. The life cycle of a Session object usually corresponds to a database transaction or an application transaction. In the first-level cache, each instance of a persisted class has a unique OID.
In the same session, we used load two times to load the same object and look at the first-level cache:
@Test publicvoid cache1test () {Session s=sessionfactory. Opensession();S. BeginTransaction();Person person= (person) s. Load(Person. Class,1);System. out. println(Person. GetName());Because the session is buffered, the second query takes the person person2= directly in the session (person) s. Load(Person. Class,1);System. out. println(Person2. GetName());S. Gettransaction(). Commit();}
The result is that only one SQL statement is issued, the first query's SQL statement, and the second query directly uses the data object in the session buffer.
This is a primary buffer, can only be used in the same session, can not cross the session to take value, if you need to cross the session value, how to do?
Right, is the sessionfactory level of buffering, like the Sessionfactory management of all sessions, here the two-level buffer jumps out of the session of the small circle, can be used for all the session to take value.
2, level two cache.
The second level cache is also known as the "Sessionfactory cache". Is the sessionfactory level of buffering, because the Sessionfactory object's life cycle and the application's entire process corresponds, so hibernate level two cache is a process-wide cache, there may be concurrency problems, so the need to adopt appropriate concurrent access policies, This policy provides a transaction isolation level for cached data.
The second level cache is optional, is a configurable third-party plug-in, by default sessionfactory does not enable this plugin, if we need to use a level two buffer, it needs to be opened beforehand.
First, turn on caching
We need to open our level two buffer in our hibernate.cfg.xml, as follows:
<!--turn on buffering--> <property name =" Hibernate.cache.use_second_level_cache "; true</property , <!--Specify which level two buffer--> <property name = "Hibernate.cache.provider_class" ; Org.hibernate.cache.ehcacheprovider</property ; <!-- Use query level two buffer--> <propertyname= "Hibernate.cache.use_ Query_cache "; true</property >
Second: indicate that the entity class needs to use buffering
We need to indicate that the entity class needs to use buffering, there are two configurations, one is the XML configuration, the other is the annotation configuration, as follows:
The configuration of the annotation is as follows:
@Entity@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)@Table(name="p_person")publicclass Persion{}
There are two types of XML configuration, one in the respective HBM file configuration, one in CFG,
The HBM files are configured as follows:
<classname="Person" table="t_person"> <cache usage="read-write"/> <idname="id"> ……</class>
The CFG file is configured as follows:
<!-- 指定Student使用二级缓存 --><class-cache class="com.hibernate.Persion" usage="read-only"/>
Third: A third-party cache is required.
In fact, level two buffering is not provided by hibernate, but is provided by a third-party buffer plug-in, usually with the following third-party buffers:
- EhCache: As a process-wide cache, the physical media that holds the data can be either memory or hard disk, which provides support for Hibernate's query caching.
- Oscache: As a process-wide cache, the physical media that holds the data can be either memory or hard disk, providing a rich cache data expiration policy that supports Hibernate's query caching.
- Swarmcache: Can be used as a cluster-wide cache, but does not support Hibernate's query cache.
- JBossCache: Can be used as a cluster-wide cache, supports transactional concurrency access policies, and provides support for hibernate query caching.
The default hibernate is using Ehcache. The default configuration is as follows:
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
To the end of the configuration here, let's test it.
@Test publicvoid Findtesterjihuanchong () {sessions=sessionfactory. Opensession();S. BeginTransaction();personperson= (person) s. Load(Person. Class,1);System. out. println(Person. GetName());S. Gettransaction(). Commit();S. Close(); Another session Sessions2=sessionfactory. Opensession();S2. BeginTransaction();SQL statements are not sent, and the data in the level two cache is read personperson2= (person) s2. Load(Person. Class,1);System. out. println(Person2. GetName());S2. Gettransaction(). Commit();S2. Close();}
The result: Only one SQL statement was sent in the first session, and the second session was not sent.
The above is the first level cache of Hibernate and the introduction of level two cache, in fact Hiberante also has a third kind of cache: query cache. What is a query cache? The difference between a query buffer and a level two cache is explained in the next blog post.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hibernate buffering Policy One