您的位置:首页 > 其它

Hibernate统计和查询二级缓存

2014-05-07 22:22 387 查看
示例:假如在数据库中插入数据title=ee若ehcacha.xml配置文件,java类,和类配置文件以及中配置文件配好的情况下:hibernate.cfg.xml中添加:<!-- 开启二级缓存的统计功能 --><property name="hibernate.generate_statistics">true</property><!-- 设置使用结构化方式来维护缓存项 --><property name="hibernate.cache.use_structured_entries">true</property>测试代码:
package com.pengsuen.test;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.pengsuen.bean.News;

public class Test {

static Configuration conf = new Configuration().configure();
static SessionFactory sf = conf.buildSessionFactory();

public static void main(String[] args) {
Test t=new Test();
t.noCacheQuery();
t.cacheQuery();
t.cacheQueryIterator();
t.stat();
sf.close();

}
private void noCacheQuery(){
Session session=sf.openSession();
session.beginTransaction();
System.out.println("*************nocacheQuery开始*************************");

List titles=session.createQuery("select news.title from News news").setCacheable(false).list();

for(Object title:titles)
{
System.out.println(title);
}
System.out.println("--------------------------");

titles=session.createQuery("select news.title from News news").setCacheable(false).list();

for(Object title:titles)
{

System.out.println(title);
}
session.getTransaction().commit();
session.close();
System.out.println("*************nocacheQuery结束*************************");

}

private void cacheQuery(){
Session session=sf.openSession();
session.beginTransaction();
System.out.println("***************cacheQuery开始*************************");

List titles=session.createQuery("select news.title from News news").setCacheable(true).list();

for(Object title:titles)
{
System.out.println(title);
}

session.getTransaction().commit();

System.out.println("-------------------------------");
Session sess2=sf.openSession();
sess2.beginTransaction();
titles=sess2.createQuery("select news.title from News news").setCacheable(true).list();

for(Object title:titles)
{
System.out.println(title);
}

sess2.getTransaction().commit();
session.close();
sess2.close();
System.out.println("*************cacheQuery结束*************************");

}

public static void cacheQueryIterator(){
Session session=sf.openSession();
session.beginTransaction();
System.out.println("************cacheQueryIterator开始*************************");

Iterator it=session.createQuery("select news.title from News news").setCacheable(true).iterate();
while(it.hasNext()){
System.out.println(it.next());
}
session.getTransaction().commit();
System.out.println("------------------------------");
Session sess2=sf.openSession();
sess2.beginTransaction();
it=sess2.createQuery("select news.title from News news").setCacheable(true).iterate();
while(it.hasNext()){
System.out.println(it.next());
}
sess2.getTransaction().commit();
session.close();
sess2.close();
System.out.println("***************cacheQueryIterator结束*************************");

}
private void stat(){
long hitCount=sf.getStatistics().getQueryStatistics("select news.title from News news").getCacheHitCount();
System.out.println("查询缓存命中次数:"+hitCount);
}
}
执行结果:
**************************nocacheQuery开始*************************
Hibernate: select news0_.title as col_0_0_ from NEWS news0_
hello
--------------------------
Hibernate: select news0_.title as col_0_0_ from NEWS news0_
hello

**************************nocacheQuery结束*************************
**************************cacheQuery开始*************************
Hibernate: select news0_.title as col_0_0_ from NEWS news0_
hello
-------------------------------
hello

**************************cacheQuery结束*************************
**************************cacheQueryIterator开始*************************
Hibernate: select news0_.title as col_0_0_ from NEWS news0_
hello
------------------------------
Hibernate: select news0_.title as col_0_0_ from NEWS news0_
hello

**************************cacheQueryIterator结束*************************
查询缓存命中次数:1

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: