【发布时间】: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