【问题标题】:Global transactions with JDBC and multiple Lucene stores使用 JDBC 和多个 Lucene 存储的全局事务
【发布时间】:2014-04-10 14:59:21
【问题描述】:
情况如下:1 次应用更新触发 2 件事:
- 插入到包装在单个事务中的关系数据库中
- 对于每个 Lucene 索引目录
- 写索引
- 提交
目标情况是:只要上述任何步骤失败,一切都应该回滚。应用程序代码在 Scala 中并作为独立应用程序运行(那里没有应用程序服务器)。
应该采取什么方法来解决这个问题?
JTA?如果是这样,你知道与上述情况相似的任何例子吗?
提前致谢!
罗尔夫
【问题讨论】:
标签:
scala
jdbc
transactions
lucene
【解决方案1】:
你可以实现你想要的回滚功能,但是你不会实现原子性。
如果你不介意,下面是伪代码:
// 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();
}
有关 Lucene 处理提交结束的更多详细信息,请参阅this thread。