Hibernate(JPA):当几个对象被修改并提交时,如何处理StaleObjectStateException [英] Hibernate (JPA): how to handle StaleObjectStateException when several object has been modified and commited

查看:1073
本文介绍了Hibernate(JPA):当几个对象被修改并提交时,如何处理StaleObjectStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下情景:
一个Db事务,它使用版本控制从不同的表中创建多个行。



例如:
shopLists和产品。 shopList可能包含产品(其数量在shoplist中)并且产品具有其当前库存。

当我插入或编辑shopList时,我想要这些库存为了做到这一点,我打开一个交易,插入/更新shopList,更新每个产品的库存(应用增量),然后提交交易。没什么大不了的。



然而,其他用户可能已经更新了一个或多个共同的产品。甚至更新shopList本身。在这两种情况下,我都会在提交事务时遇到StaleObjectStateException。



问题是:有没有办法确定哪个表导致StaleObjectStateException?



如果产品导致异常,我可以刷新数据库中的所有环境产品,然后重新应用库存量增量。这很好。
如果shopList导致异常,最好将问题简单地报告给用户,以便他可以重新开始。



非常感谢您的帮助。

解决方案

我发现如何。第一:
JPA(或休眠本身)将org.hibernate.StaleObjectStateException异常包装为javax.persistence.OptimisticLockException。
因此,如果您想要捕获正确的异常,请参阅OptimisticLockException。

第二:
当您调用hibernate时,只会抛出OptimisticLockException EntityManager的方法在提交之前刷新。如果直接调用Commit,则会得到另一个异常(我忘记了)。考虑到几乎每个人都会捕获由commit方法发出的异常并进行事务回滚,您将得到一个Rollback的相关异常(不能再记住它)。



第三,最后回答我的原始问题:
您只需从OptimisticLockException实例调用getEntity方法即可获取Versioning错误的来源。这将为您提供任何您需要的相关信息。



感谢所有通过此处的人。
如有任何疑问,只需提问,我会很乐意提供帮助。


Consider the scenario: A Db transaction envolving more than one row from different tables with versioning.

For example: A shopLists and products. Where a shopList may contain products (with their amount in the shoplist) and products have their current stock.

When I insert ou edit a shopList, I want the stock of those products in the shopList to be updated to keep the stock consistant.

To do that, I open a transaction, insert/update the shopList, update the stocks for each product (apply delta) and then commit the transaction. No big deal up to now.

However, other user may have updated one or more products in common. Or even updated the shopList itself. In both cases, I would get a StaleObjectStateException when commiting the transaction.

Question is: Is there a way to determine which table caused the StaleObjectStateException?

In case the product caused the exception, I could refresh all envolved products from the DB and then reapply the stock deltas. And that's fine. In case the shopList caused the exception, it would be better to simply report the issue to the user so he could start over again.

Thanks very much for your help.

解决方案

I found out how.

First things first: the JPA (or the hibernate itself) wraps the org.hibernate.StaleObjectStateException exception as javax.persistence.OptimisticLockException. So, if you want to catch the right exception, go for OptimisticLockException.

Second: The hibernate will only throw the OptimisticLockException when you call EntityManager's method Flush before commit. If you call Commit directly you will get another exception instead (I've forgot which). Considering almost everyone catches those exceptions issued by the commit method and go for a transaction rollback, you'll get a Rollback's related exception (can't remember which once again).

Third and finaly answering my original question: You just have to call the getEntity method from the OptimisticLockException instance to get the origin of the Versioning error. That's going to provide you anything you need related to that.

Thanks for all of those who passed by here. Any questions regarding that, just ask and I'll be glad to help.

这篇关于Hibernate(JPA):当几个对象被修改并提交时,如何处理StaleObjectStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆