转自(http://elf8848.iteye.com/blog/700262)
?
一. 我们使用oscache来演示 , 引入oscache的jar包到工程中.
?
二. 把下载的Hibernate压缩包中的? hibernate-3.2\etc\oscache.properties放入classpath中. 用默认设置就行了.
?
三.修改hibernate.cfg.xml? , 加入如下, 意思启用二级缓存, 指定使用OSCache这个实现.
?
?
- <!DOCTYPE?hibernate-configuration?PUBLIC ??
- ????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN" ??
- ????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">??
- ??
- <hibernate-configuration>??
- ????<session-factory?>??
- ????????<property?name="show_sql">true</property>??
- ????????<property?name="hibernate.hbm2ddl.auto">update</property>??
- ???????? ??
- ????????<property?name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>??
- ????????<property?name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>??
- ????????????????? ??
- ????????<property?name="hibernate.connection.url">jdbc:mysql:///test</property>??
- ????????<property?name="hibernate.connection.username">root</property>??
- ????????<property?name="hibernate.connection.password">1234</property>??
- ???????? ??
- ????????<!--?这行不写也行,默认就为true-->??
- ????????<property?name="cache.use_second_level_cache">true</property>??
- ????????<!--?对Query也开启缓存?,由于命中率太低,建议不开?-->??
- ????????<property?name="cache.use_query_cache">true</property>??
- ????????<!--?指定使用OSCache这个实现-->??
- ????????<property?name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property>??
- ????????<!--?打开统计信息-->??
- ????????<property?name="generate_statistics">true</property>???????? ??
- ???????????? ??
- ????????<mapping?resource="dao/po/Department.hbm.xml"/>??
- ????????<mapping?resource="dao/po/Employee.hbm.xml"/>??
- ????????<mapping?resource="dao/po/Person.hbm.xml"/>??
- ????????<mapping?resource="dao/po/IdCard.hbm.xml"/>??
- ????????<mapping?resource="dao/po/Teacher.hbm.xml"/>??
- ????????<mapping?resource="dao/po/Student.hbm.xml"/>??
- ????????<mapping?resource="dao/po/User.hbm.xml"/>? ??
- ????????<mapping?resource="extends_test/extends_1/Person.hbm.xml"/>??
- ????????<mapping?resource="extends_test/extends_2/Person.hbm.xml"/>??
- ????????<mapping?resource="extends_test/extends_4/Person.hbm.xml"/>??? ??
- ???? ??
- ????????<!--?以下类被缓存-->??
- ????????<class-cache?class="dao.po.Person"?usage="read-write"/>??
- ???????? ??
- ????</session-factory>??
- </hibernate-configuration>??
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory > <property name="show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">1234</property> <!-- 这行不写也行,默认就为true--> <property name="cache.use_second_level_cache">true</property> <!-- 对Query也开启缓存 ,由于命中率太低,建议不开 --> <property name="cache.use_query_cache">true</property> <!-- 指定使用OSCache这个实现--> <property name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property> <!-- 打开统计信息--> <property name="generate_statistics">true</property> <mapping resource="dao/po/Department.hbm.xml"/> <mapping resource="dao/po/Employee.hbm.xml"/> <mapping resource="dao/po/Person.hbm.xml"/> <mapping resource="dao/po/IdCard.hbm.xml"/> <mapping resource="dao/po/Teacher.hbm.xml"/> <mapping resource="dao/po/Student.hbm.xml"/> <mapping resource="dao/po/User.hbm.xml"/> <mapping resource="extends_test/extends_1/Person.hbm.xml"/> <mapping resource="extends_test/extends_2/Person.hbm.xml"/> <mapping resource="extends_test/extends_4/Person.hbm.xml"/> <!-- 以下类被缓存--> <class-cache class="dao.po.Person" usage="read-write"/> </session-factory> </hibernate-configuration>
??
?
?
?
四.usage的说明
?
????? "read-only"????????????????????? 只读的,? po不能修改, 修改会抛异常
????? "read-write"??????????????????? 可对PO读写. 不怕并发修改
????? "nonstrict-read-write"???? 非严格的 读写, 并发修改不会问题,? 一般用在能容忍出问题的环境.
????? "transactional"??????????????? 有事务的,缓存 , oscache不是支持,? jboss 有实现.
?
?
?
五. 查询测试, 看看命中几次
?
- package?cache_test; ??
- ??
- import?org.hibernate.Session; ??
- import?org.hibernate.SessionFactory; ??
- import?org.hibernate.stat.Statistics; ??
- ??
- import?dao.HibernateUtil; ??
- import?dao.po.Person; ??
- ??
- /** ?
- ?*?测试缓存 ?
- ?*? ?
- ?*/??
- public?class?Test ??
- { ??
- ????public?static?void?main(final?String[]?args) ??
- ????{ ??
- ????????query(); ??
- ????????final?SessionFactory?sf?=?HibernateUtil.getSessionFactory(); ??
- ????????final?Statistics?s?=?sf.getStatistics(); ??
- ????????System.out.println(s);?//打印所有信息?监测SessionFactory ??
- ????????System.out.println("---------------"); ??
- ????????System.out.println("放入:"?+?s.getSecondLevelCachePutCount());?//打印缓存的信息 ??
- ????????System.out.println("命中:"?+?s.getSecondLevelCacheHitCount()); ??
- ????????System.out.println("丢失:"?+?s.getSecondLevelCacheMissCount()); ??
- ????} ??
- ??
- ????public?static?void?query() ??
- ????{ ??
- ????????Session?session?=?null; ??
- ??
- ????????for?(int?i?=?0;?i?<?10;?i++) ??
- ????????{ ??
- ????????????session?=?HibernateUtil.getSeesion(); ??
- ????????????final?Person?p1?=?(Person)?session.get(Person.class,?1); ??
- ????????????session.close(); ??
- ????????????System.out.println(p1.getName()); ??
- ????????} ??
- ??
- ????} ??
- }??
package cache_test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.stat.Statistics; import dao.HibernateUtil; import dao.po.Person; /** * 测试缓存 * */ public class Test { public static void main(final String[] args) { query(); final SessionFactory sf = HibernateUtil.getSessionFactory(); final Statistics s = sf.getStatistics(); System.out.println(s); //打印所有信息 监测SessionFactory System.out.println("---------------"); System.out.println("放入:" + s.getSecondLevelCachePutCount()); //打印缓存的信息 System.out.println("命中:" + s.getSecondLevelCacheHitCount()); System.out.println("丢失:" + s.getSecondLevelCacheMissCount()); } public static void query() { Session session = null; for (int i = 0; i < 10; i++) { session = HibernateUtil.getSeesion(); final Person p1 = (Person) session.get(Person.class, 1); session.close(); System.out.println(p1.getName()); } } }
?
?
六:? 打印出:
放入:1
命中:9
丢失:1