JTA交易

发布于 2024-10-04 22:38:26 字数 366 浏览 0 评论 0原文

我有一个 jta 交易代码如下:

try{
  //start jta user transcation utx


//commit utx

}catch(Exception ex){
   try{
     //rollback utx
   }catch(Exception){
    //print error "cannot rollback
   }
}
finally{
  if(null != utx && utx.getStatus() == Status.STATUS_ACTIVE){
                    utx.commit();
  }
}

我不明白为什么 utx 会在最后提交?

I have a code for jta transcations as follows:

try{
  //start jta user transcation utx


//commit utx

}catch(Exception ex){
   try{
     //rollback utx
   }catch(Exception){
    //print error "cannot rollback
   }
}
finally{
  if(null != utx && utx.getStatus() == Status.STATUS_ACTIVE){
                    utx.commit();
  }
}

I am not understanding why utx is commited in finally?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

悲凉≈ 2024-10-11 22:38:26

仅当事务状态为 STATUS_ACTIVE 时才会调用 finally 块中的提交,这意味着它既未提交也未回滚。它看起来像是一种安全措施,可以确保事务在方法结束时回滚或提交,以防在方法 try 块中忘记 utx.commit() 。

The commit in finally block is only called if the transaction status is STATUS_ACTIVE, meaning it has neither been committed nor rollbacked. it looks like a security to ensure the transaction is either rollbacked or committed at the end of the method, in case utx.commit() was forgotten in the method try block.

┼── 2024-10-11 22:38:26

我想说在finally块中进行这样的提交并不是一个好的做法。存在半途而废的风险,这对于大多数目的来说都是危险的。检查事务状态为“活动”后,回滚将是更好的选择。

HTH。

谢谢,
镍丁

I would say it is not a good practice to do such commit in the finally block. There is a risk of committing a half-way work which would be dangerous for most purposes. A rollback would be a better option after checking the state of the transaction to be ACTIVE.

HTH.

Thanks,
Nitin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文