Java hibernate缓存查询在插入新记录时未更新

Java hibernate缓存查询在插入新记录时未更新,java,hibernate,spring,ehcache,Java,Hibernate,Spring,Ehcache,我们有一个EHCache集群、hibernate和Mysql 一切都很顺利。条件搜索正在被缓存,当在集群的其他成员上修改记录时,缓存的查询会在其他服务器上立即更新 然而,我的问题是当插入新记录时。在缓存的查询过期之前,该表上的缓存查询不知道它 我可能错过了EHcache.xml配置中的某些内容,但我不知道会是什么 有什么想法吗 EHCache.xml如下所示: ` `参考: 请注意,当设置为“true”时,external属性将覆盖timeToLive和timeToIdle,因此不会发生过

我们有一个EHCache集群、hibernate和Mysql

一切都很顺利。条件搜索正在被缓存,当在集群的其他成员上修改记录时,缓存的查询会在其他服务器上立即更新

然而,我的问题是当插入新记录时。在缓存的查询过期之前,该表上的缓存查询不知道它

我可能错过了EHcache.xml配置中的某些内容,但我不知道会是什么

有什么想法吗

EHCache.xml如下所示:

`


`

参考:

请注意,当设置为“true”时,external属性将覆盖timeToLive和timeToIdle,因此不会发生过期


您有1个永恒属性设置为true。也许你可以试着将其设置为false,看看是否有帮助?

恐怕对作者来说有点太晚了,但我认为我的答案对其他有同样问题的人可能有用

您应该记住
StandardQueryCache
UpdateTimestampsCache
是如何工作的。在检查查询缓存以进行查询时,将缓存查询的时间与查询中所有表的上次更新的时间戳进行比较。如果在缓存查询后更新了任何表,则缓存的结果将被丢弃,而使用数据库。每个表上次更新的时间戳存储在
UpdateTimestampsCache


在上述配置中,
UpdateTimestampsCache
中的值不会复制到集群的其他成员,因此Hibernate会查看旧的时间戳,并认为缓存的查询是最新的。因此,新插入的记录被忽略。要修复它,只需将
UpdateTimestampsCache
replicateUpdatesViaCopy
设置为true。

如果不发布ehcache配置,很难告诉您错过了什么:)matt,好了,用conf更新。我可以使用ehcache调试日志级别实际看到插入(put)在缓存条件查询的主机中接收的消息。但是由于某些原因,查询没有过期。那么hibernate配置呢?如何进行插入?您是否有可能通过JDBC插入记录而不通过hibernate?查询将过期,然后找到插入的行。我已经设置了120秒的过期时间。两分钟后,查询将获取插入的行。然而,我认为EHCache应该更智能,并发现插入了一条新记录并使该表上的缓存查询无效。。。或者类似的……我也有同样的问题。“replicateUpdatesViaCopy”设置为true。“永恒”设置为false。查询缓存/更新的时间戳缓存仍然没有失效/复制。还有其他想法吗?@lars首先,检查复制是否有效。如果使用JGroups,请参阅“运行演示程序”。请注意
-Djava.net.preferIPv4Stack=true
部分,如果没有它您无法运行演示,您还应该将它添加到配置中。其次,Ehcache配置中的其他内容可能有问题。是一个对我有用的例子。明白了,基本错误。我使用“org.hibernate.cache.UpdateTimestampsCache”而不是“org.hibernate.cache.spi.UpdateTimestampsCache”作为时间戳缓存的名称。
<!--<diskStore path="java.io.tmpdir"/>-->

<!-- means for cache replication -->

<cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
    properties="connect=
        TCP(bind_port=10700):
        S3_PING(...):
        MERGE2(max_interval=30000;min_interval=10000):
        FD_SOCK(start_port=0):
        FD(timeout=3000;max_tries=3):
        VERIFY_SUSPECT(timeout=1500):
        BARRIER():
        pbcast.NAKACK(use_mcast_xmit=false;gc_lag=0;retransmit_timeout=300,600,1200,2400,4800;discard_delivered_msgs=true):
        UNICAST(timeout=300,600,1200):
        pbcast.STABLE(stability_delay=1000;desired_avg_gossip=50000;max_bytes=400K):
        pbcast.GMS(print_local_addr=true;join_timeout=300;view_bundling=true):
        FC(max_credits=2M;min_threshold=0.10):
        FRAG2(frag_size=60K):
        pbcast.STREAMING_STATE_TRANSFER()"
    propertySeparator="::" />    

<!-- default query cache to be used for all queries without an explicit cache -->

<cache
    name="org.hibernate.cache.StandardQueryCache"
    maxElementsInMemory="100"
    eternal="false"
    timeToLiveSeconds="600"
    overflowToDisk="false"
    statistics="true">
    <cacheEventListenerFactory
        class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
        properties="replicateAsynchronously=true, replicatePuts=true,
        replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true" />
</cache>    

<!-- timestamps of particular last update time to tables -->

<cache
    name="org.hibernate.cache.UpdateTimestampsCache"
    maxElementsInMemory="5000"
    eternal="true"
    overflowToDisk="false"
    statistics="true">
    <cacheEventListenerFactory
        class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
        properties="replicateAsynchronously=true, replicatePuts=true,
        replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true" />
</cache>

<!-- default cache to use for all cacheable entities without an explicit cache -->

<defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="600"
        timeToLiveSeconds="600"
        overflowToDisk="false"
        maxElementsOnDisk="10000000"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="600"
        memoryStoreEvictionPolicy="LRU"
        statistics="true">
        <cacheEventListenerFactory
            class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
            properties="replicateAsynchronously=true, replicatePuts=true,
            replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true" />
</defaultCache>