简体   繁体   English

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

[英]hibernate cached query not updated when new record inserted

We have a EHCache cluster, hibernate and Mysql.我们有一个 EHCache 集群,hibernate 和 Mysql。

Everything is working almost fine.一切正常。 Criteria searches are being cached and when records are modified on other members of the clusters the cached queries are updated instantly on the other servers.标准搜索被缓存,当集群的其他成员上的记录被修改时,缓存的查询会立即在其他服务器上更新。

however, my problem is when new records are inserted.但是,我的问题是插入新记录时。 The cached queries on that table do not know about it until the cached query are expired.在缓存查询过期之前,该表上的缓存查询不知道它。

I probably have missed something on my EHcache.xml configuration, but I have no idea what could it be.我可能在我的 EHcache.xml 配置上遗漏了一些东西,但我不知道会是什么。

Any ideas?有任何想法吗?

EHCache.xml follows: EHCache.xml 如下:

` `

<!--<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>

` `

I'm afraid it's a little too late for the author, but I thought my answer can be useful for anyone else with the same problem.恐怕作者为时已晚,但我认为我的回答对遇到同样问题的其他人有用。

You should remember how the StandardQueryCache and the UpdateTimestampsCache work.您应该记住StandardQueryCacheUpdateTimestampsCache是如何工作的。 When the query cache is checked for a query, the time the query was cached is compared to the timestamps of the last update of all tables in the query.检查查询缓存时,会将查询缓存的时间与查询中所有表的最后更新时间戳进行比较。 If any tables were updated after the query was cached, the cached result gets discarded and the database is used instead.如果在缓存查询后更新了任何表,则缓存结果将被丢弃并使用数据库。 Timestamps of the last update for each table are stored in the UpdateTimestampsCache .每个表的最后一次更新的时间戳存储在UpdateTimestampsCache中。

In the above configuration the values from the UpdateTimestampsCache are not copied to other members of the cluster, so Hibernate looks at the old timestamps and thinks that a cached query is up-to-date.在上述配置中, UpdateTimestampsCache中的值不会复制到集群的其他成员,因此 Hibernate 会查看旧时间戳并认为缓存的查询是最新的。 As a result newly inserted records are neglected.结果,新插入的记录被忽略。 To fix it simply set replicateUpdatesViaCopy for the UpdateTimestampsCache to true.要修复它,只需将UpdateTimestampsCachereplicateUpdatesViaCopy设置为 true。

Reference: Ehcache configuration参考: Ehcache 配置

Note that the eternal attribute, when set to "true", overrides timeToLive and timeToIdle so that no expiration can take place.请注意,当设置为“true”时,永恒属性会覆盖 timeToLive 和 timeToIdle,这样就不会发生过期。

You have 1 eternal attribute setting to true.您有 1 个永恒属性设置为真。 Maybe you can try setting it to false and see if it helps?也许您可以尝试将其设置为 false 看看是否有帮助?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM