Hibernate中的二级缓存 EHCache

hibernate.cfg.xml

代码:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
 <property name="dialect">
  org.hibernate.dialect.MySQLDialect
 </property>
 <property name="connection.url">
  jdbc:mysql://localhost:3307/users
 </property>
 <property name="connection.username">root</property>
 <property name="connection.password">root</property>
 <property name="connection.driver_class">
  com.mysql.jdbc.Driver
 </property>
 <property name="myeclipse.connection.profile">mysqlusers</property>
 <property name="show_sql">true</property>
 <property name="format_sql">true</property>
 <property name="current_session_context_class">thread</property>
 <property name="cache.provider_class">
  org.hibernate.cache.EhCacheProvider
 </property>
 <mapping resource="com/b510/examplex/Guestbook.hbm.xml" />

</session-factory>

</hibernate-configuration>

src目录下面编写ehcache.xml

代码;

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
             <!-- 说明:maxElementsInMemory  设置 保存在内存中的缓存对象的最大数量
               etemal  设置缓存中对象 是否永远不过期,如果值为true,超过设置被忽略,缓存对象永远不过期
               timeToIdleSeconds   设置缓存中对象在他过期之前的最大空闲时间,单位为秒
               timeToLiveSeconds   设置缓存中对象在他过期之前的最大生存时间 ,单位为秒
               overflowToDisk      设置内存中的缓存对象达到maxElementsInMemory限制时,是否将缓存对象保存到硬盘中      
             -->
 <diskStore path="java.io.tmpdir"/>
 <defaultCache maxElementsInMemory="10000" eternal="false"
 timeToIdleSeconds="120" timeToLiveSeconds="120"
 overflowToDisk="true"/>
 <cache name="com.b510.examplex.Guestbook" maxElementsInMemory="1000"
 eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600"
 overflowToDisk="true"/> 
</ehcache>

Guestbook.hbm.xml

代码:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="com.b510.examplex.Guestbook" table="guestbook"
  catalog="users" optimistic-lock="version">
  <!-- EHCache二级缓存的策略:
      只读缓存          (read-only)
      读/写缓存          (read-write)
      不严格的读/写缓存       (nonstrict-read-write)
      事务缓存           (transactional)         EHCache不支持事务缓存  
   -->
  <!-- 应用EHCache二级缓存的策略 -->
  <cache usage="read-only"/>
  <id name="id" type="java.lang.Integer">
   <column name="id" />
   <generator class="increment" />
  </id>
  <version name="version" column="version" access="field"></version>
  <property name="name" type="java.lang.String">
   <column name="name" length="200" />
  </property>
  <property name="email" type="java.lang.String">
   <column name="email" length="50" />
  </property>
  <property name="phone" type="java.lang.String">
   <column name="phone" length="20" />
  </property>
  <property name="title" type="java.lang.String">
   <column name="title" length="200" />
  </property>
  <property name="content" type="java.lang.String">
   <column name="content" length="1000" />
  </property>
  <property name="createdTime" type="java.util.Date">
   <column name="created_time" length="10" />
  </property>
 </class>
</hibernate-mapping>

测试代码:

HIbernateTest.java

代码:

/**
 *
 */
package com.b510.examplex;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;


/**
 *
 * @author XHW
 *
 * @date 2011-7-15
 *
 */
public class HibernateTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  new HibernateTest().getGuestbooks();
 }

 public void getGuestbooks(){
  Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();
  session.beginTransaction();
  Query query=session.createQuery("from Guestbook");
  Iterator it=query.iterate();
  while(it.hasNext()){
   Guestbook gb=(Guestbook)it.next();
   System.out.println("name: "+gb.getName()+"  Id:"+gb.getId());
  }
  query=session.createQuery("from Guestbook where id=2");
  Guestbook gb=(Guestbook)query.uniqueResult();
  System.out.println("name: "+gb.getName()+"  Id:"+gb.getId());
  session.getTransaction().commit();
 } 
}

运行效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
    select
        guestbook0_.id as col_0_0_
    from
        users.guestbook guestbook0_
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: liuwei  Id:1
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: Hongtenzone@foxmail.com  Id:2
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: Hongten  Id:3
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: HOngten  Id:4
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: HOngten  Id:5
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: HOngten  Id:6
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: HOngten  Id:7
Hibernate:
    select
        guestbook0_.id as id0_,
        guestbook0_.version as version0_,
        guestbook0_.name as name0_,
        guestbook0_.email as email0_,
        guestbook0_.phone as phone0_,
        guestbook0_.title as title0_,
        guestbook0_.content as content0_,
        guestbook0_.created_time as created8_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=2
name: Hongtenzone@foxmail.com  Id:2

测试代码二:

HibernateTest.java

代码:

/**
 *
 */
package com.b510.examplex;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;


/**
 *
 * @author XHW
 *
 * @date 2011-7-15
 *
 */
public class HibernateTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  new HibernateTest().getGuestbooks();
 }

 public void getGuestbooks(){
  Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();
  session.beginTransaction();
  Query query=session.createQuery("from Guestbook");
  Iterator it=query.iterate();
  while(it.hasNext()){
   Guestbook gb=(Guestbook)it.next();
   System.out.println("name: "+gb.getName()+"  Id:"+gb.getId());
  }
  Guestbook gb=(Guestbook)session.load(Guestbook.class, 2);
  System.out.println("name: "+gb.getName()+"  Id:"+gb.getId());
  session.getTransaction().commit();
 } 
}

运行效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
    select
        guestbook0_.id as col_0_0_
    from
        users.guestbook guestbook0_
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: liuwei  Id:1
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: Hongtenzone@foxmail.com  Id:2
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: Hongten  Id:3
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: HOngten  Id:4
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: HOngten  Id:5
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: HOngten  Id:6
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
name: HOngten  Id:7
name: Hongtenzone@foxmail.com  Id:2

我们会看到这里和上面的查询结果不同的是,这次要少用一条select语句。这就是利用hibernate的二级缓存,

他在内存中保留了我们要查询的id=2的这条记录,所以当我们再次查询的时候,是直接从缓存中读出来。

posted @ 2011-07-17 19:04  Hongten  阅读(2018)  评论(0编辑  收藏  举报
Fork me on GitHub