diff options
author | Brian S. O'Neill <bronee@gmail.com> | 2011-04-12 18:00:10 +0000 |
---|---|---|
committer | Brian S. O'Neill <bronee@gmail.com> | 2011-04-12 18:00:10 +0000 |
commit | 6e16419c18da08323e6f6177c9065ccf1dfd1eaf (patch) | |
tree | 169ed8e905bfb79f9df07ef362d521d774598f6d /src/main/java/com | |
parent | e4e0840f17b1961ad1826699a8993d06ccfbc75a (diff) |
Handle concurrent close while copying cursor contents.
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/amazon/carbonado/cursor/AbstractCursor.java | 73 |
1 files changed, 38 insertions, 35 deletions
diff --git a/src/main/java/com/amazon/carbonado/cursor/AbstractCursor.java b/src/main/java/com/amazon/carbonado/cursor/AbstractCursor.java index a7be434..17e9f51 100644 --- a/src/main/java/com/amazon/carbonado/cursor/AbstractCursor.java +++ b/src/main/java/com/amazon/carbonado/cursor/AbstractCursor.java @@ -21,6 +21,7 @@ package com.amazon.carbonado.cursor; import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import java.util.NoSuchElementException;
import com.amazon.carbonado.Cursor;
import com.amazon.carbonado.FetchException;
@@ -39,17 +40,18 @@ public abstract class AbstractCursor<S> implements Cursor<S> { public int copyInto(Collection<? super S> c) throws FetchException {
try {
int count = 0;
- while (hasNext()) {
- c.add(next());
- count++;
+ try {
+ while (hasNext()) {
+ c.add(next());
+ count++;
+ }
+ } catch (NoSuchElementException e) {
+ // Race condition cause by concurrent repository close.
+ silentClose();
}
return count;
} catch (FetchException e) {
- try {
- close();
- } catch (Exception e2) {
- // Don't care.
- }
+ silentClose();
throw e;
}
}
@@ -57,17 +59,18 @@ public abstract class AbstractCursor<S> implements Cursor<S> { public int copyInto(Collection<? super S> c, int limit) throws FetchException {
try {
int count = 0;
- while (--limit >= 0 && hasNext()) {
- c.add(next());
- count++;
+ try {
+ while (--limit >= 0 && hasNext()) {
+ c.add(next());
+ count++;
+ }
+ } catch (NoSuchElementException e) {
+ // Race condition cause by concurrent repository close.
+ silentClose();
}
return count;
} catch (FetchException e) {
- try {
- close();
- } catch (Exception e2) {
- // Don't care.
- }
+ silentClose();
throw e;
}
}
@@ -78,11 +81,7 @@ public abstract class AbstractCursor<S> implements Cursor<S> { copyInto(list);
return list;
} catch (FetchException e) {
- try {
- close();
- } catch (Exception e2) {
- // Don't care.
- }
+ silentClose();
throw e;
}
}
@@ -93,11 +92,7 @@ public abstract class AbstractCursor<S> implements Cursor<S> { copyInto(list, limit);
return list;
} catch (FetchException e) {
- try {
- close();
- } catch (Exception e2) {
- // Don't care.
- }
+ silentClose();
throw e;
}
}
@@ -112,19 +107,27 @@ public abstract class AbstractCursor<S> implements Cursor<S> { try {
int count = 0;
- while (--amount >= 0 && hasNext()) {
- next();
- count++;
+ try {
+ while (--amount >= 0 && hasNext()) {
+ next();
+ count++;
+ }
+ } catch (NoSuchElementException e) {
+ // Race condition cause by concurrent repository close.
+ silentClose();
}
-
return count;
} catch (FetchException e) {
- try {
- close();
- } catch (Exception e2) {
- // Don't care.
- }
+ silentClose();
throw e;
}
}
+
+ private void silentClose() {
+ try {
+ close();
+ } catch (Exception e2) {
+ // Don't care.
+ }
+ }
}
|