From 291ac5d88b79cae83ab812ff866f4364e62ef49f Mon Sep 17 00:00:00 2001 From: broneill Date: Sun, 12 Apr 2015 08:02:29 -0700 Subject: Prevent deadlock between cursor close and TransactionScope cleanup. --- .../java/com/amazon/carbonado/raw/RawCursor.java | 98 +++++++++++----------- 1 file changed, 49 insertions(+), 49 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/amazon/carbonado/raw/RawCursor.java b/src/main/java/com/amazon/carbonado/raw/RawCursor.java index 4b7ef09..b3dbb0b 100644 --- a/src/main/java/com/amazon/carbonado/raw/RawCursor.java +++ b/src/main/java/com/amazon/carbonado/raw/RawCursor.java @@ -111,8 +111,8 @@ public abstract class RawCursor extends AbstractCursor { } public boolean hasNext() throws FetchException { - mLock.lock(); try { + mLock.lock(); try { switch (mState) { case UNINITIALIZED: @@ -137,47 +137,47 @@ public abstract class RawCursor extends AbstractCursor { case HAS_NEXT: return true; } - } catch (FetchException e) { - // Auto-close in response to FetchException. - try { - close(); - } catch (FetchException e2) { - // Ignore. - } - throw e; + } finally { + mLock.unlock(); } - - // Reached the end naturally, so close. - close(); - } finally { - mLock.unlock(); + } catch (FetchException e) { + // Auto-close in response to FetchException. + try { + close(); + } catch (FetchException e2) { + // Ignore. + } + throw e; } + // Reached the end naturally, so close. + close(); + return false; } public S next() throws FetchException, NoSuchElementException { - mLock.lock(); try { - if (!hasNext()) { - handleNoSuchElement(); - throw new NoSuchElementException(); - } + mLock.lock(); try { + if (!hasNext()) { + handleNoSuchElement(); + throw new NoSuchElementException(); + } S obj = instantiateCurrent(); mState = TRY_NEXT; return obj; - } catch (FetchException e) { - // Auto-close in response to FetchException. - try { - close(); - } catch (FetchException e2) { - // Ignore. - } - throw e; + } finally { + mLock.unlock(); } - } finally { - mLock.unlock(); + } catch (FetchException e) { + // Auto-close in response to FetchException. + try { + close(); + } catch (FetchException e2) { + // Ignore. + } + throw e; } } @@ -190,38 +190,38 @@ public abstract class RawCursor extends AbstractCursor { return 0; } - mLock.lock(); try { int actual = 0; - if (hasNext()) { - try { + mLock.lock(); + try { + if (hasNext()) { actual += mReverse ? toBoundedPrevious(amount) : toBoundedNext(amount); - } catch (FetchException e) { - // Auto-close in response to FetchException. - try { - close(); - } catch (FetchException e2) { - // Ignore. - } - throw e; - } - if (actual >= amount) { - return actual; + if (actual >= amount) { + return actual; + } + mState = TRY_NEXT; + // Since state was HAS_NEXT and is forced into TRY_NEXT, actual + // amount skipped is effectively one more. + actual++; } - mState = TRY_NEXT; - // Since state was HAS_NEXT and is forced into TRY_NEXT, actual - // amount skipped is effectively one more. - actual++; + } finally { + mLock.unlock(); } // Reached the end naturally, so close. close(); return actual; - } finally { - mLock.unlock(); + } catch (FetchException e) { + // Auto-close in response to FetchException. + try { + close(); + } catch (FetchException e2) { + // Ignore. + } + throw e; } } -- cgit v1.2.3