Hibernate Infinispan 2LC:在同一缓存区域中查询缓存和实体缓存

时间:2017-05-12 14:17:18

标签: hibernate infinispan wildfly-10 second-level-cache

使用JBoss Cache,可以将entity缓存和query缓存存储在同一缓存区域中,而不会出现任何问题。但是一旦迁移到野生蝇10(infinispan为2LC),似乎就有问题了。

我已经为实体提供了缓存区域,如下所示。

@Entity     
@Cacheable  
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY, 
     region="regionA")
public class EntityXYZ{

查询缓存为

 entManager.createQuery(
        "....")
    .setHint("org.hibernate.cacheable", true)
    .setHint("org.hibernate.cacheRegion", "regionA").getResultList();

请注意,上述query还包含entities,它们自称为cacheable,并且与regionA具有相同的缓存区域。

现在执行时,出现此错误。我怀疑这是由于冲突,因为它们存储在同一区域,并且它尝试获取返回另一个对象的对象id。但有人会抛光。这是真的吗?并解释有关错误的更多信息?

但同样适用于JBoss 5中的JBoss缓存。 infinispan处理方式不同吗?或者这是Hibernate问题吗?

ERROR [org.jboss.as.ejb3.invocation] (default task-8) WFLYEJB0034: EJB Invocation failed on component... javax.ejb.EJBTransactionRolledbackException: Object [id=4] was not of the specified subclass [ com.abc.xyz] : loaded object was of wrong class class  com.abc.yyy
                at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleInCallerTx(CMTTxInterceptor.java:159)
                at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:256)

     [org.hibernate.event.internal.DefaultLoadEventListener] (default task-8) HHH000327: Error performing load command : org.hibernate.WrongClassException: Object [id=4] was not of the specified subclass [com.abc.xyz] : loaded object was of wrong class class com.abc.yyy

更新

entitymanager尝试加载具有@id int的2个此类(在相同的共享缓存2LC区域中)的不同调用。正如弗拉维乌斯指出的那样,id=4似乎在两个班级之间很常见。

将以下配置添加到 persistence.xml 时,会出现启动错误。

persistence.xml

<property name="hibernate.cache.keys_factory" value="default" />

错误

 javax.persistence.PersistenceException: [PersistenceUnit: app-entity] Unable to build Hibernate SessionFactory
            at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:179)
            at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:121)
            at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:667)
            at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:193)
            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: javax.persistence.PersistenceException: [PersistenceUnit: app-entity] Unable to build Hibernate SessionFactory
            at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954)
            at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882)
            at org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
            at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:161)
            ... 7 more
    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:264)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:228)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
            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)
            ... 9 more
    Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [default] as strategy [org.hibernate.cache.spi.CacheKeysFactory]
            at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:113)
            at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:162)
            at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:126)

更新2

我已更改为将default提供给确切的FQN,但它不起作用。该错误不正确,因为该类是指定类的实现。

<property name="hibernate.cache.keys_factory" value="org.hibernate.cache.internal.DefaultCacheKeysFactory" />

错误:

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:264)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:228)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
        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)
        ... 9 more
Caused by: java.lang.ClassCastException: org.hibernate.cache.internal.DefaultCacheKeysFactory cannot be cast to org.hibernate.cache.spi.CacheKeysFactory
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.determineCacheKeysFactory(InfinispanRegionFactory.java:427)
        at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:378)
        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:254)

1 个答案:

答案 0 :(得分:3)

查询缓存是字符串索引的,并且使用原始ID(与EntityXYZ中标有@Id的字段/方法相同的对象)索引实体缓存,因此我想知道冲突是如何发生在{{ 1}}。

无论如何,对于实体和查询使用相同的区域(缓存)并不是一个好主意 - 实体有时需要更复杂的处理才能实现DB风格的隔离。使用Hibernate 6可能无法实现这样的组合。在许多线程中运行压力测试之前,您可能无法解决问题,顺序调用通常可以正常工作。

如果你坚持将它们存储在同一个缓存中,你可以尝试设置id=4(我希望你的WF版本已经包含这个设置,新版本应该已经将它作为默认设置)。

有关详细信息,请参阅https://hibernate.atlassian.net/browse/HHH-11083https://hibernate.atlassian.net/browse/HHH-10287