Hibernate 二级缓存 - 何时可以从二级缓存返回过时的集合成员?

发布于 2024-09-03 04:34:36 字数 670 浏览 1 评论 0原文

我和我的一位同事一直在争论二级缓存中缓存的集合何时(如果?)可以返回过时的数据(我们正在使用 ehcache 和 hibernate 3.2.4 )。

场景如下:

  • 父对象 P 被缓存并且是一个实体。
  • 父级有一个子级的集合(包),这些子级被缓存并且是惰性的。
  • 子对象 C 被缓存并且是实体。
  • 我们使用非严格读写作为所有这些的缓存并发策略。
  • 我们的会话工厂是进程范围的,并且只涉及 1 个 JVM。
  • 假设上述所有缓存的缓存区域足够大,可以轻松地将所有子级和父级的所有实例保存在内存中。

在时间 T1:通过执行 session.load(p) 并迭代子集合来加载父 P 和子 C1...CN,以便加载所有 C1...CN。

在时间 T2:另一个会话加载 C1 并更新 C1,更改其一些数据。

什么情况下调用 P.getChildren().get(0) 会返回旧版本的 C1(在时间 T1 加载的 C1 版本)?

我认为有两个:

  1. 如果我与 T1 中的操作处于同一个休眠会话中(在这种情况下,返回会话缓存版本)
  2. 对于非严格读写,可能会出现竞争条件,其中T2 处的更新和加载几乎同时发生,加载赢得了比赛并从缓存中检索过时的对象。 (这种情况我可以改成读写就可以了)

A colleague of mine and I have been having a debate as to when (if?) a collection cached in the second level cache can return stale data (we're using ehcache and hibernate 3.2.4 ).

Here's the scenario:

  • parent object P is cached and is an entity.
  • Parent has a collection (bag) of children which are cached and lazy.
  • Children objects C are cached and are entities.
  • We are using Nonstrict read-write as our cache concurrency strategy for all of these.
  • Our session factory is process scoped and only 1 JVM is involved.
  • Assume that the cache regions for all caches above are large enough to hold all instances of all children and parents in memory easily.

At time T1: Load parent P and children C1...CN by performing a session.load(p) and iterating over the children collection so that all C1...CN are loaded.

At time T2: Another session loads C1 and updates C1 changing some of its data.

Under what circumstances can I call P.getChildren().get(0) and a stale version of C1 (the version of C1 loaded at time T1) will be returned?

I'm thinking there are two :

  1. If I'm in the same hibernate session as the operations in T1 (in which case the session cached version is returned)
  2. With non-strict read write, it would be possible to have a race condition where the update at T2 and the load occur at almost the same time and the load wins the race and retrieves the stale object from the cache. (In which case I can change to read-write and I'll be ok)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文