Hibernate统计和查询二级缓存

示例:假如在数据库中插入数据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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值