在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>

<!-- 
maxElementsInMemory:缓存中最大允许创建的对象数
eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期
timeToIdleSeconds:缓存数据钝化时间(设置对象在它过期之前的空闲时间)
timeToLiveSeconds:缓存数据的生存时间(设置对象在它过期之前的生存时间)
overflowToDisk:内存不足时,是否启用磁盘缓存
clearOnFlush:内存数量最大时是否清除
 -->
      <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

Hibernate JPA 中配置Ehcache二级缓存的更多相关文章

  1. 在 JPA、Hibernate 和 Spring 中配置 Ehcache 缓存

    jpa, hibernate 和 spring 时配置 ehcache 二级缓存的步骤. 缓存配置 首先在 persistence.xml 配置文件中添加下面内容: <property name ...

  2. hibernate ehcache二级缓存

    xml配置 <?xml version="1.0" encoding="UTF-8"?> <ehcache> <!-- Sets ...

  3. 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma

     1 PersonTestMapper.xml中的内容如下: <?xmlversion="1.0"encoding="UTF-8"?> < ...

  4. Hibernate学习(九)———— 二级缓存和事务级别详讲

    序言 这算是hibernate的最后一篇文章了,下一系列会讲解Struts2的东西,然后说完Struts2,在到Spring,然后在写一个SSH如何整合的案例.之后就会在去讲SSM,在之后我自己的个人 ...

  5. NHibernate中使用memcache二级缓存

    在NHibernate中使用memcache二级缓存 一.Windows下安装Memcache  1. 下载   http://jehiah.cz/projects/memcached-win32/  ...

  6. mybatis缓存,包含一级缓存与二级缓存,包括ehcache二级缓存

    一,引言 首先我们要明白一点,缓存所做的一切都是为了提高性能.明白了这一点下面我们开始进入正题. 二,mybatis缓存概要 ①.mybatis的缓存有两种,分别是一级缓存和二级缓存.两者都属于查询缓 ...

  7. MyBatis ehcache二级缓存

    ehcache二级缓存的开启步骤: 1.导入jar 2.在映射文件中指定用的哪个缓存 3.加一个配置文件,这个配置文件在ehcache jar包中就有 使增删改对二级缓存不刷新: 对一级缓存没有用的, ...

  8. Spring-Boot项目中配置redis注解缓存

    Spring-Boot项目中配置redis注解缓存 在pom中添加redis缓存支持依赖 <dependency> <groupId>org.springframework.b ...

  9. Hibernate缓存策略(一级缓存和EHcache二级缓存)

    如何配置二级缓存: 第一步:导入EHcache依赖 1)Maven项目: <!--此处使用hibernate4--> <dependency> <groupId>o ...

随机推荐

  1. 转: vim简明教程

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...

  2. 17.1.2?Replication Formats 复制格式:

    17.1.2?Replication Formats 复制格式: 17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Ba ...

  3. HDU 2178 猜数字

    题解:设猜到的最大的数是h,在1到h间,你最多只要猜log2(h)+1(取整)次,所以易知==>h=2^m-1.即猜m次,能猜到的最大的数为2^m-1. #include <cstdio& ...

  4. JAVA的IO学习

    IO 有具体的分类: 有具体的分类:1:根据处理的数类型不同:字节流和字符流.2:根据流向不同:输入流和输出流. =============(补充字节跟字符概念区分)================= ...

  5. http://download.qt-project.org/archive/qt/4.5/qt-all-opensource-src-4.5.2.tar.bz2

    Index of /archive/qt/4.5 http://download.qt-project.org/archive/qt/4.5/qt-all-opensource-src-4.5.2.t ...

  6. ORACLE分科目统计每科前三名的学生的语句

    有个成绩表 score(student_no,Subject_no,Score)分别为学号,课程号,成绩.我想用语句查询出每科的前三名学生的学号,请各位高手教教小弟 1.创建测试语句:create t ...

  7. 【Linux命令】数据库mysql配置命令

    # 检查MySQL服务器系统进程 ~ ps -aux|grep mysql mysql 1103 0.0 0.3 492648 51780 ? Ssl 14:04 0:21 /usr/sbin/mys ...

  8. sqm(sqlmapGUI) pcat修改版

    sqlmap是一款开源的注入工具,支持几乎所有的数据库,支持get/post/cookie注入,支持错误回显注入/盲注,还有其他多种注入方法. 支持代理,指纹识别技术判断数据库 .而sqm(sqlma ...

  9. TF-IDF算法-自动提取关键词汇

    引子:Automatic Keyphrase extraction 很长文章里面,如何自动提取关键词汇呢? 比如在<中国的蜜蜂养殖>的长文里面,我们准备提取它的关键词.首先一个思路, 那些 ...

  10. python第二步,类对象部分

    类创建: class 类名: '类说明' def __init__ (self,参数):#类似php类的构造函数,self不知道什么东东 方法体 实例创建: 变量名 = 类名() #构造函数后的参数, ...