JTA交易
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅当事务状态为 STATUS_ACTIVE 时才会调用
finally
块中的提交,这意味着它既未提交也未回滚。它看起来像是一种安全措施,可以确保事务在方法结束时回滚或提交,以防在方法 try 块中忘记 utx.commit() 。The commit in
finally
block is only called if the transaction status isSTATUS_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 caseutx.commit()
was forgotten in the method try block.我想说在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