You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Satoshi Hasegawa <ha...@p03.worldsoftware.co.jp> on 2004/11/16 02:25:11 UTC

Is opening IndexReader multiple times safe?

Hello,

I need to handle IOExceptions that arise from index access 
(IndexReader#open, #delete, IndexWriter#optimize etc.), and I'm not sure if 
the IndexReader is open when the exception is thrown/caught. Specifically, 
my code is as follows.

        try {
            indexReader.delete(term);
            indexReader.close();
            IndexWriter indexWriter = new IndexWriter(fsDirectory,
                    new JapaneseAnalyzer(), false);
            indexWriter.optimize();
            indexWriter.close();
        } catch (Exception e) {
            // IndexReader may or may not be open
            indexReader = IndexReader.open(path);
            indexReader.undelete();
        }

Is the above code safe? IndexReader may already be open at the beginning of 
the catch clause if the exception was thrown before closing the IndexReader.



---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Re: Is opening IndexReader multiple times safe?

Posted by Satoshi Hasegawa <ha...@p03.worldsoftware.co.jp>.
Thank you, Luke. I decided to branch (use multiple try/catch clauses) so 
that I know if the IndexReader is open or not. Your remark on locking was 
helpful for my understanding of Lucene anyway.

----- Original Message ----- 
From: "Luke Shannon" <ls...@hypermedia.com>
To: "Lucene Users List" <lu...@jakarta.apache.org>
Sent: Tuesday, November 16, 2004 11:45 AM
Subject: Re: Is opening IndexReader multiple times safe?


> Hi Satoshi;
>
> I troubled shooted a problem similar to this by moving around a
> IndexReader.isLocked(indexFileLocation) to determine exactly when the 
> reader
> was closed.
>
> Note: the method throws an error if the index file doesn't exist that you
> are checking on.
>
> Luke
>
> ----- Original Message ----- 
> From: "Satoshi Hasegawa" <ha...@p03.worldsoftware.co.jp>
> To: "Lucene Users List" <lu...@jakarta.apache.org>
> Sent: Monday, November 15, 2004 8:25 PM
> Subject: Is opening IndexReader multiple times safe?
>
>
>> Hello,
>>
>> I need to handle IOExceptions that arise from index access
>> (IndexReader#open, #delete, IndexWriter#optimize etc.), and I'm not sure
> if
>> the IndexReader is open when the exception is thrown/caught. 
>> Specifically,
>> my code is as follows.
>>
>>         try {
>>             indexReader.delete(term);
>>             indexReader.close();
>>             IndexWriter indexWriter = new IndexWriter(fsDirectory,
>>                     new JapaneseAnalyzer(), false);
>>             indexWriter.optimize();
>>             indexWriter.close();
>>         } catch (Exception e) {
>>             // IndexReader may or may not be open
>>             indexReader = IndexReader.open(path);
>>             indexReader.undelete();
>>         }
>>
>> Is the above code safe? IndexReader may already be open at the beginning
> of
>> the catch clause if the exception was thrown before closing the
> IndexReader.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: lucene-user-help@jakarta.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-user-help@jakarta.apache.org
>
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Re: Is opening IndexReader multiple times safe?

Posted by Luke Shannon <ls...@hypermedia.com>.
Hi Satoshi;

I troubled shooted a problem similar to this by moving around a
IndexReader.isLocked(indexFileLocation) to determine exactly when the reader
was closed.

Note: the method throws an error if the index file doesn't exist that you
are checking on.

Luke

----- Original Message ----- 
From: "Satoshi Hasegawa" <ha...@p03.worldsoftware.co.jp>
To: "Lucene Users List" <lu...@jakarta.apache.org>
Sent: Monday, November 15, 2004 8:25 PM
Subject: Is opening IndexReader multiple times safe?


> Hello,
>
> I need to handle IOExceptions that arise from index access
> (IndexReader#open, #delete, IndexWriter#optimize etc.), and I'm not sure
if
> the IndexReader is open when the exception is thrown/caught. Specifically,
> my code is as follows.
>
>         try {
>             indexReader.delete(term);
>             indexReader.close();
>             IndexWriter indexWriter = new IndexWriter(fsDirectory,
>                     new JapaneseAnalyzer(), false);
>             indexWriter.optimize();
>             indexWriter.close();
>         } catch (Exception e) {
>             // IndexReader may or may not be open
>             indexReader = IndexReader.open(path);
>             indexReader.undelete();
>         }
>
> Is the above code safe? IndexReader may already be open at the beginning
of
> the catch clause if the exception was thrown before closing the
IndexReader.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-user-help@jakarta.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org