问题描述
当我的应用程序发生乐观并发问题时,StaleObjectStateException正在我的应用程序中抛出,而不是在OptimisticLockException(我读我应该期望这个)。无需发布代码,因为它是最基本的并发问题 - 时间戳列中的错误版本。
A StaleObjectStateException is being thrown in my app instead of OptimisticLockException (as I read I should expect this one) when optimistic concurrency problem occurs in my app. No need to post code, as it's the most basic concurrency problem - wrong version in a timestamp column.
我应该如何获取OptimisticLockException,而不是另一个? / p>
How am I supposed to get OptimisticLockException, not the other one?
推荐答案
当使用直接的hibernate API时,会抛出StaleObjectStateException。如果你使用JPA风格的hibernate,会抛出OptimisticLockException。如果这有困惑,请阅读: JPA和Hibernate之间有什么区别?
StaleObjectStateException is thrown when you use straight hibernate API. OptimisticLockException is thrown if you used JPA style hibernate. If this confuses you please read: What's the difference between JPA and Hibernate?
使用try catch块捕获异常:
Use try catch block to catch the exception:
try {
// your hibernate operation here
} catch (OptimisticLockException e) {
// do something (eg: inform user update is conflicting)
}
这是值得注意的OptimisticLockException发生,因为其他事务更新(因此创建的更新版本)的对象之前,你有机会这样做。在UI应用程序中,通常提示用户是否覆盖/丢弃/合并他/她的对象版本
It's worth noting OptimisticLockException occur due to other transaction has updated (hence created newer version of) the object before you got chance to do so. In a UI application it is common to prompt the user whether to overwrite / discard / merge his/her version of the object
这篇关于StaleObjectStateException VS OptimisticLockException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!