在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
分享到:
相关推荐
Spring3.2 Hibernate4.2 JPA2全注解实例.采用JTA事务管理,配置ehcache为二级缓存,在glassfish3.2.2和postgresql9测试通过。参考网上的资料整理。
spring4.04,springmvc, hibernate4.3 ,JPA2.1, shiro1.2, ehcache2 完全整合,用Ehcache做缓存,通用的DAO、Service接口和实现。完全注解配置,事务拦截方式处理。C0p3做连接池,JSP和Freemarker做View的模板。Shiro...
DWR3.0 Spring3.2 Hibernate4.2 JPA全注解实例。采用JTA事务管理,配置ehcache为二级缓存,在glassfish3.2.2和postgresql9测试通过。参考网上的资料整理。.
commons-logging-1.1.1.jar,dom4j-1.6.1.jar,ehcache-1.5.0.jar,hibernate3.jar,hibernate-jpa-2.0-api-1.0.0.Final.jar,javassist-3.12.0.GA.jar,jta-1.1.jar,slf4j-api-1.6.1.jar,slf4j-log4j12-1.6.1.jar...
此项目整合了目前主流和最前源的web开发技术:采用ehcache实现二级缓存(包含查询缓存);用sf4j及logback(log4j的升级版)记录日志;proxool(据说是dbcp和c3p0三者中最优秀的)做连接池;使用jquery的ajax实现仿...
入门级功能:包括属性(配置),JDBC模板(数据库CRUD),JPA(Hibernate数据库CRUD),Mybatis(数据库CRUD),Mybatis(Mybatis数据库CRUD),thymeleaf(模板解析),beetl(模板解析),freemarker (模板解析)...
员工SpringMvcRest 使用spring mvc、spring data jpa、ehcache、jsp、javascript和apache tomcat进行员工管理。 描述:它是一个员工管理系统,作为 Web 和休息服务暴露给用户,用户可以使用它执行基本的 crud 操作,...
J2EE的论坛web网站用到了三大框架spring+struts+JPA(hibernate)扫描注解和数据库用的是mySql数据库,使用ehcache做了缓存优化,起初有设计 有论坛、个 人空间、投票、商城、android客户端的,现在因为要找工作了,暂时...
二级小分类 品牌表 地区表 品牌月行汇总表 品牌月列汇总表 汇总行合计表 汇总列合计表 财务费用类别表 财务费用类别二级小类 店铺消费金额明细表 店铺消费金额主表 原始文件记录表 导入文件日志表 商品决策系统: ...
1.9 JPA - Hibernate 1.10 使用JPA保存数据 1.11 使用JdbcTemplate 1.12 Spring Boot修改端口号 1.13 Spring Boot配置ContextPath 1.14 Spring Boot改变JDK编译版本 1.15 处理静态资源(默认资源映射) 1.16 处理静态...
技术选择说明:管理 maven依赖和项目管理 git/svn 版本控制后端 Spring SpringMVC Spring data jpa(hibernate实现) Shiro 验证框架 hibernate validator 缓存 ehcache 数据源 druid servlet 3.0+前端 jsp 验证码 ...
web网站用到了三大框架spring+struts+JPA(hibernate)扫描注解和数据库用的是mySql数据库,使用ehcache做了缓存优化,起初有设计 有论坛、个人空间、投票、商城、android客户端的,实现的有论坛、和简单的商城,网上...
使用Spring+JPA+Velocity+Ehcache作为基础开发架构,应用SpringSecurity框架管理系统权限,结合URL重写技术静态化商城前台页面。 Shopping结合多年的电商开发经验及主流电商的功能特点,注重系统易用性、功能性、...
缓存框架:Ehcache ORM框架:Spring Data JPA、hibernate 4.3.5.Final 日志管理:SLF4J 1.7.21、Log4j 编辑器:ueditor 工具类:Apache Commons、Jackson 2.8.5、POI 3.15 view层:JSP 数据库:mysql...
ORM框架:Spring Data JPA、hibernate 4.3.5.Final 日志管理:SLF4J 1.7.21、Log4j 编辑器:ueditor 工具类:Apache Commons、Jackson 2.8.5、POI 3.15 view层:JSP 数据库:mysql、oracle等关系型数据库 前端 dom ...
18)、缓存使用EhCache或是Memcached。 19)、数据库连接池使用CommonsDBCP。 20)、Office文档操作使用POI。 21)、工作流使用Activiti。 22)、报表使用BIRT。 23)、Web资源优化使用wro4j。 24)、日志拦截使用...
ORM框架: Spring Data JPA、hibernate 4.3.5.Final 日志管理: SLF4J 1.7.21、I Log4j 编辑器: ueditor 工具类: Apache Commons、 Jackson 2.8.5、POI 3.15 view层:JSP 数据库: mysql
xmall是采用Java语言开发的多用户商城系统,使用Spring+JPA+Velocity+Ehcache作为基础开发架构,应用SpringSecurity框架管理系统权限, 结合URL重写技术静态化商城前台页面。结合多年的电商开发经验及主流电商的功能...
Spring 2.0的'spring-jdo.jar', 'spring-jpa.jar', 'spring-hibernate3.jar', 'spring-toplink.jar' 和 'spring-ibatis.jar' 被合并到Spring 2.5大粒度的'spring-orm.jar'中。 Spring 2.5的 'spring-test.jar' 取代...