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.
IndexWriter
is thread-safe... – Matthijs Bierman