Hibernate - 查询中可能出现 StaleObjectStateException 吗?

发布于 2024-08-22 17:40:25 字数 407 浏览 3 评论 0原文

当您在一个 tx 内执行两次相同的查询时,如果该查询的结果数据因第一个和第二个查询之间的不同会话内的并发更新而更改,是否有可能在 Hibernate 中获得 StaleObjectStateException

我在此场景中对所有实体使用乐观并发控制。

所以它看起来像这样。

线程 1:事务开始
线程 1:执行查询并检索 key=4711 的 ie 订单
线程 2:在第二个线程中检索、更改并提交与键 4711 相同的顺序
Thread-1:查询再次执行,应该返回 key=4711 的 order

我会在第二个查询的 Thread-1 中收到 StaleObjectStateException 吗?

感谢您的帮助!

托马斯

Is it possible to get a StaleObjectStateException with Hibernate when you do the same query twice inside one tx if the result data of that query gets changed by a concurrent update inside a different session between the first and the second query?

I am using optimistic concurrency control on all entities in this scenario.

So it looks like this.

Thread-1: Transaction begins
Thread-1: query gets executed and retrieves i.e order with key=4711
Thread-2: same order with key 4711 gets retrieved, changed and committed in second thread
Thread-1: query gets executed again and should return order with key=4711

Will I get a StaleObjectStateException in Thread-1 in the second query?

Thanks for your help!

Thomas

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮倦 2024-08-29 17:40:25

免责声明:我还没有尝试过,这是我对休眠的了解所期望的。

在执行第二个查询或提交来自 thread-1 的事务时,您将不会得到 StaleObjectStateException

但是,如果在执行第二个查询之前修改了订单,则订单将在第二个查询之前刷新(假设自动刷新模式和读写事务)查询被执行,这将触发 StaleObjectStateException

Disclaimer: I have not tried it, this is what is expect from what I know of hibernate.

You will not get a StaleObjectStateException when executing the second query nor when the transaction from thread-1 is commited.

However, if if the order was modified before the second query is executed, the order will get flushed (assuming auto-flush mode and read-write transaction) right before the second query gets executed and this will trigger a StaleObjectStateException.

水染的天色ゝ 2024-08-29 17:40:25

我不这么认为。 Thread-1 中的第二个查询甚至不会访问数据库,您将从一级缓存(会话)中获取(陈旧)对象。但是,如果您在第二次查询后更改顺序,则在刷新会话时会出现异常。

I don't think so. The second query in Thread-1 doesn't even hit the database, you'll get the (stale) object from the 1st level cache (the Session). But if you change the order after the second query, you'll get the exception when flushing the session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文