【问题标题】:Issue with hibernate ehcache configuration: Error configuring from null. Initial cause was null休眠 ehcache 配置问题:从 null 配置时出错。初始原因为空
【发布时间】:2018-04-09 21:07:02
【问题描述】:

我正在尝试使用休眠版本 3 在我们的产品中实现二级缓存。

但是,当我尝试访问我的应用程序时,出现以下错误:

[休眠会话工厂]:

java.lang.NullPointerException 在 net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:102) [已包装] net.sf.ehcache.CacheException:从 null 配置时出错。 最初的原因是 null

在 net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:105)

在 net.sf.ehcache.hibernate.HibernateUtil.loadAndCorrectConfiguration(HibernateUtil.java:51)

在 net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory.start(SingletonEhCacheRegionFactory.java:76)

[包装] org.hibernate.cache.CacheException: net.sf.ehcache.CacheException:从 null 配置时出错。最初的 原因为空

在 net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory.start(SingletonEhCacheRegionFactory.java:82)

在 org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:238)

在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)

hibernate.cfg.xml:

<hibernate-configuration>
    <session-factory>
<property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
        <property name="current_session_context_class">thread</property>
        <property name="hibernate.cache.use_query_cache">true</property>
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <property name="hibernate.cache.use_structured_entries">true</property>
        <property name="hibernate.cache.generate_statistics">true</property>
        <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
        <property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</property>
...
...

</session-factory>
</hibernate-configuration>

ehcache.xml

<ehcache>

<defaultCache maxElementsInMemory="100" eternal="false"
        timeToIdleSeconds="500" timeToLiveSeconds="1000" />

    <cache name="Audit" maxElementsInMemory="100"
        eternal="false" timeToIdleSeconds="500" timeToLiveSeconds="1000" />  
</ehcache>

我正在使用以下 jar 来实现缓存:

  • ehcache-core-2.6.10.jar
  • hibernate-3.6.6.jar
  • hibernate-validator-4.2.0.Final.jar

请指导我,哪里出错了。为什么它将文件显示为空。两个配置文件都在同一个文件夹中。

【问题讨论】:

    标签: java hibernate ehcache


    【解决方案1】:

    对我来说,它通过替换以下行来工作:

    <property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
    

    <property name="net.sf.ehcache.configurationResourceName">META-INF/config/ehcache.xml</property>
    

    虽然两个文件都在同一个文件夹中,但它无法识别该文件。所以,META-INF 成功了。

    【讨论】:

      【解决方案2】:

      似乎是找不到ehcache,xml 文件。你可以试试/ehcache.xml吗?

      【讨论】:

      • 我也尝试了该选项,但没有成功。然而,在浪费了 3 天的时间后,我找到了解决方案。请检查我的答案是否相同。
      • 是的...如果您的文件在 config/ 中,那么指定它似乎是可行的方法...您应该告诉我们文件在您的问题中的位置。看起来它位于类路径的根目录。
      【解决方案3】:

      经过 3 天的连续尝试,我发现了问题。根据互联网上的帖子,我发现它仅从类路径读取配置。

      虽然我的两个配置文件位于同一位置,但它们位于 classpath:config/ 位置。因此,按照以下方式更改我的配置对我有用:

      <property name="net.sf.ehcache.configurationResourceName">config/ehcache.xml</property>
      

      【讨论】:

        猜你喜欢
        • 2014-01-26
        • 1970-01-01
        • 2015-03-16
        • 2015-07-19
        • 1970-01-01
        • 2020-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多