【问题标题】:How to integrate Terracotta ehcache with hibernate如何将 Terracotta ehcache 与休眠集成
【发布时间】:2016-01-12 17:42:48
【问题描述】:

我正在尝试将我的应用程序连接到一个 Terracotta 缓存集群,但我无法使用我设置的配置启动我的应用程序。我在控制台中没有收到任何错误,但是如果我进行调试,它会在尝试创建 CacheManager 时失败。

我得到的错误是这样的。引起:java.lang.ClassNotFoundException: net.sf.ehcache.config.TerracottaConfiguration

我正在使用 Hibernate 4.x、Spring 4.x、Terracotta BigMemory Max 4.x。

您能告诉我我做错了什么或在哪里可以找到最新的文档吗?

这些是我的配置:

休眠属性:

<prop key="hibernate.cache.use_structured_entries">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop>

Ehcache-hibernate.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="ehcache-hibernate"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="ehcache.xsd">

<cache name="User" maxElementsInMemory="1000"
       maxElementsOnDisk="10000" eternal="false" timeToIdleSeconds="3600"
       timeToLiveSeconds="1200" memoryStoreEvictionPolicy="LFU">
    <terracotta />
</cache>

<defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="1200"
        timeToLiveSeconds="1200">
    <!--<terracotta />-->
</defaultCache>
<terracottaConfig url="localhost:9510" />

Maven相关依赖:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>4.3.10.Final</version>
</dependency>
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-terracotta</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>org.terracotta</groupId>
    <artifactId>terracotta-toolkit-1.6-runtime-ee</artifactId>
    <version>5.8.0</version>
</dependency>
<dependency>
    <groupId>org.terracotta</groupId>
    <artifactId>terracotta-toolkit-runtime-ee</artifactId>
    <version>4.1.1</version>
</dependency>

我在 Windows 上,我已经启动了 Terracotta 服务器和管理控制台。服务器显示为活动的,但没有客户端连接到它。

我尝试使用类似于我自己的环境来查找有效配置的示例,但找不到任何示例。

谢谢!

【问题讨论】:

  • 我们为此需要一条错误消息。
  • 看来我遇到了日志记录问题,错误没有出现。我明白了: 引起:java.lang.ClassNotFoundException: net.sf.ehcache.config.TerracottaConfiguration

标签: java spring hibernate ehcache terracotta


【解决方案1】:

您的类路径中缺少ehcache-core。添加以下依赖项使其工作:

<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache-core</artifactId>
  <version>2.1.1</version>
</dependency>

【讨论】:

  • 问题是hibernate 4.x包含ehcache-core。在我的例子中,我得到了 ehcache-core 版本 2.4.3,它不包含类 net.sf.ehcache.config.TerracottaConfiguration。我试图强制应用程序使用 ehcache-core 版本 2.1.1,但随后出现此错误:java.lang.NoSuchMethodError: net.sf.ehcache.config.TerracottaConfiguration.getNonstopConfiguration()Lnet/sf/ehcache/config/NonstopConfiguration ;
  • 最好将terracotta-ehcache 升级到2.4.3。您当前仅使用 2.1.1,这可能与 hibernate 引入的 ehcache-core 不兼容。
【解决方案2】:

我担心你不会得到这个工作。我不知道有一个代码库(甚至是非官方的)可以支持带有 Hibernate 4.x 的 Terracotta 4.1。很可能是我的记忆力在这里让我失望了,但是...

您可以尝试的最好方法是使用 Ehcache 嵌入式 Hibernate 2LC 提供程序类(在 net.sf.ehcache 包中),但我认为这些不支持 Hibernate 4.x SPI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    相关资源
    最近更新 更多