spring数据ehcache在多节点环境中返回过时的数据



使用以下代码,ehcache不会反映多节点(JVM)环境中的最新数据。相反,spring数据存储库方法返回过时的数据。返回最新数据的代码中缺少什么
FooJPA存储库中的findByName方法返回陈旧数据。在第一次调用时,它会缓存数据。在随后的调用中(在timeToLiveSeconds过去之后),即使DB中实体的数据发生了更改,我仍然会得到相同的缓存数据。我预计timeToLiveSeconds将导致缓存过期,并在随后的调用中重新加载数据

在接收数据库更新调用的节点1上,缓存将按预期进行更新。即CCD_ 3方法返回最新的数据。对另一个节点的相同调用返回过时的数据。(即使在ToLiveSeconds时间过去之后)

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

正如本论坛帖子中所讨论的,这似乎是一个错误,相应的票证是关闭的,因为根据Jira ,缺少测试用例

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

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

最新更新