Hibernate的2层缓存机制:
第一层:session级别的缓存,hibernate本身就打开的,有一些缺陷,session关闭之后,缓存也就清理了。
另外,session级别的CRUD操作都会更新缓存,假如批量处理,可能会使内存溢出,可以使用两种方法清理
session缓存:1,session.evict(obj);2,session.clear();
session缓存的好处显而易见,但缺点也暴露无遗。
接着SessionFactory缓存就诞生了,也就是传说中的2级缓存,配置hibernate的2级缓存:
首先在hibernate.cfg.xml中添加
<property name="cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.internal.OSCacheProvider</property>
然后把oscache.properties放到classpath下即可,默认配置即可,
其中只有一个属性配置cache.capacity=1000,但可以随意配置,10000,-1000000,看内存容量。
只有上述操作还是不行的,必须告诉hibernate要缓存那些对象。
也有2中配置方式:
第一种:在hibernate.cfg.xml中添加配置信息
<class-cache usage="read-only" class="com.hejian.hibernate.domain.Employee"/>
usage想说明一下,有四个配置:read-only只读,read-write并发读写(推荐),nonstrict-read-write非严格并发读写(造成数据丢失),transactional事物型(缓存也回滚,一般cache不支持这种,因为实现非常复杂,只有jboss支持)
第二种:在映射文件配置:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hejian.hibernate.domain">
<class name="Employee" table="employee" dynamic-update="true" discriminator-value="0">
<!--
<id name="id" column="id">
<generator class="native"></generator>
</id>
-->
<cache usage="read-write"/>
<!-- use for union-class -->
<id name="id">
<generator class="hilo"></generator>
</id>
<!--
<discriminator column="type" type="int"></discriminator>
-->
<property name="name"></property>
<many-to-one name="depart" column="depart_id" ></many-to-one>
<!-- only one talbe
<subclass name="Skill" discriminator-value="1">
<property name="skill"></property>
</subclass>
<subclass name="Sale" discriminator-value="2">
<property name="sale"></property>
</subclass>
-->
<!--
<joined-subclass name="Skill" table="skill">
<key column="employee_id"></key>
<property name="skill"></property>
</joined-subclass>
<joined-subclass name="Sale" table="sale">
<key column="employee_id"></key>
<property name="sale"></property>
</joined-subclass>
-->
<!--
<subclass name="Skill" discriminator-value="1">
<property name="skill"></property>
</subclass>
<subclass name="Sale" discriminator-value="2">
<join table="sale">
<key column="employee_id"></key>
<property name="sale"></property>
</join>
</subclass>
-->
<union-subclass name="Skill" table="skill">
<property name="skill"></property>
</union-subclass>
<union-subclass name="Sale" table="sale">
<property name="sale"></property>
</union-subclass>
</class>
</hibernate-mapping>
为了便于调试,可在hibernate.cfg.xml中打开
<property name="generate_statistics">true</property>
之后可在代码中
System.out.println(HibernateUtil.getSessionFactory().getStatistics());
总结:在hibernate3.3.2中使用2级缓存,是可以正常使用,但不知为何
在hibernate4.0.1中同样配置会出现:
Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:68)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:339)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
at com.hejian.hibernate.domain.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
... 3 more
求解。