Temporary backup tables, if process fails restore

75 views Asked by At

I have an application that periodically downloads a large XML and saves it into a database. What is the proper way to deal with failure during the insertion process. Current process:

  1. download XML
  2. parse XML
  3. clear current data
  4. insert data into database <= spans thousands of rows over 8 different tables

I would like to be able to do a backup before step 4 and if step 4 fails restore the data from backup. Currently using ebean to do persistence. I was trying to use temporary tables and copy all data there and if case of failure copy data back, but I am not sure how to hold on to a single session while waiting for 4 to finish.

1

There are 1 answers

0
Omar Hussien On

you can use Savepoint see savepoints in ebean:

transaction.setNestedUseSavepoint() :

can use transaction.setNestedUseSavepoint() to enable it to use Savepoint for nested transactions. This means that these nested transactions can be rolled back leaving the outer transaction to continue and potentially commit

// start 'outer' transaction
try (Transaction outerTxn = database.beginTransaction()) {

  outerTxn.setNestedUseSavepoint();

  // save backup here...
  

  try (Transaction nestedTransaction = database.beginTransaction()) {
    // nested transaction is a savepoint ...

        // do some piece of work which we might want to either commit or   rollback ...
    otherBean.save();

    if (...) {
      nestedTransaction.rollback();

    } else {
      nestedTransaction.commit();
    }
  }

  // continue using 'outer' transaction ...

  outerTxn.commit();
}