Java 如何为JPA L2缓存配置WIldfly infinispan子系统?

Java 如何为JPA L2缓存配置WIldfly infinispan子系统?,java,hibernate,jpa,wildfly,infinispan,Java,Hibernate,Jpa,Wildfly,Infinispan,我正在尝试在Wildfly 14上启用JPA二级缓存。即使我已经添加了配置缓存,也没有发生。日志显示实体正在被缓存,但在检索实体时似乎没有使用缓存 我使用Hibernate作为JPA提供程序,这些是我的配置 wildfly中的standalon-ha.xml <cache-container name="hibernate"> <transport lock-timeout="60000"/> <local-cache name="local-qu

我正在尝试在Wildfly 14上启用JPA二级缓存。即使我已经添加了配置缓存,也没有发生。日志显示实体正在被缓存,但在检索实体时似乎没有使用缓存

我使用Hibernate作为JPA提供程序,这些是我的配置

  • wildfly中的standalon-ha.xml

    <cache-container name="hibernate">
        <transport 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>
        <replicated-cache name="timestamps"/>
    </cache-container>
    
这是执行数据库查询时从Hibernate获得的输出。每次我看到数据库查询都在进行

[org.hibernate.engine.internal.StatisticalLoggingSessionEventListener] (default task-1) Session Metrics {
664166 nanoseconds spent acquiring 2 JDBC connections;
125120 nanoseconds spent releasing 2 JDBC connections;
1002407 nanoseconds spent preparing 2 JDBC statements;
989037118 nanoseconds spent executing 2 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
1994332 nanoseconds spent performing 2 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
366780 nanoseconds spent executing 1 flushes (flushing a total of 2 entities and 0 collections);
201076 nanoseconds spent executing 2 partial-flushes (flushing a total of 1 entities and 1 collections)

我们为WildFly提供了Infinispan Hibernate缓存教程。我强烈建议你遵循这一点。它目前使用的是WildFly 15,但应该可以与WildFly 14配合使用

要知道的最重要的一点是,您不需要在persistence.xml中定义一半的属性。看

这本书和许多其他Infinispan简单教程都可以找到。特别是,我们有一些简单的教程,展示如何在独立或Spring应用程序中使用Infinispan Hibernate缓存。这些用例的配置可能略有不同

@Entity
@Cacheable
@Table(name = "table_name")
public class EntityName implements Serializable { // }
[org.hibernate.engine.internal.StatisticalLoggingSessionEventListener] (default task-1) Session Metrics {
664166 nanoseconds spent acquiring 2 JDBC connections;
125120 nanoseconds spent releasing 2 JDBC connections;
1002407 nanoseconds spent preparing 2 JDBC statements;
989037118 nanoseconds spent executing 2 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
1994332 nanoseconds spent performing 2 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
366780 nanoseconds spent executing 1 flushes (flushing a total of 2 entities and 0 collections);
201076 nanoseconds spent executing 2 partial-flushes (flushing a total of 1 entities and 1 collections)