Question

Here is the situation: 1 application update triggers 2 things:

  1. insertions into a relational database wrapped in a single transaction
  2. for each Lucene index directory
    1. write indices
    2. commit

The targetted situation is: whenever any of the steps above fails, everything should roll back. The application code is in Scala and is run as a standalone application (no application server there).

What should be the approach to tackle this? JTA? If so, do you know any example close to the situation described above?

Thanks in advance! Rolf

Was it helpful?

Solution

You can achieve the rollback feature you want, however you won't achieve atomicity. If you don't mind that, here's the pseudo code below:

// your db connection should have autocommit=off at this point
try {
  preparedStatement.executeQuery(...);

  indexWriter1.addDocument(...);
  indexWriter2.addDocument(...);

  indexWriter1.prepareCommit();
  indexWriter2.prepareCommit();
  // at this point, chances of Lucene failing are pretty slim
  // the only thing left for Lucene now is to rename the segments file

  dbConnection.commit();      
  // if the DB connection commit fails, rollback Lucene writers below

  indexWriter1.commit();
  indexWriter2.commit();
} catch (Throwable t) {
  indexWriter1.rollback();
  indexWriter2.rollback();

  dbConnection.rollback();
}

For more details on Lucene end of handling commits, see this thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top