title: Trouble configuring Hibernate cache -- org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage
body: <p>I'm deploying a Hibernate (v 4.1.5.SP1, validator 4.3.0.Final) application (a Jar file) running on a MySQL 5.5 db to JBoss 4.2.3.GA (can't change this).  I'm getting the error "org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage" when running the app, but it seems I've configured everythign correctly.  I'm using these Maven dependencies ...</p>
  
  <pre><code>            &lt;!-- Hibernate dependencies --&gt;
              &lt;dependency&gt;
                      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
                      &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
                      &lt;version&gt;${hibernate.validator.version}&lt;/version&gt;
              &lt;/dependency&gt;
              &lt;dependency&gt;
                      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
                      &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
                      &lt;version&gt;${hibernate.version}&lt;/version&gt;
              &lt;/dependency&gt;
              &lt;dependency&gt;
                      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
                      &lt;artifactId&gt;hibernate-ehcache&lt;/artifactId&gt;
                      &lt;version&gt;${hibernate.version}&lt;/version&gt;
              &lt;/dependency&gt;
  </code></pre>
  
  <p>The error is odd because I have the cache configured in my hibernate.cfg.xml file ...</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
          &lt;session-factory&gt;
                  &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
                  &lt;property name="hibernate.connection.datasource"&gt;java:/MySqlDS&lt;/property&gt;
                  &lt;property name="hibernate.connection.pool_size"&gt;10&lt;/property&gt;
                  &lt;property name="show_sql"&gt;true&lt;/property&gt;
                  &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  
                  &lt;!-- Enable Hibernate's automatic session context management --&gt;
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
          &lt;!--  Caching --&gt;
          &lt;property name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt;
                  &lt;property name="hibernate.cache.region.factory_class"&gt;org.hibernate.cache.ehcache.EhCacheRegionFactory&lt;/property&gt;
  
                  &lt;mapping class="org.mainco.subco.orgsclient.model.Organization" /&gt;
                  &lt;mapping class="org.mainco.subco.orgsclient.model.Community" /&gt;
                  &lt;mapping class="org.mainco.subco.orgsclient.model.Domain" /&gt;
                  &lt;mapping class="org.mainco.subco.orgsclient.model.State" /&gt;
                  &lt;mapping class="org.mainco.subco.orgsclient.model.Country" /&gt;
  
          &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>This is included in my META-INF/persistence.xml file, which is below ...</p>
  
  <pre><code>&lt;persistence&gt;
          &lt;persistence-unit name="orgsclientunit" transaction-type="JTA"&gt;
                  &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
                  &lt;jta-data-source&gt;java:/MySqlDS&lt;/jta-data-source&gt;
                  &lt;properties&gt;
                          &lt;property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml" /&gt;
                          &lt;property name="hibernate.validator.apply_to_ddl"&gt;false&lt;/property&gt;
                          &lt;property name="hibernate.validator.autoregister_listeners"&gt;false&lt;/property&gt;
                          &lt;property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/&gt;
                  &lt;/properties&gt;
          &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>To override the JBoss classloaders, I include this file at META-INF/jboss.xml ...</p>
  
  <pre><code>&lt;jboss&gt;
          &lt;class-loading java2ClassLoadingCompliance="false"&gt;
                  &lt;!-- setup classLoader repository. java2ParentDelegation to always have
                          package libs override server libs for this application --&gt;
                  &lt;loader-repository&gt;
                          org.mainco.subco.orgs:loader=orgsclient
                          &lt;loader-repository-config&gt;
                                  java2ParentDelegation=false
                          &lt;/loader-repository-config&gt;
                  &lt;/loader-repository&gt;
          &lt;/class-loading&gt;
  &lt;/jboss&gt;
  </code></pre>
  
  <p>Here is the complete stack trace.  What's the cause of this?</p>
  
  <pre><code>2012-08-16 11:01:37,195 ERROR [org.mainco.subco.orgsclient.service.OrganizationServiceImpl] org.hibernate.HibernateException: Could not instantiate cache implementation^M
  javax.persistence.PersistenceException: org.hibernate.HibernateException: Could not instantiate cache implementation^M
          at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:720)^M
          at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)^M
          at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)^M
          at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)^M
          at org.mainco.subco.orgsclient.service.AbstractTransactionService.startOperation(AbstractTransactionService.java:27)^M
          at org.mainco.subco.orgsclient.service.OrganizationServiceImpl.saveOrganization(OrganizationServiceImpl.java:87)^M
          at org.mainco.subco.orgsclient.service.OrganizationServiceImpl.getEODBOrganizations(OrganizationServiceImpl.java:221)^M
          at org.mainco.subco.orgsclient.service.OrganizationServiceImpl.saveOrgsFromWebService(OrganizationServiceImpl.java:183)^M
          at org.mainco.subco.orgsclient.quartz.UpdateOrgsJob.execute(UpdateOrgsJob.java:55)^M
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)^M
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)^M
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)^M
          at java.lang.reflect.Method.invoke(Method.java:597)^M
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)^M
          at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)^M
          at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)^M
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)^M
          at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)^M
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)^M
          at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)^M
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)^M
          at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)^M
          at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)^M
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)^M
          at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)^M
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)^M
          at org.jboss.ejb3.mdb.MessagingContainer.localInvoke(MessagingContainer.java:249)^M
          at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.delivery(MessageInflowLocalProxy.java:268)^M
          at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.invoke(MessageInflowLocalProxy.java:138)^M
          at $Proxy81.execute(Unknown Source)^M
          at org.jboss.resource.adapter.quartz.inflow.QuartzJob.execute(QuartzJob.java:57)^M
          at org.quartz.core.JobRunShell.run(JobRunShell.java:203)^M
          at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)^M
  Caused by: org.hibernate.HibernateException: Could not instantiate cache implementation^M
          at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:64)^M
          at org.hibernate.impl.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:214)^M
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)^M
          at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)^M
          ... 32 more^M
  Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]^M
          at org.hibernate.cache.NoCacheProvider.buildCache(NoCacheProvider.java:21)^M
          at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:61)^M
      ... 35 more^M
  </code></pre>
  
  <p>Thanks, - Dave</p>