Women in Technology

Hear us Roar



Article:
  Adding Transactions to Servlets with JOTM
Subject:   Transaction is flawed
Date:   2003-07-31 15:32:01
From:   anonymous2
if (success) {
utx.commit();
} else {
utx.rollback();
}


What happens if the commit fails? The transaction is never rolled back.


I usually do the following.


boolean success = false;
try{
//stuff
utx.commit();
success = true;
}
finally{
if(!success)
utx.rollback();
}

Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • Transaction is flawed
    2003-08-07 01:42:12  jeff.mesnil [View]

    The transaction manager is there to ensure that if a problem occurs in the commit() method, the transaction is then automatically rolled back.

    Your code may also not worked with all JTA transaction managers: the commmit() and rollback() specifications states that "When this method completes, the thread becomes associated with no transaction".
    If your call to commit() fails, the call to rollback() should then throw a IllegalStateException because your transaction has been disassociated from your thread in the commit() method.

    It's really up to the commit() method to cleanly rollback the transaction because JTA implicitely states that you can't call *both* commit() and rollback() on the same transaction.

    jeff