amjn wrote:
try{
openConnection();
new DAO().create();
commitTrans();
}
catch(RuntimeException e){ // the line was changed.
rollbackTrans();
}
finally{
closeConnection();
}
This code is even more dangerous, because RuntimeException is a
subclass of Exception, which is subclass of Throwable. There are other throwables (Errors), which are not Exceptions. If code in your try block will throw Error, transaction won't be aborted.
amjn wrote:
try{
openConnection();
new DAO().create();
}
finally{
try{
commitTrans();
}
catch(RuntimeException e)}
rollbackTrans();
}
closeConnection();
}
This code is also wrong. You will commit your transaction even if exception is thrown in try block.
I recomend you to refresh your memories, regarding exception handling in Java, especially how finally block works.
Correct sequence it your case would be something like this:
Code:
try
{
beginTransaction();
// perform transactional work
commitTransaction();
}
finally
{
// if transaction is still active, then we have an exception
if ( currentTransaction().isActive() )
{
rollbackTransaction();
}
}