title: Java, Lucene : Set lock timeout for IndexWriter in Java.
body: <p>I am working on integrating Lucene with our Spring-MVC based application. Currently we have it working, but rarely we get a <code>cannot obtain lock</code> error. After which I have to manually delete the lock file and then it works normally. </p>
  
  <p><strong>How can I set a timeout for Locking the index in Java?</strong> I don't have any XML configuration for Lucene. I added the project library in maven via POM.xml and instantiated the required classes. </p>
  
  <p>Code :</p>
  
  <pre><code>public void saveIndexes(String text, String tagFileName, String filePath, long groupId, boolean type, int objectId) {
          try {
              // path is the indexing directory. 
              File testDir;
              Path suggestionsPath;
              Directory suggestionsDir;
  
              Path phraseSuggestPath;
              Directory phraseSuggestDir;
  
              Directory directory = org.apache.lucene.store.FSDirectory.open(path);
              IndexWriterConfig config = new IndexWriterConfig(new SimpleAnalyzer());
              IndexWriter indexWriter = new IndexWriter(directory, config);
  
              org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
              if (filePath != null) {
                  File file = new File(filePath); // current directory
                  doc.add(new TextField("path", file.getPath(), Field.Store.YES));
              }
              doc.add(new StringField("id", String.valueOf(objectId), Field.Store.YES));
              //  doc.add(new TextField("id",String.valueOf(objectId),Field.Store.YES));
              if (text == null) {
                  if (filePath != null) {
                      FileInputStream is = new FileInputStream(filePath);
                      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                      StringBuilder stringBuffer = new StringBuilder();
                      String line;
                      while ((line = reader.readLine()) != null) {
                          stringBuffer.append(line).append("\n");
                      }
                      stringBuffer.append("\n").append(tagFileName);
                      reader.close();
                      doc.add(new TextField("contents", stringBuffer.toString(), Field.Store.YES));
                  }
              } else {
  
                  FieldType fieldType = new FieldType(TextField.TYPE_STORED);
                  fieldType.setTokenized(false);
                  doc.add(new Field("contents", text+"\n"+tagFileName, fieldType));
              }
              indexWriter.addDocument(doc);
              indexWriter.commit();
              indexWriter.flush();
              indexWriter.close();
              directory.close();
  
              StandardAnalyzer analyzer = new StandardAnalyzer();
              AnalyzingInfixSuggester wordSuggester = new AnalyzingInfixSuggester(suggestionsDir, analyzer);
  
              ArrayList&lt;String&gt; words = new ArrayList&lt;&gt;();
              if (text != null) {
                  text = html2text(text);
                  Pattern pt = Pattern.compile("[^\\w\\s]");
                  Matcher match = pt.matcher(text);
                  while (match.find()) {
                      String s = match.group();
                      text = text.replaceAll("\\" + s, "");
                  }
  
                  if (text.contains(" ")) {
                      Collections.addAll(words, text.split(" "));
  
                  } else {
                      words.add(text);
                  }
                  SuggestionIterator suggestionIterator = new SuggestionIterator(words.iterator());
                  wordSuggester.build(suggestionIterator);
                  wordSuggester.close();
                  suggestionsDir.close();
              }
  
              AnalyzingInfixSuggester phraseSuggester = new AnalyzingInfixSuggester(phraseSuggestDir, analyzer);
              if (text != null) {
                  text = html2text(text);
                  ArrayList&lt;String&gt; phrases = new ArrayList&lt;&gt;();
                  phrases.add(text);
                  SuggestionIterator suggestionIterator = new SuggestionIterator(phrases.iterator());
                  phraseSuggester.build(suggestionIterator);
                  phraseSuggester.close();
                  phraseSuggestDir.close();
              }
  
          } catch (Exception ignored) {
          }
      }
  </code></pre>
  
  <p>Thank you. </p>