我的问题很简单,当使用IndexReader.openIfChanged (reader)
替换以前的reader
时,如何安全地关闭oldReader
?
下面是代码:(使用Lucene 3.5)
IndexReader newReader=IndexReader.openIfChanged(reader);
if(newReader!=null){
IndexReader oldReader=reader;
IndexSearcher oldSearcher=searcher;
reader=newReader;
searcher=new IndexSearcher(newReader);
oldSearcher.close();
oldReader.close();//or oldReader.decRef(),result is the same
}
该代码在守护线程中,每5秒运行一次
IndexReader
实例(reader
对象)是全局唯一的由于此更改,我得到一个例外:
org.apache.lucene.store.AlreadyClosedException: this IndexReader is closed
at org.apache.lucene.index.IndexReader.ensureOpen(IndexReader.java:297)
at org.apache.lucene.index.IndexReader.getSequentialSubReaders(IndexReader.java:1622)
at org.apache.lucene.search.TermQuery$TermWeight.scorer(TermQuery.java:98)
at org.apache.lucene.search.BooleanQuery$BooleanWeight.scorer(BooleanQuery.java:298)
at org.apache.lucene.search.BooleanQuery$BooleanWeight.scorer(BooleanQuery.java:298)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:577)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:517)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:487)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:400)
at org.zenofo.index.IndexManager.query(IndexManager.java:392)
...
使用
IndexManager.java:392
对象的reader
(IndexReader
实例,全局唯一)IndexManager.query
方法具有大量并发请求,所有请求都使用一个全局唯一的IndexReader
实例(reader
对象)我需要关闭
oldReader
只是因为:参考:
我该如何解决这个问题?
最佳答案
看一下NRTManager和SearcherManager。您实际上不必自己处理。