我想索引從tomcat服務器獲取的一大組日誌文件。我編寫了代碼打開每個文件,爲每行創建索引,然後使用Apache lucene存儲每行。所有這些都是使用多線程完成的。org.apache.lucene.store.LockObtainFailedException:鎖定獲取超時:
我得到這個異常,當我嘗試這種代碼
org.apache.lucene.store.LockObtainFailedException: Lock obtain timed out:
代碼
if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE)
{
// New index, so we just add the document (no old document can be there):
System.out.println("adding " + path);
indexWriter.addDocument(doc);
} else {
// Existing index (an old copy of this document may have been indexed) so
// we use updateDocument instead to replace the old one matching the exact
// path, if present:
System.out.println("updating " + path);
indexWriter.updateDocument(new Term("path", path), doc);
}
indexWriter.commit();
indexWriter.close();
現在我想,因爲我每次都犯了索引,則可能會導致一個寫鎖。所以我刪除indexWriter.commit();
:
if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE)
{
// New index, so we just add the document (no old document can be there):
System.out.println("adding " + path);
indexWriter.addDocument(doc);
} else {
// Existing index (an old copy of this document may have been indexed) so
// we use updateDocument instead to replace the old one matching the exact
// path, if present:
System.out.println("updating " + path);
indexWriter.updateDocument(new Term("path", path), doc);
}
indexWriter.close();
現在我得到也不例外
問:所以我的問題是,爲什麼indexWriter.commit();導致異常。即使我刪除indexWriter.commit();我在搜索時沒有遇到任何問題。那是我得到我想要的確切結果。那麼爲什麼要使用indexWriter.commit(); ?