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>
<!--
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二级缓存的更多相关文章
- 在 JPA、Hibernate 和 Spring 中配置 Ehcache 缓存
jpa, hibernate 和 spring 时配置 ehcache 二级缓存的步骤. 缓存配置 首先在 persistence.xml 配置文件中添加下面内容: <property name ...
- hibernate ehcache二级缓存
xml配置 <?xml version="1.0" encoding="UTF-8"?> <ehcache> <!-- Sets ...
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
1 PersonTestMapper.xml中的内容如下: <?xmlversion="1.0"encoding="UTF-8"?> < ...
- Hibernate学习(九)———— 二级缓存和事务级别详讲
序言 这算是hibernate的最后一篇文章了,下一系列会讲解Struts2的东西,然后说完Struts2,在到Spring,然后在写一个SSH如何整合的案例.之后就会在去讲SSM,在之后我自己的个人 ...
- NHibernate中使用memcache二级缓存
在NHibernate中使用memcache二级缓存 一.Windows下安装Memcache 1. 下载 http://jehiah.cz/projects/memcached-win32/ ...
- mybatis缓存,包含一级缓存与二级缓存,包括ehcache二级缓存
一,引言 首先我们要明白一点,缓存所做的一切都是为了提高性能.明白了这一点下面我们开始进入正题. 二,mybatis缓存概要 ①.mybatis的缓存有两种,分别是一级缓存和二级缓存.两者都属于查询缓 ...
- MyBatis ehcache二级缓存
ehcache二级缓存的开启步骤: 1.导入jar 2.在映射文件中指定用的哪个缓存 3.加一个配置文件,这个配置文件在ehcache jar包中就有 使增删改对二级缓存不刷新: 对一级缓存没有用的, ...
- Spring-Boot项目中配置redis注解缓存
Spring-Boot项目中配置redis注解缓存 在pom中添加redis缓存支持依赖 <dependency> <groupId>org.springframework.b ...
- Hibernate缓存策略(一级缓存和EHcache二级缓存)
如何配置二级缓存: 第一步:导入EHcache依赖 1)Maven项目: <!--此处使用hibernate4--> <dependency> <groupId>o ...
随机推荐
- struts2 taglib struts标签学习整理中
项目中经常会用到struts标签,这样可以减少代码量. select用法: <s:select list="#request.sysTypes" name="rul ...
- php call_user_func和call_user_func_array
首先要看这个页面关于callable类型:http://www.php.net/manual/zh/language.types.callable.php 自 PHP 5.4 起可用 callable ...
- ALV列、行、单元格颜色设置
)颜色含义 :海蓝:2:浅清:3:黄色:4:浅蓝:5:青色:6:红色:7:橙色. (1)首位为主颜色: (2)次位为辅助颜色: (3)末位为0时,表示首位数字表为表格的底色: 末位为1时,则表示以1为 ...
- Java中volatile的作用以及用法
volatile让变量每次在使用的时候,都从主存中取.而不是从各个线程的“工作内存”. volatile具有synchronized关键字的“可见性”,但是没有synchronized关键字的“并发正 ...
- HttpWebRequest和WebClient的区别
HttpWebRequest和WebClient的区别(From Linzheng): 1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Creat ...
- ie调试控制台
function initConsole() { var console = document.createElement('div'); console.id = 'errorConsole'; d ...
- java结构与算法之选择排序
一 .java结构与算法之选择排序(冒择路兮快归堆) 什么事选择排序:从一组无序数据中选择出中小的的值,将该值与无序区的最左边的的值进行交换. 简单的解释:假设有这样一组数据 12,4,23,5,找到 ...
- java键盘录入
System.out:标准输出设备(默认是:控制台) System.in:标准输入设备(默认是:键盘) --------------------- InputStream in = System.in ...
- win7安装 Apache2.2 PHP5.3 MySQL5.6
. APACHE2.2 经典参考资料 http://blog.csdn.net/yousuosi/article/details/9859507 官方下载地址 http://mirror.bi ...
- LinkList Operation
链表典型数据结构: #define ElemType int typedef struct LinkNode{ ElemType value; struct LinkNode* next; }; 相比 ...