[size=medium]Hibernate的二级缓存
hibernate.cfg.xml(src目录下)
(即可以配置到hibernate.cfg.xml也可以配置到hibernate.properties)
## enable the query cache
hibernate.cache.use_query_cache true
## choose a cache implementation
hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider
ehcache.xml(src目录下)
<?xml version="1.0" ?>
<!--cache name="org.itfuture.www.po.DeptPo" maxElementsInMemory="500" eternal="false" timeToLiveSeconds="7200" timeToIdleSeconds="3600" overflowToDisk="true" /-->
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="180" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
<cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="50" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="7200" overflowToDisk="true"/>
<cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="true" overflowToDisk="true"/>
<cache name="org.itfuture.www.po.DeptPo" maxElementsInMemory="500" eternal="false" timeToLiveSeconds="7200" timeToIdleSeconds="3600" overflowToDisk="true" />
<cache name="org.itfuture.www.po.EmpPo" maxElementsInMemory="500" eternal="false" timeToLiveSeconds="7200" timeToIdleSeconds="3600" overflowToDisk="true" />
</ehcache>
Dept.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="org.itfuture.www.po">
<class name="DeptPo" table="dept">
<!--cache usage="read-write"/-->
<!--cache usage="read-only"/-->
<id name="deptid" column="deptid" type="java.lang.Integer">
<generator class="assigned"></generator>
</id>
<property name="deptname" type="java.lang.String" column="deptname"></property>
<property name="deptnum" type="java.lang.Integer" column="deptnum"></property>
<property name="deptdesc" type="java.lang.String" column="deptdesc"></property>
<set name="emps" cascade="all-delete-orphan" lazy="true" inverse="true">
<key column="deptid"></key>
<one-to-many class="EmpPo"/>
</set>
</class>
</hibernate-mapping>
运用二级缓存
//二级缓存的具体应用
public static void querycache()
{
//加载配置文件(hibernate.propertis,hibernate.cfg.xml)
Configuration conf=new Configuration();
conf.configure();
SessionFactory f=conf.buildSessionFactory();
Session session =f.openSession();
//Query q=session.createQuery("from java.lang.Object");
Query q=session.createQuery("select A from DeptPo A where A.deptid<20");
q.setCacheable(true);
List list=q.list();
for(Iterator iter=list.iterator();iter.hasNext();)
{
Object obj=iter.next();
System.out.println(obj);
//DeptPo po=(DeptPo)iter.next();
//System.out.println(po.getDeptid()+"---"+po.getDeptname()+"---"+po.getDeptnum()+"---"+po.getDeptdesc());
}
session.close();
System.out.println("*****************************************************");
session =f.openSession();
q=session.createQuery("select A from DeptPo A where A.deptid<20");
q.setCacheable(true);
list=q.list();
for(Iterator iter=list.iterator();iter.hasNext();)
{
Object obj=iter.next();
System.out.println(obj);
//DeptPo po=(DeptPo)iter.next();
//System.out.println(po.getDeptid()+"---"+po.getDeptname()+"---"+po.getDeptnum()+"---"+po.getDeptdesc());
}
f.close();
//java.sql.Connection conn=session.connection();
}
public static void Criteriacache()
{
//加载配置文件(hibernate.propertis,hibernate.cfg.xml)
Configuration conf=new Configuration();
conf.configure();
SessionFactory f=conf.buildSessionFactory();
Session session =f.openSession();
//Query q=session.createQuery("from java.lang.Object");
Criteria c=session.createCriteria(DeptPo.class).add(Expression.lt("deptid",20));
c.setCacheable(true);
List list=c.list();
for(Iterator iter=list.iterator();iter.hasNext();)
{
Object obj=iter.next();
System.out.println(obj);
//DeptPo po=(DeptPo)iter.next();
//System.out.println(po.getDeptid()+"---"+po.getDeptname()+"---"+po.getDeptnum()+"---"+po.getDeptdesc());
}
session.close();
System.out.println("*****************************************************");
session =f.openSession();
c=session.createCriteria(DeptPo.class).add(Expression.lt("deptid",20));
c.setCacheable(true);
list=c.list();
for(Iterator iter=list.iterator();iter.hasNext();)
{
Object obj=iter.next();
System.out.println(obj);
//DeptPo po=(DeptPo)iter.next();
//System.out.println(po.getDeptid()+"---"+po.getDeptname()+"---"+po.getDeptnum()+"---"+po.getDeptdesc());
}
f.close();
//java.sql.Connection conn=session.connection();
}
public static void loadcache()
{
//加载配置文件(hibernate.propertis,hibernate.cfg.xml)
Configuration conf=new Configuration();
conf.configure();
SessionFactory f=conf.buildSessionFactory();
Session session =f.openSession();
//Query q=session.createQuery("from java.lang.Object");
Object obj=session.load(DeptPo.class,1);
System.out.println(obj);
session.close();
System.out.println("*****************************************************");
session =f.openSession();
obj=session.load(DeptPo.class,1);
System.out.println(obj);
session.close();
f.close();
//java.sql.Connection conn=session.connection();
}
public static void writecache()
{
//加载配置文件(hibernate.propertis,hibernate.cfg.xml)
Configuration conf=new Configuration();
conf.configure();
SessionFactory f=conf.buildSessionFactory();
Session session =f.openSession();
DeptPo po=new DeptPo(800,"aaac",20,"asdfasd");
Transaction tran=session.beginTransaction();
session.save(po);
tran.commit();
session.close();
System.out.println("*****************************************************");
session =f.openSession();
Object obj=session.load(DeptPo.class,800);
System.out.println(obj);
session.close();
f.close();
//java.sql.Connection conn=session.connection();
}
//缓存更新
public static void updatecache()
{
//加载配置文件(hibernate.propertis,hibernate.cfg.xml)
Configuration conf=new Configuration();
conf.configure();
SessionFactory f=conf.buildSessionFactory();
Session session =f.openSession();
DeptPo po=(DeptPo)session.load(DeptPo.class,800);//放到二级缓存中
po.setDeptname("策划部2");
Transaction tran=session.beginTransaction();
session.update(po);
tran.commit();
session.close();
System.out.println("*****************************************************");
session =f.openSession();
po=(DeptPo)session.load(DeptPo.class,800);
System.out.println(po.getDeptname());
session.close();
f.close();
//java.sql.Connection conn=session.connection();
}
配置解释
<!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 --><property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <!-- 是否使用查询缓存 --> <property name="hibernate.cache.use_query_cache">true</property>
<defaultCache
maxElementsInMemory="10000" <!-- 缓存最大元素数目 -->
eternal="false" <!-- 缓存是否持久保存 -->
overflowToDisk="true" <!-- 是否保存到磁盘 ->
timeToIdleSeconds="3600" <!-- 当缓存闲置n秒后销毁 -->
timeToLiveSeconds="7200" <!-- 当缓存存活n秒后销毁-->
diskPersistent="false" <!?是否磁盘持久化-->
diskExpiryThreadIntervalSeconds= "120"/> <!?运行磁盘终结的时间间隔-->
</ehcache>
session
可以session进行增删改查功能实现
1、一个session中放过多的po,在执行flush和transaction时,会影响性能
2、一个session会对应一个一级缓存,另一个session不能使用其它session一级缓存中的数据
3、由以上必须要使用二级缓存
(1)、二级缓存是基于 SessionFactory工厂,一个SessionFactory的二级缓存中存的数据,凡是基于该SessionFactory 创建的session,都可以使用该二级缓存中存的数据(共享)
(2)、在具体session的执行flush或transaction提交时,不会把二级缓存中存的数据同步到数据库,提高了session的执行flush或transaction提交时 效率
4、什么时候会使用二级缓存
基于SessionFactory的二级缓存,会存通过SessionFactory创建的session查询时的数据,如果通过 session查数据时,先到session一级缓存中查找(根据po的标示符identifier)如果查不到的话,而且该查询 setCachable(true),那么会到二级缓存中去找,如果 二级缓存还找不到话,会生成sql到库中查
5、一般一个po需要一个二级缓存配置,当然可以配置一个缺省的二级缓存配置,所有po都使用该配置
二级缓存对当前查询的对象进行缓存,如果该对象有Set容器(盛放是相关的PO对象),不对set容器中对象缓存,但对set容器当中每个对象的标示符 (identifier)进行缓存(二级缓存),如果想在查容器中的对象, hibernate会以identifier为条件配一sql到数据库查询。[/size]
分享到:
相关推荐
以EhCache为例,我们需要在项目中引入ehcache-core或ehcache的依赖,并在Hibernate配置文件(hibernate.cfg.xml或persistence.xml)中启用二级缓存,添加如下配置: ```xml <property name="hibernate.cache.use_...
在这个"hibernate二级缓存实例"中,我们将深入探讨二级缓存的原理、配置以及在实际项目中的应用。 首先,我们需要了解一级缓存和二级缓存的区别。一级缓存是Session级别的,每个Session都有自己的一级缓存,用于...
《Hibernate二级缓存配置详解》 在Java的持久化框架Hibernate中,缓存技术是提升系统性能的关键之一。本文将深入探讨Hibernate的二级缓存,包括其事务范围、进程范围和集群范围的配置,特别是关注进程范围内的...
Hibernate二级缓存是一种提高应用程序性能的技术,它将数据存储在SessionFactory级别的缓存中,使得数据可以在不同的Session之间共享。这与一级缓存(Session级别)不同,一级缓存仅存在于单个Session生命周期内,当...
二级缓存可以通过设置不同的并发访问策略来解决并发问题,如事务型、读写型或非严格读写型,以适应不同场景下的需求。 持久化层的缓存范围决定了缓存的生命周期和访问权限。事务范围的缓存最安全,但只限于当前事务...
以下是一个简单的二级缓存配置示例: ```xml <hibernate-configuration> <!-- 配置缓存插件 --> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ...
**hibernate二级缓存详解** Hibernate作为Java领域中广泛使用的对象关系映射(ORM)框架,极大地简化了数据库操作。然而,在处理大量数据时,性能优化显得尤为重要,这就是二级缓存的作用。本文将深入探讨Hibernate...
配置Ehcache作为二级缓存提供商,需要在Hibernate的配置文件中设置`hibernate.cache.provider_class`为`net.sf.hibernate.cache.EhCacheProvider`。如果启用查询缓存,还需添加`hibernate.cache.use_query_cache=...
- **配置复杂**:二级缓存需要在Hibernate配置文件中指定缓存提供商,并对实体类进行配置,以便它们能够参与缓存。 - **缓存策略**:二级缓存支持不同的缓存策略,如读写策略、只读策略、定时刷新策略等,可以根据...
本篇将深入探讨Hibernate的一级缓存和二级缓存,以及查询缓存的配置和使用。 ### 一级缓存 一级缓存是Hibernate默认提供的缓存,它是Session级别的,每个Hibernate Session都有一个私有的、本地的一级缓存。当我们...
Hibernate二级缓存是Java开发中使用Hibernate框架进行数据持久化时优化性能的一种重要技术。它在一级缓存(Session级别的缓存)的基础上,提供了一个全局的、跨会话的数据存储层,可以显著减少对数据库的访问,从而...
在Java企业级开发中,Spring和Hibernate是两个非常...3. **配置Spring**:在Spring的配置文件(如`applicationContext.xml`)中,配置Hibernate SessionFactory,并注入二级缓存配置。以下是一个配置示例: ```xml ...
通过以上步骤,我们就成功地在Spring Boot 2.1.4.RELEASE项目中配置了使用Redis作为Hibernate二级缓存的环境。这将显著提升数据库查询效率,减少对数据库的压力,尤其在高并发场景下,效果尤为明显。记得在实际生产...
二级缓存是 Hibernate 缓存策略的一部分,它在应用程序的多个会话之间共享数据,进一步优化了数据库访问效率。 二级缓存分为以下关键知识点: 1. **一级缓存与二级缓存的区别**: - 一级缓存:每个 Hibernate ...
**二、Hibernate二级缓存** 二级缓存是SessionFactory级别的,跨越了多个Session,可以被多个线程共享。它通常由第三方插件如EhCache、Infinispan等提供。二级缓存分为以下几种类型: 1. **集合缓存**:用于存储...
在Hibernate配置文件`hibernate.cfg.xml`中,我们需要开启二级缓存并指定缓存提供者: ```xml <property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_...
本篇将详细介绍Hibernate二级缓存的概念、作用以及所需jar包的作用。 一、Hibernate二级缓存概念 Hibernate的一级缓存是指Session级别的缓存,每个Session内部都有一个一级缓存,用于存储实体对象,当Session关闭时...
本篇文章将深入探讨Hibernate二级缓存的概念、工作原理以及如何在实际项目中设置和使用。 **一、二级缓存概念** 一级缓存是每个Hibernate Session内部的一个内存区域,用于存储Session期间的操作对象。当Session...
对于Hibernate二级缓存的配置和使用,通常需要在Hibernate的配置文件(如hibernate.cfg.xml)中进行相应的设置。二级缓存的配置会涉及到具体的缓存提供者,例如可以使用EhCache、OSCache等作为二级缓存的实现。 综...