如何引发 OptimisticLockException
无法捕获乐观锁异常。
引发 OptimisticLockException 的一种方法是使用 em.flush()
try{
//some enitity
em.flush()
}
catch(OptimisticLockException ole){}
但我不认为这是最好的解决方案,因为在这个完整的数据库中是刷新的。
另一种解决方法是捕获 EJBException
并在其中找到 RollBackException
。
try{
// some code
}
catch (EJBException ex) {
if (ex.getCausedByException().getCause().toString().
indexOf("javax.transaction.RollbackException")!= -1){
// do work
}
}
}
请帮助您是否有任何其他想法或告诉我哪种方法更好。
Unable to catch optimistic lock exception.
one way to raise OptimisticLockException is by using em.flush()
try{
//some enitity
em.flush()
}
catch(OptimisticLockException ole){}
but i dont think this is best solution beacuse in this full database is flush.
another work around is by catching EJBException
and find RollBackException
in that ..
try{
// some code
}
catch (EJBException ex) {
if (ex.getCausedByException().getCause().toString().
indexOf("javax.transaction.RollbackException")!= -1){
// do work
}
}
}
Please help do you have any other idea or tell me which way is better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为如果您想捕获 OptimisticLockException 并刷新数据或重试操作,第一种方法是合理的方法。至于第二种方式,如果当前事务不活跃,则不会抛出RollbackException。
I think the first way is a reasonable way if you want to catch OptimisticLockException and refresh your data or retry your operation. As for the second way, if the current transaction is not active, there is no RollbackException thrown.