Welcome 微信登录

首页 / 软件开发 / JAVA / hibernate3二级缓存的配置及测试

hibernate3二级缓存的配置及测试2011-01-041.配置ehcache.xml文件,放到classpath下:

<?xml version="1.0" encoding="GBK"?>
<ehcache>
<diskStore path="D:\TempObject"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="1000"
overflowToDisk="true"
/>
<cache name="com.sitechasia.occ.core.base.ExampleForTest" maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="1000"
overflowToDisk="true"
/>
</ehcache>

建议自定义cache时,cache名字和类路径名相同。

(1)不要使用默认缓存策略defaultCache(多个class共享)

(2)不要给cache name另外起名

否则继承AbstractTransactionalDataSourceSpringContextTests做测试时,抛出

org.hibernate.cache.CacheException: java.lang.IllegalStateException: The com.sitechasia.occ.core.base.ExampleForTest Cache is not alive.(我注释了红色的cache,使用defaultCache导致)

2.在ExampleForTest.hbm.xml中添加:(如果有集合,也需要添加)

<hibernate-mapping>
<class name="com.sitechasia.occ.core.base.ExampleForTest"
table="TESTTABLE" lazy="false">
<cache usage="read-write"/>
<id name="id" type="java.lang.String">
<column name="id" length="32" />
<generator class="uuid"></generator>
</id>
<property name="field1" type="java.lang.String" />
<property name="field2" type="java.lang.String" />
</class>
</hibernate-mapping>

如果使用Annocation,则类前添加

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)