配置Spring+hibernate使用ehcache作为second-level cache
来源:百度文库 编辑:神马文学网 时间:2024/04/25 14:29:29
大量数据流动是web应用性能问题常见的原因,而缓存被广泛的用于优化数据库应用。cache被设计为通过保存从数据库里load的数据来减少应用和数据库之间的数据流动。数据库访问只有当检索的数据不在cache里可用时才必要。hibernate可以用两种不同的对象缓存:first-level cache 和 second-level cache。first-level cache和Session对象关联,而second-level cache是和Session Factory对象关联。
缺省地,hibernate已经使用基于每个事务的first-level cache。Hibernate用first-level cache主要是减少在一个事务内的sql查询数量。例如,如果一个对象在同一个事务内被修改多次,hibernate将只生成一个包括所有修改的UPDATE SQL语句。为了减少数据流动,second-level cache在Session Factory级的不同事务之间保持load的对象,这些对象对整个应用可用,不只是对当前用户正在运行的查询。这样,每次查询将返回已经load在缓存里的对象,避免一个或更多潜在的数据库事务。
下载ehcache,hibernate3.2必须要ehcache1.2以上才能支持。可以修改log4j配置文件log4j.logger.net.sf.hibernate.cache=debug查看日志
1.在类路径上ehcache.xml:
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
2.applicationContext-hibernate.xml里Hibernate SessionFactory配置:
classpath:hibernate.cfg.xml
@HIBERNATE-DIALECT@
3
true
10
true
true
org.hibernate.cache.EhCacheProvider
说明:如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行
3.model类里采用Xdoclet生成*.hbm.xml里的cache xml标签,即
/**
* @hibernate.class table="WF_WORKITEM_HIS"
* @hibernate.cache usage="read-write"
*
*/
4.对于"query cache",需要在程序里编码:
getHibernateTemplate().setCacheQueries(true);
return getHibernateTemplate().find(hql);
缺省地,hibernate已经使用基于每个事务的first-level cache。Hibernate用first-level cache主要是减少在一个事务内的sql查询数量。例如,如果一个对象在同一个事务内被修改多次,hibernate将只生成一个包括所有修改的UPDATE SQL语句。为了减少数据流动,second-level cache在Session Factory级的不同事务之间保持load的对象,这些对象对整个应用可用,不只是对当前用户正在运行的查询。这样,每次查询将返回已经load在缓存里的对象,避免一个或更多潜在的数据库事务。
下载ehcache,hibernate3.2必须要ehcache1.2以上才能支持。可以修改log4j配置文件log4j.logger.net.sf.hibernate.cache=debug查看日志
1.在类路径上ehcache.xml:
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
2.applicationContext-hibernate.xml里Hibernate SessionFactory配置:
说明:如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行
3.model类里采用Xdoclet生成*.hbm.xml里的cache xml标签,即
/**
* @hibernate.class table="WF_WORKITEM_HIS"
* @hibernate.cache usage="read-write"
*
*/
4.对于"query cache",需要在程序里编码:
getHibernateTemplate().setCacheQueries(true);
return getHibernateTemplate().find(hql);
配置Spring+hibernate使用ehcache作为second-level cache
详解ehcache.xml文件中cache配置参数
Speed Up Your Hibernate Applications with Second-Level Caching
Speed Up Your Hibernate Applications with Second-Level Caching
Hibernate ehcache二级缓存技术
在Spring中配置Hibernate事务
spring下hibernate事务配置范例
Spring下Hibernate事务配置范例
在Spring中配置Hibernate事务
使用struts+spring+hibernate 组装web应用
使用struts+spring+hibernate 组装web应用
使用struts spring hibernate 组装web应用
Hibernate/Spring/Struts架构使用OpenSessionInView的...
Spring Hibernate在AOP方面的配置冲突解决之道
SQUID Cache Manager(cachemgr.cgi)配置及使用
Spring使用Annotation后的xml配置
使用struts+spring+hibernate 组装web应用1
将Spring和Hibernate 与WAS一起使用
使用struts+spring+hibernate 组装web应用1
如何使用eclipse构建一个struts spring hibernate系统
将Spring和Hibernate 与WAS一起使用
Spring的四种声明式事务的配置-Hibernate事务
Spring的四种声明式事务的配置-Hibernate事务
Java - hibernate+spring