`
dxp4598
  • 浏览: 81702 次
  • 来自: 上海
社区版块
存档分类
最新评论

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

 

 

分享到:
评论
1 楼 qq362228416 2012-07-27  
正好,试一下

相关推荐

Global site tag (gtag.js) - Google Analytics