From 6e16419c18da08323e6f6177c9065ccf1dfd1eaf Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Tue, 12 Apr 2011 18:00:10 +0000 Subject: Handle concurrent close while copying cursor contents. --- .../amazon/carbonado/cursor/AbstractCursor.java | 73 +++++++++++----------- 1 file changed, 38 insertions(+), 35 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/cursor') 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 implements Cursor { public int copyInto(Collection 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 implements Cursor { public int copyInto(Collection 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 implements Cursor { 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 implements Cursor { 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 implements Cursor { 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. + } + } } -- cgit v1.2.3