Sanket
Sanket

Reputation: 149

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){
    //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?

Upvotes: 3

Views: 594

Answers (2)

Nitin Verma
Nitin Verma

Reputation: 508

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

Upvotes: 1

Guillaume
Guillaume

Reputation: 5555

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.

Upvotes: 4

Related Questions