JPA second level cache

本文详细介绍了JPA2.0标准的第二级缓存实现,包括Hibernate内置缓存解决方案(HashTable、Ehcache、Infinispan),如何启用缓存及配置缓存提供者(如使用Ehcache作为后端缓存提供商)。通过示例代码演示了如何在实体类中使用@Cacheable注解来启用缓存,并提供了测试代码以验证缓存效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#JPA second level cache

JPA 2.0 standardized the Hibernate second level cache and introduced a new cache API.

There are 3 built-in cache implementation shipped with Hibernate 4, HashTable, Ehcache, Infinispan. HashTable is a in-memory solution, it is useful to test purpose. Ehcache is one of the most popular cache solutions, Hibernate provides integration with Ehcache. Infinispan is also from JBoss.org community, it is the base of JBoss's next-generation cache and datagrid solution, infinispan is the recommended cache solution if you are using Hibernate/JPA in JBoss application servers.

##Enable Cache

By default, JPA 2nd level cache is disabled.

You must specify the value of javax.persistence.sharedCache.mode property or shared-cache-mode element in persistence.xml file.

<pre> javax.persistence.sharedCache.mode=ENABLE_SELECTIVE </pre>

or

<pre> &lt;shared-cache-mode>ENABLE_SELECTIVE&lt;/shared-cache-mode> </pre>

There are four optional values, ALL, ENABLE_SELECTIVE, DISABLE_SELECTIVE, NONE.

ALL will enable cache for all entities, ENABLE_SELECTIVE and DISABLE_SELECTIVE should be used with the JPA specific @Cacheable annotation. The former will enable cache for the entities annotated with @Cacheable or @Cacheable(true). The later will DISABLE cache for the entities annotated with @Cacheable(false) and enable cache for other entities. NONE will disable cache for all entities.

Configure Hibernate cache provider

Now you need configure some properties for Hibernate specific cache provider.

An example of using Ehcache as backend cache provider.

<pre> hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=true hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory </pre>

hibernate.cache.use_query_cache is use for enable cache for JPA query result.

It also require you add extra dependency in your pom.xml file.

<pre> &lt;dependency> &lt;groupId>org.hibernate&lt;/groupId> &lt;artifactId>hibernate-ehcache&lt;/artifactId> &lt;/dependency> </pre>

##Test codes

Add @Cacheable(true) to the entity class, we are using the ENABLE_SELECTIVE cache strategy and must apply this annotation to enable cache.

<pre> @Entity @Cacheable(true) //@Cache(usage=CacheConcurrencyStrategy.READ_ONLY, region="conf_region") public class Conference { } </pre>

Hibernate also provides specific @Cache annotation to tune the cache settings. Please read the official Hibernate Document for more detailed info.

Write some codes for test purpose and enable the logging info.

<pre> @Test @Transactional public void retrieveConference() { log.debug("============beginning of calling retrieveConference==========="); final Long id = 1L; Conference conference = conferenceDao.findById(id); assertTrue(conference != null); assertTrue("JUD2013".equals(conference.getName())); Conference conference2 = conferenceDao.findById(id); assertTrue(conference == conference2); log.debug("============end of calling retrieveConference==========="); } </pre>

You will find the entity object is cached after it is persisted, and the later queries will not hint database at all.

##Sample codes

The codes are hosted on my Github account.

https://github.com/hantsy/spring-sandbox

I also tried to add sample settings and codes for Infinispan, but failed to make it worked in the standalone test.

转载于:https://my.oschina.net/hantsy/blog/188726

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值