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.