hibernate笔记--缓存机制之 二级缓存(sessionFactory)和查询缓存
二级缓存(sessionFactory): Hibernate的二级缓存由SessionFactory对象管理,是应用级别的缓存。它可以缓存整个应用的持久化对象,所以又称为“SessionFactory缓存”. hibernate二级缓存中的缓存对象可以被整个应用的Session对象共享,即使关闭当前Session对象,新建的Session对象仍可使用。使用Hibernate的二级缓存之后查询数据,Session对象会首先在以及缓存中查找有无缓存数据被命中。如果没有,则查找二级缓存。如果有,则直接返回所命中的数据;否则查询数据库 下面介绍在hibernate中如何开启和使用二级缓存: 1.在hibernate.cfg.xml中开启二级缓存,并且配置cache.region.factory_class: <!-- 开启二级缓存 --> <property name="cache.use_second_level_cache">false</property> <!-- 配置cache.region.factory_classs --> <property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> 2