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中加入以下配置

 

     

                value="org.hibernate.cache.SingletonEhCacheProvider" />
     ehcache.xml" />
     use_second_level_cache" value="true" />
     use_query_cache" value="true" />

 

 

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

     
     


     
           timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"
           clearOnFlush="true">
     

       
      PromotionEntity" maxElementsInMemory="100"

           eternal="false"
           timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"
           clearOnFlush="true">
     
    

 

 

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

你可能感兴趣的:(Hibernate,jpa)