简体   繁体   English

Spring数据发现未获取最新数据

[英]Spring data findone not fetching latest data

I have spring data configured for my application where my DAO interface extends org.springframework.data.repository.CrudRepository to performer simple database operations. 我为我的应用程序配置了spring数据,其中DAO接口将org.springframework.data.repository.CrudRepository扩展为执行者简单的数据库操作。 One of the DAO's findOne(id) method is not returning the latest record of a table even though my database has the latest data. DAO的findOne(id)方法之一不返回表的最新记录,即使我的数据库具有最新数据也是如此。 It is always returning the old version of the record. 它总是返回记录的旧版本。 There is no caching configured for the application. 没有为应用程序配置缓存。 Is there any default JPA level caching which is preventing it to return the latest data? 是否有任何默认的JPA级别缓存会阻止其返回最新数据?

EDIT: I am calling em.flush() after saving the record, so the subsequent call to findOne(id) must give me the latest record. 编辑:保存记录后,我正在调用em.flush(),因此对findOne(id)的后续调用必须给我最新的记录。

However, I see the one potential issue, my entire application is running in a clustered environment, so calling em.flush() might be refreshing the data only in one node where the data is being updated and all other nodes might still have the old data which may be causing the subsequent call to findOne(id) method is going through some other node where flush may not have happened yet. 但是,我看到了一个潜在的问题,我的整个应用程序都在集群环境中运行,因此调用em.flush()可能仅在一个要更新数据的节点中刷新数据,而其他所有节点可能仍然具有旧数据。可能导致随后调用findOne(id)方法的数据正在通过其他可能尚未发生刷新的节点。 Is there a way to flush across nodes or is there any default timeout where the data gets refreshed automatically? 有没有一种方法可以跨节点刷新,或者是否有默认超时可以自动刷新数据?

You can try disabling second level cache for hibernate . 您可以尝试为休眠禁用二级缓存 As you are using a clustered environment, when you create/update anything, as per my understanding it updates the cache for one cluster where the request went but not the other one. 在您使用集群环境时,根据我的理解,当您创建/更新任何内容时,它都会更新请求所在的一个集群的缓存,而不会更新另一个集群。 However, when you try to fetch the same record and when your request goes to the other cluster, it will give you the old record but not the latest one. 但是,当您尝试获取相同的记录并且您的请求转到另一个群集时,它将为您提供旧记录,而不是最新记录。

You can use the following configuration : 您可以使用以下配置:

jpa:
  properties:
    hibernate:
      #################### HIBERNATE EHCACHE ####################
      #Disable second level cache for hibernate
      cache:
        use_second_level_cache: false
        auto_evict_collection_cache: false
        region:
          factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
        use_query_cache: true

Also, instead of using findOne() method, you should use findById() as in the 2.0 version, Spring-Data-JPA modified findOne() and now it returns an Optional. 另外,除了使用findOne()方法之外,还应该像2.0版本一样使用findById() ,Spring-Data-JPA修改了findOne(),现在它返回了Optional。 Which is not so bad to prevent NullPointerException. 防止NullPointerException并不是很糟糕。

See this for more info. 请参阅此以获取更多信息。

Correct me if I'm mistaken. 如果我弄错了,请纠正我。

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

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