如何引发 OptimisticLockException

发布于 2024-09-24 21:43:43 字数 645 浏览 0 评论 0原文

无法捕获乐观锁异常。

引发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的鱼塘能养鲲 2024-10-01 21:43:43

我认为如果您想捕获 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.

棒棒糖 2024-10-01 21:43:43
try
{
    getEntityRepository().update("Some Persistence Obj");
}
catch (EJBException e)
{
    if (e.getCause() instanceof OptimisticLockException)
    {
        // code goes here
    }
}
try
{
    getEntityRepository().update("Some Persistence Obj");
}
catch (EJBException e)
{
    if (e.getCause() instanceof OptimisticLockException)
    {
        // code goes here
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文