title: Spring boot +Spring data JPA +Second-level cache giving mutate error
body: <p>I am having an application with <strong>mutiple datasource</strong> using Spring Boot and Spring data JPA.</p>
  
  <p>I am getting the below exception <code>hibernate.cache.region.factory_class</code> not set, though I am setting it in code. Spring Boot is checking for it before I set, or not able to read my setting.</p>
  
  <pre><code>caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
         at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:83)
         at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:364)
         at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
         at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
         at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
         at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
         at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
         at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
         at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:338)
         at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
         at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
         ... 130 more
  
  8-Nov-2015 00:23:46.220 SEVERE [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive D:\sw\apache-tomcat-8.0.23\webapps\SecurityValidator.war
  java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SecurityValidator]]
  </code></pre>
  
  <p>I am having the below code. Only relevant parts are pasted:-</p>
  
  <p>Spring Boot configuation class to show the excluding auto configurations:-</p>
  
  <pre><code>@EnableAsync
  @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
          DataSourceTransactionManagerAutoConfiguration.class,})
  @ComponentScan("com.demo")
  public class Initializer extends SpringBootServletInitializer {
  </code></pre>
  
  <p>My two datasource configuations:-</p>
  
  <pre><code> @Bean
  public LocalContainerEntityManagerFactoryBean certEntityManager() {
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(certificateDataSource());
          em.setPackagesToScan(new String[] { "dao.entity.cert" });
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          HashMap&lt;String, Object&gt; properties1 = new HashMap&lt;String, Object&gt;();
          properties1.put("hibernate.hbm2ddl.auto", "validate");
          properties1.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
          em.setJpaPropertyMap(properties1);
          Properties properties = new Properties();
          properties.setProperty("spring.jpa.properties.hibernate.cache.use_second_level_cache", "true");
          properties.setProperty("spring.jpa.properties.hibernate.cache.use_query_cache", "true");
          properties.setProperty("spring.jpa.properties.hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
          properties.setProperty("spring.jpa.properties.hibernate.cache.default_cache_concurrency_strategy", "NONSTRICT_READ_WRITE");
          properties.setProperty("spring.jpa.properties.hibernate.cache.region_prefix", "valCache");
          properties.setProperty("spring.jpa.properties.hibernate.cache.use_structured_entries", "false");
          properties.setProperty("spring.jpa.properties.h4m.adapter.class", "kr.pe.kwonnam.hibernate4memcached.spymemcached.SpyMemcachedAdapter");
          properties.setProperty("spring.jpa.properties.h4m.timestamper.class", "kr.pe.kwonnam.hibernate4memcached.timestamper.HibernateCacheTimestamperMemcachedImpl");
  
          properties.setProperty("spring.jpa.properties.h4m.adapter.spymemcached.hosts", "localhost:11211");
          properties.setProperty("spring.jpa.properties.h4m.adapter.spymemcached.hashalgorithm", "KETAMA_HASH");
          properties.setProperty("spring.jpa.properties.h4m.adapter.spymemcached.operation.timeout.millis", "5000");
          properties.setProperty("spring.jpa.properties.h4m.adapter.spymemcached.transcoder", "kr.pe.kwonnam.hibernate4memcached.spymemcached.KryoTranscoder");
          properties.setProperty("spring.jpa.properties.h4m.adapter.spymemcached.cachekey.prefix", "vali");
          properties.setProperty("spring.jpa.properties.h4m.adapter.spymemcached.kryotranscoder.compression.threashold.bytes", "20000");
  
  
          properties.setProperty("spring.jpa.properties.h4m.expiry.seconds", "600");
  
          properties.setProperty("spring.jpa.properties.h4m.expiry.seconds.valCache.org.hibernate.cache.spi.UpdateTimestampsCache", "86400");
  
          properties.setProperty("spring.jpa.properties.h4m.expiry.seconds.valCache.org.hibernate.cache.internal.StandardQueryCache", "3600");
  
  
          properties.setProperty("spring.jpa.properties.h4m.expiry.seconds.valCache.cert", "1800");
          properties.setProperty("spring.jpa.properties.h4m.expiry.seconds.valCache.users", "1800");
          em.setJpaProperties(properties);
  
          return em;
      }
  </code></pre>
  
  <p>Similarly for the second data source also:-</p>
  
  <pre><code>@Bean
      @Primary
      public LocalContainerEntityManagerFactoryBean userEntityManager() {
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  ..................
  -----------------
  }
  </code></pre>
  
  <p>I am using the below dependencies:-</p>
  
  <pre><code>&lt;/dependency&gt;
              &lt;dependency&gt;
              &lt;groupId&gt;kr.pe.kwonnam.hibernate4memcached&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate4-memcached-core&lt;/artifactId&gt;
              &lt;version&gt;0.7&lt;/version&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
                      &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;kr.pe.kwonnam.hibernate4memcached&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate4-memcached-spymemcached-adapter&lt;/artifactId&gt;
              &lt;version&gt;0.7&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>Any suggestions or pointers is also welcomed.</p>
  
  <p><strong>UPDATE ERROR</strong></p>
  
  <p>I am still not able to move fully forward, but I am getting the below error now.</p>
  
  <pre><code>Caused by: net.spy.memcached.OperationTimeoutException: Mutate operation timed out,unable to modify counter [validator.validatorCache.users@]
          at net.spy.memcached.MemcachedClient.mutate(MemcachedClient.java:1663) ~[spymemcached-2.10.2.jar:2.10.2]
          at net.spy.memcached.MemcachedClient.mutateWithDefault(MemcachedClient.java:1835) ~[spymemcached-2.10.2.jar:2.10.2]
          at net.spy.memcached.MemcachedClient.incr(MemcachedClient.java:1767) ~[spymemcached-2.10.2.jar:2.10.2]
          at kr.pe.kwonnam.hibernate4memcached.spymemcached.SpyMemcachedAdapter.getNamespacedKey(SpyMemcachedAdapter.java:141) ~[hibernate4-memcached-spymemcached-adapter-0.7.jar:na]
          at kr.pe.kwonnam.hibernate4memcached.spymemcached.SpyMemcachedAdapter.set(SpyMemcachedAdapter.java:167) ~[hibernate4-memcached-spymemcached-adapter-0.7.jar:na]
          at kr.pe.kwonnam.hibernate4memcached.regions.GeneralDataMemcachedRegion.put(GeneralDataMemcachedRegion.java:86) ~[hibernate4-memcached-core-0.7.jar:na]
          at kr.pe.kwonnam.hibernate4memcached.strategies.MemcachedRegionAccessStrategy.putFromLoad(MemcachedRegionAccessStrategy.java:52) ~[hibernate4-memcached-core-0.7.jar:na]
          at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:221) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1115) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.loader.Loader.processResultSet(Loader.java:973) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.loader.Loader.doQuery(Loader.java:921) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.loader.Loader.doList(Loader.java:2554) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.loader.Loader.doList(Loader.java:2540) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.loader.Loader.list(Loader.java:2365) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
          at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:67) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
          at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:315) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
          at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:67) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_20]
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_20]
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_20]
          at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_20]
  </code></pre>
  
  <p>I corrected the properties setting, by removing the properties bit. It was a typo. But now I am getting the above error.</p>