spring 数据 ehcache 在多节点环境中返回陈旧数据

spring data ehcache returning stale data in multi node environment

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

在接收到数据库更新调用的节点 1 上,缓存按预期更新。即 findByName 方法 returns 最新数据。对另一个节点 return 的陈旧数据进行相同的调用。 (即使在 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>

这似乎是 this forum thread and the corresponding ticket is closed sighting lack of testcase as per Jira

中讨论的错误

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

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