Hibernate JPA 中配置Ehcache二级缓存

在Hibernate3 JPA里配置了一下非分布式环境的二级缓存,效果不错。具体过程如下:

 

1, 需要引入的jar包

      http://ehcache.org/downloads/catalog 下载的包里已经包含了简单的例子和javadoc

 

      ehcache-core-2.4.6.jar (必需)

      ehcache-terracotta-2.4.6.jar (必需)

      slf4j-api-1.6.1.jar

      slf4j-jdk14-1.6.1.jar

 

 

2, 在JPA的persistence.xml中加入以下配置

 

      <property name="hibernate.cache.provider_class" 

                value="org.hibernate.cache.SingletonEhCacheProvider" />
     <property name="hibernate.cache.provider_configuration" value="/ehcache.xml" />
     <property name="hibernate.cache.use_second_level_cache" value="true" />
     <property name="hibernate.cache.use_query_cache" value="true" />

 

 

3, 对ehcache进行简单的设置(ehcache.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <ehcache>
      <defaultCache maxElementsInMemory="1000" eternal="false"
           timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"
           clearOnFlush="true">
      </defaultCache>

       <!-- 单独对某个entity的缓存策略设置-->
      <cache name="com.payment.entity.PromotionEntity" maxElementsInMemory="100"

           eternal="false"
           timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"
           clearOnFlush="true">
      </cache>
     </ehcache>

 

 

4, JPA的Entity类中声明缓存的隔离机制

 

       import org.hibernate.annotations.Cache;
       import org.hibernate.annotations.CacheConcurrencyStrategy;

 

      @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
      @Entity
      @Table(name = "catagory")
      public class CatagoryEntity extends BaseEntity { ... }

 

 

5, 如何使用二级缓存中的对象

       在Hibernate中可以通过org.hibernate.Query.setCacheable(true);

       在JPA中,由于EntityManager中得到的javax.persistence.Query没有这个方法了。我们可以通过

       javax.persistence.Query.setHint(”org.hibernate.cacheable”, true);来实现读取二级缓存。

 

 

 

6, 在log4j输出日志中可以看到缓存机制作用

 

      18:05:30,682 DEBUG SessionImpl:265 - opened session at timestamp: 5410486397673472
      18:05:30,682 DEBUG StandardQueryCache:125 - checking cached query results in region:     

          org.hibernate.cache.StandardQueryCache
      18:05:30,682 DEBUG EhCache:74 - key: sql: select promotione0_.id as id2_, 

          promotione0_.catagory_id as catagory6_2_, promotione0_.description as descript2_2_, 

          promotione0_.enabled as enabled2_, promotione0_.name as name2_, promotione0_.picture as

          picture2_, promotione0_.product_id as product7_2_ from promotion promotione0_; parameters:

          ; named parameters: {}
      18:05:30,682 DEBUG StandardQueryCache:183 - Checking query spaces for up-to-dateness:

          [promotion]
      18:05:30,682 DEBUG EhCache:74 - key: promotion
      18:05:30,682 DEBUG EhCache:83 - Element for promotion is null
      18:05:30,682 DEBUG StandardQueryCache:140 - returning cached query results
      18:05:30,714 DEBUG EhCache:74 - key: com.payment.entity.PromotionEntity#1
      18:05:30,714 DEBUG StatefulPersistenceContext:893 - initializing non-lazy collections
      18:05:30,714 DEBUG EhCache:74 - key: com.payment.entity.PromotionEntity#2
      18:05:30,714 DEBUG StatefulPersistenceContext:893 - initializing non-lazy collections
      18:05:30,714 DEBUG EhCache:74 - key: com.payment.entity.PromotionEntity#3
      18:05:30,714 DEBUG StatefulPersistenceContext:893 - initializing non-lazy collections
      18:05:30,714 DEBUG EhCache:74 - key: com.payment.entity.PromotionEntity#4
      18:05:30,714 DEBUG StatefulPersistenceContext:893 - initializing non-lazy collections
      18:05:30,714 DEBUG EhCache:74 - key: com.payment.entity.PromotionEntity#5

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值