hibernate二级缓存:hibernate3 2级缓存Cache的配置及测试

本文详细介绍了如何配置Ehcache并将其与Hibernate集成,包括配置文件的编写、使用策略的选择以及常见错误的排查。通过实例演示了在测试中遇到的CacheException问题,并提供了相应的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1.配置ehcache.xml文件放到path下:

<?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)不要使用默认缓存Cache策略defaultCache(多个共享)

  (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>
  < 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 ="uuid"></generator>
    </id>
    <property name="field1" type="java.lang.String" />
    <property name="field2" type="java.lang.String" />
  </>
</hibernate-mapping>

  如果使用Annocation则类前添加

  @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值