JPA cache and jpa Cache
JPA Cache
For JPA2.0, cache can be classified into Level 1 cache and level 2 cache (JPA1.0 only supports Level 1 cache ). The second-level cache is usually used to improve the performance of applications. It can avoid accessing data that has been loaded from the database and improve the speed of accessing data objects that have not been modified.
The persistence context is the primary cache of JPA. By storing snapshots of objects in the persistent context, dirty detection can be performed and cached as persistent entities. The first-level cache belongs to the request range level, as shown below:
The JPA second-level cache spans the persistence context and is the global application cache in the true sense.
If the second-level cache is activated, JPA first searches for entities from the first-level cache, and then finds entities from the second-level cache. When the second-level cache is effective, you cannot rely on transactions to protect concurrent data, but on lock policies. For example, you need to manually handle optimistic lock failures after confirming the modification.
Note: The second-level cache can only cache the objects queried through the find or getReference method of EntityManager and the associated entities obtained through the getter method of the object. It cannot cache the data obtained through JPQL query.
The second-level cache is usually used to improve performance. At the same time, using the second-level cache may lead to the extraction of "obsolete" data and concurrent write problems. Therefore, the second-level cache is best used when reading data frequently and updating data less often, instead of using second-level cache for important data.
For different JPA implementation products, the method for enabling Level 2 cache is different. The following uses Spring + Hibernate as an example to enable Level 2 Cache:
1) Add hibernate-ehcache.jar files, it is recommended to use maven to manage;
2) modify the Spring configuration file and enable level-2 caching.
3) mark the object class to be cached @ javax. persistence. Cacheable or org. hibernate. annotations. Cache.
1. @ Cacheable Configuration
Use the @ javax. persistence. Cacheable configuration to enable the second-level cache as follows:
<! -- Jpa -->
<Bean id = "entityManagerFactory" class = "org. springframework. orm. jpa. LocalContainerEntityManagerFactoryBean">
<Property name = "dataSource" ref = "dataSource"/>
<Property name = "persistenceXmlLocation" value = "classpath: META-INF/persistence. xml"/>
<Property name = "persistenceUnitName" value = "template"/>
<Property name = "packagesToScan" value = "org. ssl. template. model"/>
<! --- Specify the JPA adapter -->
<Property name = "jpaVendorAdapter">
<Bean class = "org. springframework. orm. jpa. vendor. HibernateJpaVendorAdapter">
<Property name = "showSql" value = "true"/>
<Property name = "generateDdl" value = "true"/>
</Bean>
</Property>
<Propertyname = "jpaProperties">
<Props>
<Propkey = "hibernate. cache. use_second_level_cache"> true </prop>
<Prop key = "hibernate. cache. region. factory_class"> org. hibernate. cache. ehcache. EhCacheRegionFactory </prop>
<! -- When sharedCache. mode is set to ENABLE_SELECTIVE, add @ Cacheable to the object to be cached. -->
<Prop key = "javax. persistence. sharedCache. mode"> ENABLE_SELECTIVE </prop>
<! -- Output statistics, which must be disabled during deployment -->
<Prop key = "hibernate. generate_statistics"> true </prop>
</Props>
</Property>
</Bean>
Note:
When marking @ cacheable on the u object, you need to set the javax. persistence. sharedCache. mode attribute value;
U javax. persistence. sharedCache. mode
The values can be ENABLE_SELECTIVE (recommended), DISABLE_SELECTIVE, NONE, and ALL;
If it is ENABLE_SELECTIVE, the object added with the @ Cacheable annotation will be cached;
If DISABLE_SELECTIVE is used, the object labeled @ Cacheable (value = false) is cached;
If it is NONE, no entity will be cached, even if it is marked by @ Cacheable;
If it is ALL, the object will be cached, even if there is no @ Cacheable annotation;
U enables Hibernate query Cache
Besides setting
Key = "hibernate. cache. use_query_cache"> true </prop>
In addition, you also need to use @ QueryHint in the JPA query to implement query cache, as shown below:
Public interface DictDao
{
// Query cache through @ QueryHint.
@ QueryHints ({@ QueryHint (name = "org. hibernate. cacheable", value = "true ")})
List <Dict> findAll ();
}
2. @ Cache Configuration
When org. hibernate. annotation. Cache is used for configuration, the settings are as follows:
<! -- Jpa -->
<Bean id = "entityManagerFactory" class = "org. springframework. orm. jpa. LocalContainerEntityManagerFactoryBean">
<Property name = "dataSource" ref = "dataSource"/>
<Property name = "persistenceXmlLocation" value = "classpath: META-INF/persistence. xml"/>
<Property name = "persistenceUnitName" value = "template"/>
<Property name = "packagesToScan" value = "org. ssl. template. model"/>
<! --- Specify the JPA adapter -->
<Property name = "jpaVendorAdapter">
<Bean class = "org. springframework. orm. jpa. vendor. HibernateJpaVendorAdapter">
<Property name = "showSql" value = "true"/>
<Property name = "generateDdl" value = "true"/>
</Bean>
</Property>
<Propertyname = "jpaProperties">
<Props>
<Propkey = "hibernate. cache. use_second_level_cache"> true </prop>
<Propkey = "hibernate. cache. region. factory_class"> org. hibernate. cache. ehcache. EhCacheRegionFactory </prop>
<! -- Output statistics, which must be disabled during deployment -->
<Prop key = "hibernate. generate_statistics"> true </prop>
</Props>
</Property>
</Bean>
When @ cache is used, in addition to marking @ Cache on the object, you also need to add the ehcache. xml configuration file, as shown below:
<Ehcache xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: noNamespaceSchemaLocation = "ehcache. xsd">
<DefaultCache
MaxElementsInMemory = "1000"
Eternal = "false"
TimeToIdleSeconds = "120"
TimeToLiveSeconds = "180"
OverflowToDisk = "false"
MemoryStoreEvictionPolicy = "LRU"
/>
<Cache name = "org. hibernate. cache. internal. StandardQueryCache"
MaxElementsInMemory = "100"
Eternal = "false"
TimeToIdleSeconds = "120"
TimeToLiveSeconds = "180"
OverflowToDisk = "true"/>
<Cache name = "org. hibernate. cache. spi. UpdateTimestampsCache"
MaxElementsInMemory = "100"
Eternal = "false"
TimeToIdleSeconds = "120"
TimeToLiveSeconds = "180"
OverflowToDisk = "false"/>
<! -- Specifies the cache settings for a single object. If this parameter is not set, the default settings are used. -->
<! --
<Cachename = "org. ssl. template. model. Template"
MaxElementsInMemory = "100"
Eternal = "false"
TimeToIdleSeconds = "120"
TimeToLiveSeconds = "180"
OverflowToDisk = "false"
MemoryStoreEvictionPolicy = "LRU"
/>
-->
</Ehcache>
Note:
U Hibernate3.x and Hibernate4.x enable Level 2 cache settings are different
Hibernate4.x:
<Propkey = "hibernate. cache. region. factory_class">
Org. hibernate. cache. ehcache. EhCacheRegionFactory
</Prop>
Hibernate3.x:
<Prop key = "hibernate. cache. provider_class">
Org. hibernate. cache. EhCacheProvider
</Prop>
When you deploy a project, you must disable debugging information, such as disabling SQL statements, updating data tables, and changing the log output level.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.