`
chuan315
  • 浏览: 31829 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

hibernate二级缓存的配置

阅读更多
[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]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics