我的問題很簡單,當用IndexReader.openIfChanged (reader)
替換以前的reader
時,如何安全關閉oldReader
?如何安全關閉IndexReader?
下面是代碼:(利用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
只是因爲:
- Too many open files in Lucene Indexing when number of users increase
- Lucene Wiki:Too many open files
參考:
我該如何解決這個問題?
確定讀者在致電openIfChanged之前沒有關閉嗎? – vikas 2012-07-08 12:17:43
什麼是併發設置?如果可以同時執行此代碼,則可能會有許多問題。無論如何,你不應該自己實現這個,因爲Lucene已經[提供自己的SearcherManager](http://lucene.apache.org/core/old_versioned_docs/versions/3_5_0/api/core/org/apache/lucene/search/ SearcherManager.html)。 – 2012-07-08 12:58:42
@vikas我的問題描述了錯誤,我必須修改和重寫問題 – Koerr 2012-07-08 14:50:44