Friday, October 7, 2011

org.apache.lucene.store.LockObtainFailedException

While creating index files for Lucene 3.0.2 search engine I got below mentioned error.

Exception:

org.apache.lucene.store.LockObtainFailedException: Lock obtain timed out: NativeFSLock@/export/home/Lucene/indexFiles/write.lock
        at org.apache.lucene.store.Lock.obtain(Lock.java:84)
        at org.apache.lucene.index.IndexWriter.init(IndexWriter.java:1060)
        at org.apache.lucene.index.IndexWriter.(IndexWriter.java:882)


Solution :

Check for IndexWriter object. Make sure you close IndexWriter object after completing index creation.
Check for any exceptions and close IndexWriter object if exception occurs.

Possible reason is there may be any exception occurred and you missed to close the IndexWriter object in catch block.


code snippte:

// Store the index in file
Directory directory = new SimpleFSDirectory(new File(indexLoc));     
IndexWriter iwriter = new IndexWriter(directory, analyzer, isNew,MaxFieldLength.UNLIMITED);

iwriter.optimize();
iwriter.close();

If you need complete tested code for Lucene 3.0.2 example, click here