Hibernate使用ehcache缓存技术

导读:
  经过一段时间的试验,终于把hibernate的缓存机制用上了。主要的代码如下示:
  
   Hibernate.cfg.xml
  
  
  
   conn
  
  jdbc:oracle:thin:@192.168.1.28:1521:hblz
  
   hblz_new
   123
  
  oracle.jdbc.driver.OracleDriver
  
  
  org.hibernate.dialect.Oracle9Dialect
  
  
  org.hibernate.hql.classic.ClassicQueryTranslatorFactory
  
   true
  
  org.hibernate.connection.C3P0ConnectionProvider
  
   20
   5
   120
   100
   120
   2
  
  org.hibernate.cache.EhCacheProvider
  
   true
  
  
  
  
  
   County.hbm.xml
  
  
  
dynamic-update="false" dynamic-insert="false">  dynamic-update="false" dynamic-insert="false">
  
  
  
   SEQ_GG_COUNTY
  
  
  
  
  
type="java.lang.Long" />  type="java.lang.Long" />
  
  
  
  
  
   Ehcache.xml
  
  
  
  
timeToIdleSeconds="300" timeToLiveSeconds="600"
overflowToDisk="true">  timeToIdleSeconds="300" timeToLiveSeconds="600"
  overflowToDisk="true">
  
  
eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
overflowToDisk="true" />  eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
  overflowToDisk="true" />
  
  
  
   TestCache.java
  package org.hblz.test;
  import java.util.List;
  import org.hblz.entity.County;
  import org.hblz.util.HibernateSessionFactory;
  import org.hibernate.Criteria;
  import org.hibernate.Session;
  public class TestCache {
  public static void main(String[] args) {
  Session s = HibernateSessionFactory.currentSession();
  Criteria c = s.createCriteria(County.class);
  c.setCacheable(true);
  List lst = c.list();
  County ct = (County) lst.get(0);
  System.out.println("--1--" + ct.getName());
  HibernateSessionFactory.closeSession();
  
  try {
  Thread.sleep(5000);
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  
  s = HibernateSessionFactory.currentSession();
  c = s.createCriteria(County.class);
  c.setCacheable(true);
  lst = c.list();
  ct = (County) lst.get(0);
  System.out.println("--2--" + ct.getName());
  HibernateSessionFactory.closeSession();
  }
  }
  
  显示结果:
  Hibernate: select this_.COUN_ID as COUN1_0_, this_.COUN_MC as COUN2_0_0_, this_.COUN_DM as COUN3_0_0_, this_.COUN_SJID as COUN4_0_0_ from GG_COUNTY this_
  --1--台安县
  --2--台安县
  这里只查询了一次数据库,缓存起到了作用!
  
  Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1601906

本文转自
http://blog.csdn.net/bingxue2332/archive/2007/05/09/1601906.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值