LUCENE-768: make sure IndexReader.close releases write lock even after hitting an exception in one of the methods that acquire the write lock

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@494492 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2007-01-09 17:13:57 +00:00
parent ceaa0c3091
commit 853c03ac43
3 changed files with 40 additions and 3 deletions

View File

@ -295,6 +295,11 @@ Bug fixes
in mergeSegments, when the corrupted segment is merged with in mergeSegments, when the corrupted segment is merged with
segment(s) after it. (Mike McCandless) segment(s) after it. (Mike McCandless)
30. LUCENE-768: Fix case where an Exception during deleteDocument,
undeleteAll or setNorm in IndexReader could leave the reader in a
state where close() fails to release the write lock.
(Mike McCandless)
Optimizations Optimizations
1. LUCENE-586: TermDocs.skipTo() is now more efficient for 1. LUCENE-586: TermDocs.skipTo() is now more efficient for

View File

@ -425,8 +425,8 @@ public abstract class IndexReader {
throws IOException{ throws IOException{
if(directoryOwner) if(directoryOwner)
aquireWriteLock(); aquireWriteLock();
doSetNorm(doc, field, value);
hasChanges = true; hasChanges = true;
doSetNorm(doc, field, value);
} }
/** Implements setNorm in subclass.*/ /** Implements setNorm in subclass.*/
@ -580,8 +580,8 @@ public abstract class IndexReader {
public final synchronized void undeleteAll() throws IOException{ public final synchronized void undeleteAll() throws IOException{
if(directoryOwner) if(directoryOwner)
aquireWriteLock(); aquireWriteLock();
doUndeleteAll();
hasChanges = true; hasChanges = true;
doUndeleteAll();
} }
/** Implements actual undeleteAll() in subclass. */ /** Implements actual undeleteAll() in subclass. */

View File

@ -788,7 +788,39 @@ public class TestIndexReader extends TestCase
fail("delete of out-of-bounds doc number failed to hit exception"); fail("delete of out-of-bounds doc number failed to hit exception");
} }
} }
public void testExceptionReleaseWriteLockJIRA768() throws IOException {
Directory dir = new RAMDirectory();
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
addDoc(writer, "aaa");
writer.close();
IndexReader reader = IndexReader.open(dir);
try {
reader.deleteDocument(1);
fail("did not hit exception when deleting an invalid doc number");
} catch (ArrayIndexOutOfBoundsException e) {
// expected
}
reader.close();
if (IndexReader.isLocked(dir)) {
fail("write lock is still held after close");
}
reader = IndexReader.open(dir);
try {
reader.setNorm(1, "content", (float) 2.0);
fail("did not hit exception when calling setNorm on an invalid doc number");
} catch (ArrayIndexOutOfBoundsException e) {
// expected
}
reader.close();
if (IndexReader.isLocked(dir)) {
fail("write lock is still held after close");
}
}
private String arrayToString(String[] l) { private String arrayToString(String[] l) {
String s = ""; String s = "";
for(int i=0;i<l.length;i++) { for(int i=0;i<l.length;i++) {