并发写入Lucene索引
一个索引由多个 IndexWriter 共享。显然,如果正在打开的索引将被其他 IndexWriter 打开,则会抛出 LockObtainFailedException 异常。 我的解决方案是创建具有较长超时的 IndexWriter:
IndexWriterConfig conf= new IndexWriterConfig(Version.LUCENE_30, new SimpleAnalyzer (Version.LUCENE_30));
conf.setWriteLockTimeout(30*1000);//Wait 30 seconds timeout
try{
IndexWriter writer = new IndexWriter(dir, conf);
}catch(LockObtainFailedException e){
System.out.println("give up trying...");
}
对于这种情况有更好的解决方案吗?
编辑:受到蒂洛的启发。我发现 IndexWriter 的 JavaDoc:
IndexWriter 实例是完全线程安全的,这意味着多个线程可以同时调用它的任何方法。如果您的应用程序需要外部同步,则不应在 IndexWriter 实例上进行同步,因为这可能会导致死锁;请使用您自己的(非 Lucene)对象。
An index is shared by multiple IndexWriters. Obviously, LockObtainFailedException
will be thrown if an opening index is about to open by other IndexWriter.
My solution is to create IndexWriter with long timeout:
IndexWriterConfig conf= new IndexWriterConfig(Version.LUCENE_30, new SimpleAnalyzer (Version.LUCENE_30));
conf.setWriteLockTimeout(30*1000);//Wait 30 seconds timeout
try{
IndexWriter writer = new IndexWriter(dir, conf);
}catch(LockObtainFailedException e){
System.out.println("give up trying...");
}
Any better solution for this case?
EDIT: inspired by Thilo. I found JavaDoc of IndexWriter:
IndexWriter instances are completely thread safe, meaning multiple threads can call any of its methods, concurrently. If your application requires external synchronization, you should not synchronize on the IndexWriter instance as this may cause deadlock; use your own (non-Lucene) objects instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单个
Directory
不能被多个IndexWriters
共享。只需在您的线程中共享一个IndexWriter
即可。A single
Directory
cannot be shared by multipleIndexWriters
. Just share oneIndexWriter
amongst your threads.IndexWriter 是完全线程安全的,因此对其进行多线程操作是安全的,但同一个 Index 上不能有多个 IndexWriter。
IndexWriter is totally ThreadSafe , so mutlthread operation on it is safe , but u can't have more than one IndexWriter over the same Index .