1

配置缓存提供程序persistence.xml以及其他一些相关属性,如下所示。

<shared-cache-mode>ALL</shared-cache-mode>

<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.format_sql" value="true" />
    <property name="hibernate.event.merge.entity_copy_observer" value="allow"/>
    <property name="hibernate.use_sql_comments" value="true" />

    <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
    <property name="hibernate.cache.use_second_level_cache" value="true"/>
    <property name="hibernate.cache.use_query_cache" value="true"/>
    <property name="hibernate.cache.use_structured_entries" value="true"/>
    <property name="hibernate.cache.generate_statistics" value="true"/>
    <property name="hibernate.cache.infinispan.statistics" value="true" />
    <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
</properties>

当以下两个 JAR 文件添加到 EE 模块本身的编译时类路径时,此方法有效。

  • hibernate-ehcache-5.0.6.Final.jar
  • ehcache-core-2.4.3.jar

是否允许将这些 JAR 文件作为模块添加到服务器本身,因为这些 JAR 文件不需要存在于编译时类路径中?

当尝试在${Home}/modules/system/layers/base/net/sf/ehcache/main/module.xml.

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="net.sf.ehcache">
    <resources>
        <resource-root path="hibernate-ehcache-5.0.6.Final.jar"/>
        <resource-root path="ehcache-core-2.4.3.jar"/>
    </resources>

    <dependencies>
        <module name="org.hibernate"/>
        <module name="org.slf4j"/>
    </dependencies>
</module>

它会导致抛出以下异常,就好像它根本没有效果一样。

15:53:32,671 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 17) MSC000001: Failed to start service jboss.persistenceunit."WildFly.ear/WildFly-ejb.jar#org.hibernate.project.unit".__FIRST_PHASE__: org.jboss.msc.service.StartException in service jboss.persistenceunit."WildFly.ear/WildFly-ejb.jar#org.hibernate.project.unit".__FIRST_PHASE__: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
    at org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl$1$1.run(PhaseOnePersistenceUnitServiceImpl.java:121)
    at org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl$1$1.run(PhaseOnePersistenceUnitServiceImpl.java:103)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:665)
    at org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl$1.run(PhaseOnePersistenceUnitServiceImpl.java:130)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
    at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.<init>(MetadataBuilderImpl.java:663)
    at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:127)
    at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:135)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:185)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:149)
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:28)
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:40)
    at org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.<init>(TwoPhaseBootstrapImpl.java:39)
    at org.jboss.as.jpa.hibernate5.HibernatePersistenceProviderAdaptor.getBootstrap(HibernatePersistenceProviderAdaptor.java:159)
    at org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl.createContainerEntityManagerFactoryBuilder(PhaseOnePersistenceUnitServiceImpl.java:243)
    at org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl.access$800(PhaseOnePersistenceUnitServiceImpl.java:60)
    at org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl$1$1.run(PhaseOnePersistenceUnitServiceImpl.java:118)
    ... 7 more
Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.ehcache.EhCacheRegionFactory]
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:84)
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:29)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
    ... 21 more
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.cache.ehcache.EhCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFactory]
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:113)
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:71)
    ... 24 more

是否不允许将这两个 JAR 文件作为模块添加到服务器,或者存在不同的方法来实现这一点?


该模块org.hibernate包含以下资源及其依赖项。

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="org.hibernate">
    <resources>
        <resource-root path="hibernate-core-5.0.6.Final.jar"/>
        <resource-root path="hibernate-envers-5.0.6.Final.jar"/>
        <resource-root path="hibernate-entitymanager-5.0.6.Final.jar"/>
    </resources>

    <dependencies>
        <module name="asm.asm"/>
        <module name="com.fasterxml.classmate"/>
        <module name="javax.api"/>
        <module name="javax.annotation.api"/>
        <module name="javax.enterprise.api"/>
        <module name="javax.persistence.api"/>
        <module name="javax.transaction.api"/>
        <module name="javax.validation.api"/>
        <module name="javax.xml.bind.api"/>
        <module name="org.antlr"/>
        <module name="org.apache.commons.collections"/>
        <module name="org.dom4j"/>
        <module name="org.javassist"/>
        <module name="org.jboss.as.jpa.spi"/>
        <module name="org.jboss.jandex"/>
        <module name="org.jboss.logging"/>
        <module name="org.jboss.vfs"/>
        <module name="org.hibernate.commons-annotations"/>
        <module name="org.hibernate.infinispan" services="import" optional="true"/>
        <module name="org.hibernate.jipijapa-hibernate4-3" services="import"/>
    </dependencies>
</module>

我在 WildFly 9.0.2 final 中使用 Hibernate 5.0.6 final。

4

2 回答 2

0

我认为您必须执行以下步骤:

  1. 在主休眠模块中添加 hibernate-ehcache.jar;

  2. 像 wildfly 模块一样添加 ehcache。您必须使用 ehcache jar 和 module.xml 创建一个文件夹:[wildfly]/modules/system/layers/base/net/sf/ehcache/main。

  3. 在主 hibernate module.xml 中引用 ehcache 模块。

问候

史蒂夫

于 2016-01-27T10:48:47.370 回答
0

在 Wildfly(例如版本 10)中,您可以使用 Infinispan(与 JSR107 兼容)来代替进行 L2 缓存。在 wildfly 的模块目录 - org/hibernate/main/module.xml,你必须像这样使用(无需指定“hibernate-ehcache-5.0.11.Final.jar”):

 <resource-root path="hibernate-core-5.0.11.Final.jar"/>
 <resource-root path="hibernate-envers-5.0.11.Final.jar"/>
 <resource-root path="hibernate-entitymanager-5.0.11.Final.jar"/>
 <resource-root path="hibernate-java8-5.0.11.Final.jar"/>

WildFly 服务器默认加载 Infinispan。

在您的 persistence.xml 中,配置将是:

 <!-- enable query cache -->
  <property name="hibernate.cache.use_query_cache" value="true"/>
  <!-- EhCache provider-->
  <!-- <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/> -->
  <!-- 2nd level cache -->
  <property name="hibernate.cache.use_second_level_cache" value="true"/>
  <!-- enable statistics -->
  <property name="hibernate.generate_statistics" value="false"/>
  <!-- for Wildfly only -->
  <property name="wildfly.jpa.hibernate.search.module" value="org.hibernate.search.orm:5.5.5.Final"/>
于 2017-01-11T15:53:16.193 回答