配置 Hibernate 缓存时遇到问题 - org.hibernate.cache.NoCachingEnabledException:二级缓存未启用使用
Posted
技术标签:
【中文标题】配置 Hibernate 缓存时遇到问题 - org.hibernate.cache.NoCachingEnabledException:二级缓存未启用使用【英文标题】:Trouble configuring Hibernate cache -- org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage 【发布时间】:2012-08-13 00:58:38 【问题描述】:我正在将运行在 mysql 5.5 db 上的 Hibernate(v 4.1.5.SP1,验证器 4.3.0.Final)应用程序(Jar 文件)部署到 JBoss 4.2.3.GA(无法更改) .运行应用程序时出现错误“org.hibernate.cache.NoCachingEnabledException:二级缓存未启用使用”,但似乎我已经正确配置了一切。我正在使用这些 Maven 依赖项...
<!-- Hibernate dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>$hibernate.validator.version</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>$hibernate.version</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>$hibernate.version</version>
</dependency>
这个错误很奇怪,因为我在 hibernate.cfg.xml 文件中配置了缓存...
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.datasource">java:/MySqlDS</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Caching -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<mapping class="org.mainco.subco.orgsclient.model.Organization" />
<mapping class="org.mainco.subco.orgsclient.model.Community" />
<mapping class="org.mainco.subco.orgsclient.model.Domain" />
<mapping class="org.mainco.subco.orgsclient.model.State" />
<mapping class="org.mainco.subco.orgsclient.model.Country" />
</session-factory>
</hibernate-configuration>
这包含在我的 META-INF/persistence.xml 文件中,位于下面 ...
<persistence>
<persistence-unit name="orgsclientunit" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/MySqlDS</jta-data-source>
<properties>
<property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml" />
<property name="hibernate.validator.apply_to_ddl">false</property>
<property name="hibernate.validator.autoregister_listeners">false</property>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
</properties>
</persistence-unit>
</persistence>
为了覆盖 JBoss 类加载器,我将这个文件包含在 META-INF/jboss.xml ...
<jboss>
<class-loading java2ClassLoadingCompliance="false">
<!-- setup classLoader repository. java2ParentDelegation to always have
package libs override server libs for this application -->
<loader-repository>
org.mainco.subco.orgs:loader=orgsclient
<loader-repository-config>
java2ParentDelegation=false
</loader-repository-config>
</loader-repository>
</class-loading>
</jboss>
这是完整的堆栈跟踪。这是什么原因?
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.<init>(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
谢谢,- 戴夫
【问题讨论】:
hibernate.cache.provider_class
是否定义为org.hibernate.cache.EhCacheProvider
?
【参考方案1】:
看看我用谷歌搜索找到的this 是否对你有帮助...
它说您应该将hibernate.cache.provider_class
配置为org.hibernate.cache.EhCacheProvider
【讨论】:
以上是关于配置 Hibernate 缓存时遇到问题 - org.hibernate.cache.NoCachingEnabledException:二级缓存未启用使用的主要内容,如果未能解决你的问题,请参考以下文章
企业框架源码 SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本
企业框架源码 SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本 websocket即时通讯
企业框架源码 SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本websocket即时通讯