Here is the standard idiom for transactions:
db.beginTransaction();
try {
...
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
I want to add a catch block, and I want to issue a rollback. Is it possible, and at all do I need it?
A transaction cannot be rolled back after a COMMIT TRANSACTION statement is executed, except when the COMMIT TRANSACTION is associated with a nested transaction that is contained within the transaction being rolled back.
A rollback is the operation of restoring a database to a previous state by canceling a specific transaction or transaction set. Rollbacks are either performed automatically by database systems or manually by users.
A Rollback is executed if a transaction aborts. It makes the whole Transaction undone. A transaction could be aborted through several errors that might occour when running the transaction or if you does an unplaned power off of your system.
You do not need it.
If there is an exception in the ...
in your above code, the code you already have will roll back the transaction. The finally {}
block is executed after the catch() {}
block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With