Hibernate脏对象的使用

发布于 2024-11-08 16:12:53 字数 349 浏览 0 评论 0原文

我的代码中有一个 Hibernate 实体。我会获取它并根据其中一个属性的值,例如“isProcessed”,继续:

  1. 将“isProcessed”的值更改为“Yes”(我检查的属性),
  2. 将一些任务添加到DelayedExecutor。

在我的性能测试中,我发现如果我锤击该函数,就会发生经典的脏读场景,并且我向执行器添加了太多任务,所有这些任务都会被执行。我不能根据任何内容检查队列中对象的相等性,我的意思是java只会执行所有添加的对象。

在将任务添加到执行器之前,我如何使用hibernate的脏对象东西来检查“isProcessed”?会起作用吗?

希望我表达得足够好。

I have a Hibernate Entity in my code. i would fetch that and based on the value of one of the properties ,say "isProcessed" , go on and :

  1. change value of "isProcessed" to "Yes" (the property that i checked)
  2. add some task to a DelayedExecutor.

in my performance test, i have found that if i hammer the function,a classic dirty read scenario happens and i add too many tasks to the Executor that all of them would be executed. i can't use checking the equality of the objects in the Queue based on anything , i mean java would just execute all of them which are added.

how can i use hibernate's dirty object stuff to be able to check "isProcessed" before adding the task to executor? would it work?

hope that i have been expressive enough.

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

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

发布评论

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

评论(1

与之呼应 2024-11-15 16:12:53

如果您可以使用同一个会话执行所有查询来分派任务,那么您可能可以将某些内容修补在一起。需要注意的是,您必须了解 hibernate 的缓存机制(是的,这是复数)如何工作。与会话关联的一级缓存将是这里的关键。另外,重要的是要知道执行查询和水合对象不会查找并从一级缓存返回对象......右手不会与左手交谈。

因此,为了完成您想要做的事情(假设您可以继续使用同一个会话...如果您不能做到这一点,那么我认为您运气不好)您可以执行以下操作:

  1. 执行查询
  2. 对于每个返回的对象,使用 Session 的 get 方法重新加载它
  3. ,检查 isProcessed 标志,并在需要时分派

通过调用 get,您将确保从一级缓存中获取对象...所有脏对象都在其中等待刷新被保留。

对于背景,这是一个非常好的 -关于休眠缓存的书面且有用的文档。

If you can do all of your queries to dispatch your tasks using the same Session, you can probably patch something together. The caveat is that you have to understand how hibernate's caching mechanisms (yes, that's plural) work. The first-level cache that is associated with the Session is going to be the key here. Also, it's important to know that executing a query and hydrating objects will not look into and return objects from the first-level cache...the right hand is not talking to the left hand.

So, to accomplish what you're trying to do (assuming you can keep using the same Session...if you can't do this, then I think you're out of luck) you can do the following:

  1. execute your query
  2. for each returned object, re-load it with Session's get method
  3. check the isProcessed flag and dispatch if need-be

By calling get, you'll be sure to get the object from the first-level cache...where all the dirty objects pending flush are held.

For background, this is an extremely well-written and helpful document about hibernate caching.

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