title: net.sf.ehcache.ObjectExistsException: The Default Cache has already been configured
body: <p>I am using Spring (4.2.6.RELEASE) with Hibernate(5.1.0.Final). Hibernate properties defined as a bean in spring.xml.<br>
  And i added ehcache for second-level caching library. 
  I am getting error <strong>net.sf.ehcache.ObjectExistsException: The Default Cache has already been configured</strong> in following project.<br>
  Is there anybody to help? </p>
  
  <p>***************************** applicationContext-dao.xml  **************************************</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;
      &lt;bean id="databaseProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="location" value="file:///C:/Config/database.properties"/&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="transactionDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/&gt;
          &lt;property name="url" value="${db.url}"/&gt;
          &lt;property name="username" value="${db.username}"/&gt;
          &lt;property name="password" value="${db.password}"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Hibernate 4 SessionFactory Bean definition --&gt;
      &lt;bean id="hibernate4AnnotatedSessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="transactionDatasource" /&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.company.model.db.ApplicationStatEntity&lt;/value&gt;
                  &lt;value&gt;com.company.DeviceCapEntity&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.Oracle10gDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.batch_size"&gt;50&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.url"&gt;jdbc:oracle:thin:@&lt;IP&gt;:1522/rac2&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.driver_class"&gt;oracle.jdbc.OracleDriver&lt;/prop&gt;
  
                  &lt;prop key="hibernate.cache.use_query_cache"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.cache.region.factory_class"&gt;org.hibernate.cache.ehcache.EhCacheRegionFactory&lt;/prop&gt;
                  &lt;prop key="org.hibernate.cache.ehcache.configurationResourceName"&gt;/ehcache.xml&lt;/prop&gt;
                  &lt;!--property name="net.sf.ehcache.configurationResourceName"&gt;/ehcache.xml&lt;/property--&gt;
  
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>I implemented following GenericDao Class. This class gets entities as generic. </p>
  
  <p>***************************** GenericDao  **********************</p>
  
  <pre><code>import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.log4j.Logger;
  import org.hibernate.*;
  import org.hibernate.hql.internal.ast.QuerySyntaxException;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  import java.util.Iterator;
  import java.util.List;
  
  
  
  public class GenericDaoImpl&lt;GenericEntity&gt; implements GenericDao&lt;GenericEntity&gt; {
  
      private Class&lt;GenericEntity&gt; type;
      private ClassPathXmlApplicationContext context = null;
      protected SessionFactory sessionFactory = null;
      Logger log = Logger.getLogger(GenericDaoImpl.class.getName());
  
      public GenericDaoImpl() {
          context = getApplicationContext();
          sessionFactory = getSessionFactory();
      }
  
      private ClassPathXmlApplicationContext getApplicationContext() {
          if (this.context == null) {
              this.context = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
          }
          return this.context;
      }
  
      private SessionFactory getSessionFactory() {
          if(this.sessionFactory==null) {
              this.sessionFactory = (SessionFactory) getApplicationContext().getBean("hibernate4AnnotatedSessionFactory");
          }
          return this.sessionFactory;
      }
  
      public Class&lt;GenericEntity&gt; getMyType() {
          return this.type;
      }
  
      public void setType(Class&lt;GenericEntity&gt; type) {
          this.type = type;
      }
  
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
  
      public void insert(GenericEntity genericEntity) throws DaoException {
          Session session = this.sessionFactory.openSession();
          Transaction transaction = null;
          try {
              transaction = session.beginTransaction();
              log.info("[GeneicDaoImpl.insert]  hibernate session opened");
              session.save(genericEntity);
              log.info("[GeneicDaoImpl.insert]  insertion done");
              transaction.commit();
          } catch (QuerySyntaxException e) {
              if (transaction != null) {
                  transaction.rollback();
                  log.error(
                          "[GeneicDaoImpl.insert]  An error occured ID generation with table mapping. "
                          + "Check schema,table name in entity object or check table in database",
                          e);
                  throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
              }
          } catch (AnnotationException e) {
              if (transaction != null) {
                  transaction.rollback();
                  log.error("[GeneicDaoImpl.insert]  An error occured ID generation with sequence. Check sequence name in entity object and in database",
                            e);
                  throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
              }
          } catch (Exception e) {
              if (transaction != null) {
                  transaction.rollback();
                  log.error("[GeneicDaoImpl.insert]  hibernate error occured,rollback done", e);
                  throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
              }
  
          } finally {
              session.close();
          }
      }
  }
  </code></pre>
  
  <p>And in Junit Test Class, I call insert batch like this. </p>
  
  <p>************************** TestClass.java *****************************</p>
  
  <pre><code>    public class TestClass {
  
          @Test
          public void insertTest() {
              GenericDao&lt;ApplicationStatEntity&gt; insertBatchDao = new GenericDaoImpl();
              ApplicationStatEntity applicationStatEntity = new ApplicationStatEntity();
              applicationStatEntity.setId(102);
              applicationStatEntity.setDeviceid("SamsungShitty");
              try {
                  insertBatchDao.insert(applicationStatEntity);
              } catch (DaoException e) {
                  e.printStackTrace();
              }
          }
      }
  </code></pre>
  
  <p>***************************** ehcache.xml  **********************</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ehcache&gt;
  
      &lt;diskStore path="java.io.tmpdir/ehcache" /&gt;
  
      &lt;defaultCache maxEntriesLocalHeap="10000" eternal="false"
                    timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
                    maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
                    memoryStoreEvictionPolicy="LRU" statistics="true"&gt;
          &lt;persistence strategy="localTempSwap" /&gt;
      &lt;/defaultCache&gt;
  
  
  &lt;/ehcache&gt;
  </code></pre>