0

I am new to the phonegap and Sqlite development. My requirement is to undo a transaction or multiple transactions.

Can I use SAVEPOINT to achieve this? Here is the pseudo code. .

db.transaction(function (transaction) {
        transaction.executeSql('SAVEPOINT XXX;',
            [],
            nullHandler,
            errorHandler);
    });
db.transaction(function (transaction) {
        transaction.executeSql('INSERT INTO .....;',
            [],
            nullHandler,
            errorHandler);
    });
 db.transaction(function (transaction) {
        transaction.executeSql('RELEASE XXX;',
            [],
            nullHandler,
            errorHandler);
    });

I also ready SAVEPOINT is only within a transaction. Does anyone of you have any ideas of how to implement this? If not I need to revamp my whole Data model to account for this requirement.

user2596892
  • 93
  • 1
  • 9

1 Answers1

0

Savepoints must be nested inside a single transaction. Once a transaction has been committed, its contained savepoints cannot be rolled back either.

You would have to reorganize the code as follows:

db.transaction(function(tx) {
    tx.executeSql('SAVEPOINT XXX', []);
    tx.executeSql('INSERT INTO ...', []);
    tx.executeSql('RELEASE XXX', []);
});

If you cannot make these statements part of the same transaction, you cannot rely on the database's transaction/savepoint functionality. You have implement your own undo mechanism and add the necessary information to the database.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • all my inserts are different action user performs but at a later time, users can come in and undo the most recent entry/entries they made. So I cannot bundle all the DML operations into one transaction. – user2596892 Apr 01 '14 at 12:57