title: Hibernate Second Level Cache & JUnit
body: <p>I'm trying to use SecondLevelCache with hibernate. This is my xml configuration file:</p>
  
  <pre><code>    &lt;persistence-unit name="EntityTestHibernate" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.show_sql" value="false"/&gt;
              &lt;property name="hibernate.format_sql" value="false"/&gt;
              &lt;property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/DB_NAME"/&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/&gt;
              &lt;property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/&gt;
              &lt;property name="hibernate.connection.username" value="USERNAME"/&gt;
              &lt;property name="hibernate.connection.password" value="PASSWORD"/&gt;            
              &lt;property name="hibernate.cache.use_second_level_cache" value="true"/&gt;             
              &lt;property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" /&gt; 
              &lt;property name="hibernate.cache.provider_configuration_file_resource_path" value="/test/ehcache.xml" /&gt;     
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>My ehcache.xml:</p>
  
  
  
  <pre><code>&lt;ehcache name="cacheTest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"&gt;
      &lt;defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" /&gt;
      &lt;cache  name="entityCache" 
              maxEntriesLocalHeap="50"
              eternal="false"
              timeToLiveSeconds="120"         
      /&gt;
  &lt;/ehcache&gt;
  </code></pre>
  
  <p>and on my entity there is an annotation like this</p>
  
  <pre><code>@Cache(region="entityCache", usage=CacheConcurrencyStrategy.READ_WRITE )
  </code></pre>
  
  <p>When i run a UnitTest i have following error (problem is same if i set or not annotation on it @DirtiesContext):</p>
  
  <pre><code>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.&lt;init&gt;(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.&lt;init&gt;(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)
  </code></pre>
  
  <p>I just have configured my CacheManager with hibernate xml file and i don't know how to manage that error. I read somewhere that i must use a RegionFactory but in ehcache documentation it's not a best practise. How could i solve my problem in right way?</p>