hibernate 二级缓存的问题~~~~~~~~~~~~~

惊变二十八天 2010-03-18 11:14:30
我想用二级缓存,有点问题搞不明白 求教中
Person.hbm.xml


<hibernate-mapping package="hiber.domain">

<class name="Person" >
<cache usage="read-write"/>
<id name="id">
<generator class="increment"/>
</id>
<property name="name" not-null="true"/>
</class>
</hibernate-mapping>



hibernate.hbm.xml


<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.generate_statistics">true</property>


ehcache.xml

<cache name="hiber.domain.Person"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>


测试类

public class One2One {

public static void main(String[] args) {

addPersonAndIdCard2();
check();
}
public static void addPersonAndIdCard2() {

Session s= HiberUtil.getSession();
Transaction t = s.beginTransaction();
try{
Person person = new Person();
person.setName("sunyaozong");

s.save(person);
t.commit();
}catch(HibernateException e){
if(t!=null){
t.rollback();
}
throw e;
}finally{
if(s!=null){

s.close();
}
}
}

public static void check(){
Session s= null;
Transaction t =null;
try{
s= HiberUtil.getSession();
t = s.beginTransaction();


//s.getSessionFactory().evict(Person.class); //语句(1)
Person p1 =(Person)s.get(Person.class, 1);//语句(2)
System.out.println(""+p1.getName());
s.clear(); //语句(3)
Person p2 =(Person)s.get(Person.class, 1); //语句(4)
System.out.println(""+p2.getName());
}catch(HibernateException e){
if(t!=null){
t.rollback();
}
throw e;
}finally{
if(s!=null){
s.close();
}
}
try{
s= HiberUtil.getSession();
t = s.beginTransaction();
s.getSessionFactory().evict(Person.class);//语句(5)
Person p2 =(Person)s.get(Person.class, 1); //语句(6)
System.out.println(""+p2.getName());
}catch(HibernateException e){
if(t!=null){
t.rollback();
}
throw e;
}finally{
if(s!=null){
s.close();
}
}
Statistics st =s.getSessionFactory().getStatistics();
System.out.println(st);
System.out.println("put:"+st.getSecondLevelCachePutCount());
System.out.println("hit:"+st.getSecondLevelCacheHitCount());
System.out.println("miss:"+st.getSecondLevelCacheMissCount());
}
}



测试结果

Hibernate: select idcard0_.person_id as person1_3_0_, idcard0_.name as name3_0_ from idcard idcard0_ where idcard0_.person_id=?
sunyaozong
Hibernate: select idcard0_.person_id as person1_3_0_, idcard0_.name as name3_0_ from idcard idcard0_ where idcard0_.person_id=?
sunyaozong
Hibernate: select person0_.id as id2_1_, person0_.name as name2_1_, idcard1_.person_id as person1_3_0_, idcard1_.name as name3_0_ from Person person0_ left outer join idcard idcard1_ on person0_.id=idcard1_.person_id where person0_.id=?
sunyaozong
Statistics[start time=1268924734687,sessions opened=3,sessions closed=3,transactions=1,successful transactions=1,optimistic lock failures=0,flushes=1,connections obtained=3,statements prepared=5,statements closed=5,second level cache puts=2,second level cache hits=2,second level cache misses=1,entities loaded=1,entities updated=0,entities inserted=1,entities deleted=0,entities fetched=2,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,max query time=0]
put:2
hit:2
miss:1



=============
问题:这里需要到数据库当中查询三次,我是理解的 但是 put=2,hit=2,miss=1 都是为什么? 能不能解释一下呢?
为什么不清理二级缓存(注释掉语句5)之后 put=1,hit=3,miss=0了?
究竟是怎么回事?求指点

...全文
56 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧