JBoss EAP 7和Ifinispan

时间:2017-12-14 13:16:42

标签: java hibernate jboss infinispan jboss-eap-7

我有一个用JEE编写的Web应用程序。我使用hibernate作为JPA提供程序,我想使用Infinispan作为二级缓存。当我浏览互联网时,我注意到只需要添加几行。我在persistence.xml中添加了这些行

<property name="hibernate.cache.provider_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/>
<property name="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/>
<property name="hibernate.cache.use_query_cache" value="true"/>

但在JBoss上部署ear和war文件时,我收到一条错误消息

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
        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.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:242)
        at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
        ... 83 more
Caused by: org.hibernate.cache.CacheException: Unable to start region factory
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:415)
        at org.hibernate.internal.CacheImpl.<init>(CacheImpl.java:49)
        at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:28)
        at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:20)
        at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:46)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
        ... 88 more
Caused by: org.infinispan.jmx.JmxDomainConflictException: ISPN000034: There's already a JMX MBean instance type=CacheManager,name="SampleCacheManager" already registered under 'org.infinispan' JMX domain. If you want to allow multiple instances configured with same JMX domain enable 'allowDuplicateDomains' attribute in 'globalJmxStatistics' config element
        at org.infinispan.jmx.JmxUtil.buildJmxDomain(JmxUtil.java:52)
        at org.infinispan.jmx.CacheManagerJmxRegistration.updateDomain(CacheManagerJmxRegistration.java:79)
        at org.infinispan.jmx.CacheManagerJmxRegistration.buildRegistrar(CacheManagerJmxRegistration.java:73)
        at org.infinispan.jmx.AbstractJmxRegistration.registerMBeans(AbstractJmxRegistration.java:37)
        at org.infinispan.jmx.CacheManagerJmxRegistration.start(CacheManagerJmxRegistration.java:41)
        at org.infinispan.manager.DefaultCacheManager.start(DefaultCacheManager.java:639)
        at org.infinispan.manager.DefaultCacheManager.<init>(DefaultCacheManager.java:300)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.createCacheManager(InfinispanRegionFactory.java:532)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory$1.doWork(InfinispanRegionFactory.java:500)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory$1.doWork(InfinispanRegionFactory.java:473)
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.workWithClassLoader(ClassLoaderServiceImpl.java:342)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.createCacheManager(InfinispanRegionFactory.java:472)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:380)
        ... 93 more

更新
当我的持久性单元看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>java:jboss/datasources/myDatasource</non-jta-data-source>
    <mapping-file>META-INF/orm_mapping.xml</mapping-file>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
         ...
        <properties>
          ...
            <property name="hibernate.cache.use_second_level_cache" value="true" />
          ...
        </properties>

    </persistence-unit>

</persistence>  

我收到了异常

Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
        at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:66)
        at org.hibernate.internal.SessionFactoryImpl.determineEntityRegionAccessStrategy(SessionFactoryImpl.java:619)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:332)
        at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
        ... 83 more

将以下行添加到persistence.xml

<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory" />

我仍然使用JMX MBean

获得异常
Caused by: org.infinispan.jmx.JmxDomainConflictException: ISPN000034: There's already a JMX MBean instance type=CacheManager,name="SampleCacheManager" already registered under 'org.infinispan' JMX domain. If you want to allow multiple instances configured with same JMX domain enable 'allowDuplicateDomains' attribute in 'globalJmxStatistics' config element
        at org.infinispan.jmx.JmxUtil.buildJmxDomain(JmxUtil.java:52)
        at org.infinispan.jmx.CacheManagerJmxRegistration.updateDomain(CacheManagerJmxRegistration.java:79)
        at org.infinispan.jmx.CacheManagerJmxRegistration.buildRegistrar(CacheManagerJmxRegistration.java:73)
        at org.infinispan.jmx.AbstractJmxRegistration.registerMBeans(AbstractJmxRegistration.java:37)
        at org.infinispan.jmx.CacheManagerJmxRegistration.start(CacheManagerJmxRegistration.java:41)
        at org.infinispan.manager.DefaultCacheManager.start(DefaultCacheManager.java:639)
        at org.infinispan.manager.DefaultCacheManager.<init>(DefaultCacheManager.java:300)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.createCacheManager(InfinispanRegionFactory.java:532)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory$1.doWork(InfinispanRegionFactory.java:500)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory$1.doWork(InfinispanRegionFactory.java:473)
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.workWithClassLoader(ClassLoaderServiceImpl.java:342)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.createCacheManager(InfinispanRegionFactory.java:472)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:380)
        ... 93 more

也许我需要某种infinispan配置文件,我可以放置

<jmx duplicate-domains="true" />
像这里提到的那样Infinispan as second level cache hibernate

2 个答案:

答案 0 :(得分:1)

您应该查看EAP 7文档,因为获得二级缓存所需的所有配置(由Infinispan在后台提供支持)是:

<persistence-unit name="...">
  (...) <!-- other configuration -->
  <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
  <properties>
    <property name="hibernate.cache.use_second_level_cache" value="true" />
    <property name="hibernate.cache.use_query_cache" value="true" />
  </properties>
</persistence-unit>

https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.1/html/development_guide/java_persistence_api_jpa#second_level_caches

答案 1 :(得分:0)

hibernate版本应为5.0.x。

persistence.xml:

<persistence ...>
        <persistence-unit ...>
                ...
                <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>  <!-- ADD THIS -->
                <properties>
                        ...
                        <property name="hibernate.cache.use_second_level_cache" value="true" />  <!-- KEEP THIS -->
                </properties>
        </persistence-unit>
</persistence>

删除所有其他hibernate.cache。*属性----只留下上面的

请注意,使用上述设置必须将实体明确标记为@Cacheable。如果不希望将每个实体明确标记为@cacheable,则可以使用ALL设置进行共享缓存模式,然后缓存所有实体。

有关配置和实施的其他文档,请参阅 EAP 7:Hibernate 5用户指南(http://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html#caching)和Hibernate 5开发人员指南(http://docs.jboss.org/hibernate/orm/5.0/devguide/en-US/html_single/#d5e1433)和Infinispan 8用户指南(http://infinispan.org/docs/8.1.x/user_guide/user_guide.html