Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to roll back a transaction on Android?

Tags:

java

android

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?

like image 377
Pentium10 Avatar asked Mar 08 '10 11:03

Pentium10


People also ask

Can transactions be rolled back?

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.

What does it mean to rollback a transaction?

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.

What causes a transaction to rollback?

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.


1 Answers

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.

like image 65
CommonsWare Avatar answered Oct 31 '22 22:10

CommonsWare