使用hibernate 4.x进行二级缓存配置

时间:2014-08-13 05:43:53

标签: hibernate ehcache

我在hibernate4中配置了ehcache 以下是我的配置

的pom.xml

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>4.0.1.Final</version>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-ehcache</artifactId>
  <version>4.0.1.Final</version>
</dependency>

hibernate配置

configuration.setProperty("hibernate.cache.region.factory_class",
                "org.hibernate.cache.ehcache.EhCacheRegionFactory");
configuration.setProperty("hibernate.cache.region.factory_class",
                "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
configuration.setProperty("hibernate.cache.use_second_level_cache", "true");
configuration.setProperty("hibernate.cache.use_query_cache", "true");

实体类

 @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Cacheable




**Note : **
I did this configuration but  i am not getting any error, and ehcache is not working

1 个答案:

答案 0 :(得分:0)

尝试使用以下

configuration.setProperty("hibernate.cache.region.factory_class",
                "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
configuration.setProperty("hibernate.cache.use_second_level_cache", "true");
configuration.setProperty("hibernate.cache.use_query_cache", "true");

ehcache.hibernate.cache.provider_configuration=classpath:ehcache.xml

<强> ehcache.xml中

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="false" monitoring="autodetect"
         dynamicConfig="true">

 <diskStore path="java.io.tmpdir"/>

 <transactionManagerLookup class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
                              properties="jndiName=java:/TransactionManager" propertySeparator=";"/>

  <cacheManagerEventListenerFactory class="" properties=""/>

     <defaultCache
                maxElementsInMemory="10000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                overflowToDisk="true"
                diskSpoolBufferSizeMB="30"
                maxElementsOnDisk="10000000"
                diskPersistent="false"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU"
                statistics="false"/>
        </ehcache>