Hibernate脏对象的使用
我的代码中有一个 Hibernate 实体。我会获取它并根据其中一个属性的值,例如“isProcessed”,继续:
- 将“isProcessed”的值更改为“Yes”(我检查的属性),
- 将一些任务添加到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 :
- change value of "isProcessed" to "Yes" (the property that i checked)
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以使用同一个会话执行所有查询来分派任务,那么您可能可以将某些内容修补在一起。需要注意的是,您必须了解 hibernate 的缓存机制(是的,这是复数)如何工作。与会话关联的一级缓存将是这里的关键。另外,重要的是要知道执行查询和水合对象不会查找并从一级缓存返回对象......右手不会与左手交谈。
因此,为了完成您想要做的事情(假设您可以继续使用同一个会话...如果您不能做到这一点,那么我认为您运气不好)您可以执行以下操作:
通过调用 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:
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.