Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Duplicate
-
2.3.2
-
None
-
Windows XP, JDK 1.6
-
New
Description
NullPointerException is thrown while indexing within multiple threads but using one single IndexWriter instance. According to "Lucene in Action" this should do: "...a single instance of either class can be shared among multiple threads, and all calls to its index-modifying methods will be properly synchronized so that index modifications are executed one after the other."
Following my exception trace:
java.lang.NullPointerException
at org.apache.lucene.index.IndexFileDeleter.decRef(IndexFileDeleter.java:475)
at org.apache.lucene.index.IndexWriter.commitTransaction(IndexWriter.java:1986)
at org.apache.lucene.index.IndexWriter.addIndexes(IndexWriter.java:2219)
at com.shi.marketingspy.test.SHILuceneWritingFromThreadsTest$testthread.run(SHILuceneWritingFromThreadsTest.java:48)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
Following my test program:
package com.shi.marketingspy.test;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
public class SHILuceneWritingFromThreadsTest
{
public static void log(String s, com.shi.jipmisc misc)
public static class testthread implements Runnable
{
public org.apache.lucene.index.IndexWriter indexwriter;
public int iThread;
public boolean bWithOptimize;
public com.shi.jipmisc misc;
public testthread(org.apache.lucene.index.IndexWriter indexwriter, int iThread, boolean bWithOptimize, com.shi.jipmisc misc)
{ this.indexwriter = indexwriter; this.iThread = iThread; this.bWithOptimize = bWithOptimize; this.misc = misc; } public void run()
{
try
{
Analyzer anal = new com.shi.lucene.index.SHIAnalyser(null,null);
org.apache.lucene.store.Directory tmpidxdirectory = new org.apache.lucene.store.RAMDirectory();
org.apache.lucene.index.IndexWriter tempindexwriter = new IndexWriter(tmpidxdirectory, anal, true);
Document doc = new Document();
for(int ifld=0; ifld < 9; ifld++)
tempindexwriter.addDocument(doc);
tempindexwriter.optimize();
org.apache.lucene.store.Directory[] idxs = new org.apache.lucene.store.Directory[1];
idxs[0] = tmpidxdirectory;
//todo: del-call...
this.indexwriter.addIndexes(idxs);
if (this.bWithOptimize)
this.indexwriter.optimize();
}
catch(Throwable e)
}
}
public SHILuceneWritingFromThreadsTest()
{
}
public static void main(String[] args)
{ testluceneconcurrency(false); testluceneconcurrency(true ); } public static java.util.concurrent.ExecutorService threadpool_optimized = java.util.concurrent.Executors.newFixedThreadPool(9999999);
public static java.util.concurrent.ExecutorService threadpool_not_optimized = java.util.concurrent.Executors.newFixedThreadPool(9999999);
public static void testluceneconcurrency(boolean bWithOptimize)
{
com.shi.jipmisc.createDir("temp", "c:");
com.shi.jipmisc.createDir("testluceneconcurrency", "c:/temp");
com.shi.jipmisc.createDir(""+bWithOptimize, "c:/temp/testluceneconcurrency");
com.shi.jipmisc misc = new com.shi.jipmisc();
misc.setMyPath("C:/temp/testluceneconcurrency");
misc.setDebug(true);
misc.emptyDir("c:/temp/testluceneconcurrency/"+bWithOptimize);
Analyzer anal = new com.shi.lucene.index.SHIAnalyser(null,null);
org.apache.lucene.index.IndexWriter indexwriter = null;
try
catch(Throwable e)
{ log("SHILuceneWritingFromThreadsTest:testluceneconcurrency\tERR:0("+bWithOptimize+"):"+e.getMessage(), misc); return; }log("SHILuceneWritingFromThreadsTest:testluceneconcurrency\tINF:step0("bWithOptimize")(BEGIN)-------------------------------------", misc);
for(int idoc=0; idoc < 9999; idoc++)
{
Document doc = new Document();
for(int ifld=0; ifld < 9; ifld++)
try
{ indexwriter.addDocument(doc); }catch(Throwable e)
{ log("SHILuceneWritingFromThreadsTest:testluceneconcurrency\tERR:1("+bWithOptimize+"):"+e.getMessage(), misc); return; } }
log("SHILuceneWritingFromThreadsTest:testluceneconcurrency\tINF:step1("bWithOptimize")", misc);
try
catch(Throwable e)
{ log("SHILuceneWritingFromThreadsTest:testluceneconcurrency\tERR:2("+bWithOptimize+"):"+e.getMessage(), misc); return; } log("SHILuceneWritingFromThreadsTest:testluceneconcurrency\tINF:step2("bWithOptimize")", misc);
java.util.concurrent.ExecutorService threadpool = bWithOptimize ? threadpool_optimized : threadpool_not_optimized;
for(int it=0; it < 999; it++)
log("SHILuceneWritingFromThreadsTest:testluceneconcurrency\tINF:step3("bWithOptimize")", misc);
threadpool.shutdown(); //=execute threads and wait til all threads finish
com.shi.jipmisc.wait(999, misc);
log("SHILuceneWritingFromThreadsTest:testluceneconcurrency\tINF:step4("bWithOptimize")", misc);
try
{ //indexwriter.optimize(); //indexwriter.close(); }catch(Throwable e)
{ log("SHILuceneWritingFromThreadsTest:testluceneconcurrency\tERR:9("+bWithOptimize+"):"+e.getMessage(), misc); return; } log("SHILuceneWritingFromThreadsTest:testluceneconcurrency\tINF:step5("bWithOptimize")(END)-----------------------------", misc);
}
}