二级缓存(SessionFacotry级共享)
弥补一级缓存生命周期短的缺陷
1、需要手动配置
2、jar包由第三方开发,常见的Hashtable,OSCache,EHCache
3、二级缓存的对象可能放在内存,也可能放在磁盘
举例:
配置OSCache细节:
(1)引入jar包
D:\Download\hibernate-distribution-3.3.1.GA\lib\optional\oscache\oscache-2.1.jar
(2)在项目src目录下引入oscache.properties
D:\Download\hibernate-distribution-3.3.1.GA\project\etc\oscache.properties
(3)hibernate.cfg.xml中配置
<hibernate-configuration>
<session-factory>
......
<property name="cache.use_second_level_cache">true</property>
<!-- 指定使用哪种二级缓存 -->
<property name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property>
<!-- 指定对哪个domain对象启用二级缓存,也可在Student.hbm.xml中设置启用二级缓存,<cache usage="read-write"/> -->
<class-cache class="com.lcy.domain.Student" usage="read-write"/>
<!-- 统计信息session打开和关闭次数,二级缓存命中次数等 -->
<property name="hibernate.generate_statistics">true</property>
......
</session-factory>
</hibernate-configuration>
注意:
用sessionFactory.getStatistics()获取统计信息
例如:
Statistics statistics= HibernateUtil.getSessionFactory().getStatistics();
posted on
浙公网安备 33010602011771号