StaleObjectStateException vs OptimisticLockException

岁酱吖の 提交于 2019-11-29 04:04:23
gerrytan

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?

Use try catch block to catch the exception:

try {
  // your hibernate operation here
} catch (OptimisticLockException e) {
  // do something (eg: inform user update is conflicting)
}

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

As of my analysis of Hibernate 3.5.2 (now bit old), I found they sometimes throw OptimisticLockException and sometimes StaleObjectStateException. Batch operations even throw StaleStateException, which is a superclass of StaleObjectStateException, but don't have the entity instance.

It seems to me as an unfinished refactoring, you probably need to catch both and react to both the same way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!