2013-08-03 47 views
1

我試圖在休眠時使用SecondLevelCache。這是我的xml配置文件:休眠第二級高速緩存&JUnit

<persistence-unit name="EntityTestHibernate" transaction-type="RESOURCE_LOCAL"> 
     <properties> 
      <property name="hibernate.show_sql" value="false"/> 
      <property name="hibernate.format_sql" value="false"/> 
      <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/DB_NAME"/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> 
      <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/> 
      <property name="hibernate.connection.username" value="USERNAME"/> 
      <property name="hibernate.connection.password" value="PASSWORD"/>    
      <property name="hibernate.cache.use_second_level_cache" value="true"/>    
      <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" /> 
      <property name="hibernate.cache.provider_configuration_file_resource_path" value="/test/ehcache.xml" />  
     </properties> 
    </persistence-unit> 

我ehcache.xml中:

<ehcache name="cacheTest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> 
    <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" /> 
    <cache name="entityCache" 
      maxEntriesLocalHeap="50" 
      eternal="false" 
      timeToLiveSeconds="120"   
    /> 
</ehcache> 

,並在我的實體有這樣

@Cache(region="entityCache", usage=CacheConcurrencyStrategy.READ_WRITE) 

註釋當我運行單元測試我有以下錯誤(問題是相同的,如果我設置或不註釋它@DirtiesContext):

net.sf.ehcache.CacheException: Another CacheManager with same name 'cacheTest' already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following: 
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary 
2. Shutdown the earlier cacheManager before creating new one with same name. 
The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ] 
    at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:573) 
    at net.sf.ehcache.CacheManager.init(CacheManager.java:389) 
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:371) 
    at net.sf.ehcache.hibernate.EhCacheProvider.start(EhCacheProvider.java:93) 
    at org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.start(RegionFactoryCacheProviderBridge.java:72) 
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:238) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872) 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:850) 
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32) 

我剛剛配置了我的CacheManager與休眠XML文件,我不知道如何管理該錯誤。我在某處讀到我必須使用RegionFactory,但在ehcache文檔中這不是最佳實踐。我怎樣才能以正確的方式解決我的問題?

回答

1

不知道你在哪裏看到使用RegionFactory不是最佳實踐......請查看http://ehcache.org/documentation/user-guide/hibernate#Configure-Ehcache-as-the-Second-Level-Cache-Provider...and上的ehcache文檔,這正是他們所說你應該使用的。

此外,你應該使用哪一個應該解決您的JUnit問題SingletonEhCacheRegionFactory(單身意味着將有隻有1個高速緩存管理器...因此沒有2的CacheManager用在你的例外所示相同cachename)

網。 sf.ehcache.hibernate.SingletonEhCacheRegionFactory

請注意:休眠4,而不是使用net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory

希望幫助org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory。