jta transcations
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){
//p开发者_如何学Crint error "cannot rollback
}
}
finally{
if(null != utx && utx.getStatus() == Status.STATUS_ACTIVE){
utx.commit();
}
}
I am not understanding why utx is commited in finally?
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.
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
精彩评论