【发布时间】:2017-09-08 07:12:53
【问题描述】:
我在 asp-core 项目中使用 Lucene。但是每隔 2-4 天我的索引就会损坏。所以我记录了异常并得到了以下堆栈跟踪:
2017-09-06 10:13:50.8338|发生未处理的异常:锁定 获取超时: SimpleFSLock@e:\inetpub\Static_Data\GKHUB\lucene_index\write.lock: System.IO.IOException:锁定文件 'e:\inetpub\Static_Data\GKHUB\lucene_index\write.lock' 已经 exists.EXCEPTION OCCURRED:Lucene.Net.Store.LockObtainFailedException
经过一番研究,我发现,
无法获取 write.lock 时会引发此异常。当作者试图打开另一个作者已经打开的索引时,就会发生这种情况。[src]
所以第二个更新请求无法更新索引。但是为什么后来整个索引都坏了呢? 我的代码没有做任何特别的事情:
public void UpdateIndex(Document doc, int idToUpadate)
{
var indexwriterConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, Analyzer);
indexwriterConfig.WriteLockTimeout = 5000; // doesn't fix the problem
using (var writer = new IndexWriter(GetLuceneDirectory, indexwriterConfig)) {
try {
writer.UpdateDocument(new Term("Id", idToUpadate.ToString()), doc);
writer.Commit();
}
catch (Exception e) {
Debug.WriteLine(e);
writer.Rollback();
}
}
}
【问题讨论】:
-
你有多少更新?您实际上在每个更新方法上创建了 indexwriter 的新实例,这不是一个好主意
-
通常一次一个,如果有几个,就会出现这个错误。为什么这是个坏主意?
标签: asp.net asp.net-core lucene