导读:
经过一段时间的试验,终于把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
经过一段时间的试验,终于把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