【问题标题】:spring data ehcache returning stale data in multi node environmentspring data ehcache在多节点环境中返回陈旧数据
【发布时间】:2015-02-18 06:39:32
【问题描述】:

使用以下代码,ehcache 无法反映多节点 (JVM) 环境中的最新数据。相反,spring 数据存储库方法返回过时的数据。这段代码缺少什么来返回最新数据?
来自以下 Foo JPA 存储库的findByName 方法返回过时的数据。在第一次调用时,它会缓存数据。在随后的调用中(在 timeToLiveSeconds 过去之后),即使数据库中实体的数据发生了变化,我仍然得到相同的缓存数据。我希望timeToLiveSeconds 会导致缓存过期并在后续调用时重新加载数据。

在接收 db update 调用的 node-1 上,缓存按预期更新。即findByName 方法返回最新数据。另一个节点上的相同调用返回陈旧数据。 (即使在 timeToLiveSeconds 过去之后)

//Entity class
@Entity
@SecondaryTable(name = "foo_content") 
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
class Foo {
@Id
@GeneratedValue(generator = "test")
@GenericGenerator(name = "test", strategy = "sequence",
 parameters = { @Parameter(name = "sequence", value = "hibernate_seq") })
private int primaryKey;
@NotNull private String name;
private int version;
@NotNull
@Lob
@Column(table = "foo_content")
private String content;
}

//Foo JPA repository:
public interface FooRepository extends JpaRepository<Foo, Integer> {
@Query("SELECT foo FROM Foo foo WHERE foo.name = ?1 AND foo.version = (SELECT MAX(t.version) from Foo t WHERE t.name = ?1)")
@QueryHints({@QueryHint(name = "org.hibernate.cacheable", value = "true")}) Foo findByName(String name);
}

ehcache.xml

<cache name="com.blah.blah.Foo" timeToLiveSeconds="300" maxElementsInMemory="1000" eternal="false"/>

jpaPropertyMap 属性

<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="javax.persistence.sharedCache.mode">DISABLE_SELECTIVE</prop> 
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
<prop key="hibernate.cache.provider_configuration_file_resource_path">META-INF/com/blah/blah/ehcache.xml</prop>

【问题讨论】:

    标签: hibernate spring-data ehcache spring-data-jpa


    【解决方案1】:

    这似乎是this forum 线程中讨论的错误,并且根据Jira,相应的票证是封闭式缺乏测试用例

    所以这是一个仍然存在的错误。解决方法可能是扩展 UpdateTimestampsCache 类以覆盖 isUpToDate 方法。我可能会尽快尝试并发布更新。

    更新: 我使用的 Hibernate 版本是 3.5.2(我同意它的旧版本)。此版本不允许按照this 线程插入自定义UpdateTimestampsCache。由于应用程序的影响,目前我没有升级到最新休眠版本的计划。

    【讨论】:

      猜你喜欢
      • 2019-06-12
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多