我在集群中有 2 个独立的 Wildfly 13,并在两个节点上部署了一个 EE 应用程序。
我有一个可缓存实体(来自 hibernate 的 @Cache 和来自 jpa 的 @Cacheable),它还有一个使用缓存的命名查询。
我希望命名查询缓存在实体更新时失效。
当我运行应用程序时,提供的名为“entity”的缓存按预期工作:更新实体时,它会在整个集群范围内失效(根据 jmx,它的类型为 invalidation_sync)。
问题出在我的自定义缓存上:它在部署时没有启动,启动时它是本地缓存。实体更新时不会发生失效。
我尝试使用 @Startup ejb 查找自定义缓存,但没有成功(未找到缓存),或者令人惊讶的是,我在 org.infinispan.Cache 上收到 NoClassDefFoundError。
来自https://developer.jboss.org/thread/276133的建议和 https://developer.jboss.org/thread/277425?start=15&tstart=0没有帮助。
我在 Wildfly 配置中添加的自定义缓存未启动:我在 jmx 中看不到它,也无法使用 Paul Ferraro here 建议的 jndi 查找来获取它。遵循 jndi 名称模式。
这是实体的相关部分:
@Entity
@Table(name = "parameter", schema = "public")
@NamedQueries({
@NamedQuery(name = "findByValue", query = "select p from Parameter p where p.valeur = :valeur", hints = {
@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"),
@QueryHint(name = org.hibernate.annotations.QueryHints.CACHE_REGION, value = "invalidation-query"),
@QueryHint(name = QueryHints.CACHE_MODE, value = "NORMAL"),
@QueryHint(name = QueryHints.COMMENT, value = "Parameter.findByValue")
})
})
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "entity")
这是我的standalone-full-ha.xml 的 hibernate 缓存容器部分:
<cache-container name="hibernate" module="org.infinispan.hibernate-cache">
<transport channel="omega-ee" lock-timeout="60000"/>
<local-cache name="local-query">
<object-memory size="10000"/>
<expiration max-idle="100000"/>
</local-cache>
<invalidation-cache name="entity">
<transaction mode="NON_XA"/>
<object-memory size="10000"/>
<expiration max-idle="100000"/>
</invalidation-cache>
<invalidation-cache name="invalidation-query">
<transaction mode="NON_XA"/>
<object-memory size="10000"/>
<expiration max-idle="100000"/>
</invalidation-cache>
<replicated-cache name="RPL-getParamGenTest" statistics-enabled="true">
<transaction mode="BATCH"/>
</replicated-cache>
<replicated-cache name="replicated-entity" statistics-enabled="true">
<transaction mode="NONE"/>
</replicated-cache>
</cache-container>
persistence.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="myPersistenceUnit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/jdbc/myDatasource</jta-data-source>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
用于尝试 jndi 查找的代码如下:
@Resource(name = "infinispan/replicated-query")
private Cache<?, ?> cache;
我错过了什么?
我使用的是 wildfly 13、hibernate 5.1.14 和 infinispan 9.2.4。
最佳答案
更新到 Infinispan 9.4 并尝试以下操作:
<cache-container module="org.infinispan.extension:ispn-9.4" name="infinispan_container" default-cache="default">
<transport/>
<global-state/>
<distributed-cache name="default"/>
<distributed-cache name="memcachedCache"/>
<distributed-cache name="namedCache"/>
</cache-container>
</subsystem>
public class ExampleApplication {
@Resource(lookup = "java:jboss/datagrid-infinispan/container/infinispan_container")
CacheContainer container;
@Resource(lookup = "java:jboss/datagrid-infinispan/container/infinispan_container/cache/namedCache")
Cache cache;
}
参见:http://infinispan.org/docs/stable/user_guide/user_guide.html#accessing_containers_and_caches
关于java - 为什么我无法获取在 Wildfly 配置中添加的缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56221270/