使用memcached进行Hibernate L2缓存

时间:2013-04-11 15:39:52

标签: hibernate memcached

我正在尝试使用memcached进行Hibernate Level 2缓存,但还没有能够让它工作。当我启动tomcat并启动我的应用程序时,我遇到了以下异常(java.lang.NoClassDefFoundError:org / hibernate / cache / TimestampsRegion):

    javax.servlet.ServletException: Servlet execution threw an exception
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)
at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxx.webapp.filter.MemberFilter.doFilter(MemberFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxx.webapp.filter.AuthFilter.doFilter(AuthFilter.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxx.webapp.filter.LanguageHandlingFilter.doFilter(LanguageHandlingFilter.java:129)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxx.webapp.filter.SsoFilter.doFilter(SsoFilter.java:72)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxx.webapp.filter.SessionStatusFilter.doFilter(SessionStatusFilter.java:41)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxx.webapp.filter.BlackListFilter.doFilter(BlackListFilter.java:36)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxx.webapp.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:143)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/cache/TimestampsRegion
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:393)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:264)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2283)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2279)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1748)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
atorg.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)

我使用JPA和hibernate 4.1.7作为提供者。我的hibernate L2缓存的persistence.xml属性如下所示:

<property name="hibernate.cache.provider_class" value="com.googlecode.hibernate.memcached.MemcachedCacheProvider" />
        <property name="hibernate.cache.use_second_level_cache" value="true" />
        <property name="hibernate.cache.region.factory_class" value="com.googlecode.hibernate.memcached.MemcachedRegionFactory"/>
        <property name="hibernate.cache.use_query_cache" value="true" /> 
         <property name="hibernate.Memcached.servers" value="localhost:11211"/>
         <property name="hibernate.Memcached.cacheTimeSeconds" value="300"/>  

我使用的是与hibernate和memcached相关的以下版本的jar文件:

memcached的-2.3.1.jar hibernate-memcached-1.6.jar(从com.googlecode.hibernate.memcached项目的源代码编译) SLF4J-1.5.0.jar SLF4J-log4j12-1.5.0.jar

我还没能解决这个问题。非常感谢你们的任何帮助。

阿伦

2 个答案:

答案 0 :(得分:1)

我知道这已经有三年了,但这仍然非常重要。您可以在Hibernate 4和5中使用memcached(或任何memcache clone,如elasticache)。我们自己也这样做。

您可以通过此gradle依赖项获取正确的jar:

compile 'com.github.mihaicostin:hibernate-l2-memcached:1.1.0'

或Maven: <groupId>com.github.mihaicostin</groupId> <artifactId>hibernate-l2-memcached</artifactId> <version>1.1.0</version>

它是最新的,效果很好。在github上开源。 https://github.com/mihaicostin/hibernate-l2-memcached

作为一个例子,来自Bjond的persistence.xml的片段。请注意,您需要该区域工厂:

    <property name="hibernate.dialect" value="com.bjond.persistence.converter.JsonPostgreSQLDialect"/>
    <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
    <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>

     <property name="hibernate.cache.use_second_level_cache" value="true" />
     <property name="hibernate.cache.use_query_cache" value="true" />    
     <property name="hibernate.memcached.REGION.cacheTimeSeconds" value="30"/>
     <property name="hibernate.cache.region.factory_class" value="com.mc.hibernate.memcached.MemcachedRegionFactory"/>
     <property name="hibernate.memcached.operationTimeout" value="5000"/>
     <property name="hibernate.memcached.connectionFactory" value="KetamaConnectionFactory"/>
     <property name="hibernate.memcached.hashAlgorithm" value="HashAlgorithm.FNV1_64_HASH"/>
     <property name="hibernate.memcached.servers" value="localhost:11211"/>
     <property name="hibernate.memcached.cacheTimeSeconds" value="300"/>
     <property name="hibernate.memcached.clearSupported" value="false"/>
     <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
     <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/> 
     <property name="hibernate.generate_statistics" value="true" />

答案 1 :(得分:0)

听起来你正在使用Hibernate 4.要使用hibernate-memcached,你应该拥有HB3。请参阅此处的hibernate-memcached配置:https://code.google.com/p/hibernate-memcached/wiki/Configuration