From 17f39437744585c656cf37d4515a0b115cc73cc8 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Mon, 29 Jan 2007 23:24:57 +0000 Subject: Fixed multiple cursor implementations to be extra paranoid and close the cursor when an exception is thrown. --- .../com/amazon/carbonado/repo/jdbc/JDBCCursor.java | 29 +++++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/repo/jdbc') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java index f419c0d..fa80fde 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java @@ -35,10 +35,10 @@ import com.amazon.carbonado.cursor.AbstractCursor; */ class JDBCCursor extends AbstractCursor { private final JDBCStorage mStorage; - private Connection mConnection; - private PreparedStatement mStatement; - private ResultSet mResultSet; + private final Connection mConnection; + private final PreparedStatement mStatement; + private ResultSet mResultSet; private boolean mHasNext; JDBCCursor(JDBCStorage storage, @@ -49,7 +49,17 @@ class JDBCCursor extends AbstractCursor { mStorage = storage; mConnection = con; mStatement = statement; - mResultSet = statement.executeQuery(); + try { + mResultSet = statement.executeQuery(); + } catch (SQLException e) { + try { + statement.close(); + storage.mRepository.yieldConnection(con); + } catch (Exception e2) { + // Don't care. + } + throw e; + } } public void close() throws FetchException { @@ -62,6 +72,7 @@ class JDBCCursor extends AbstractCursor { throw mStorage.getJDBCRepository().toFetchException(e); } finally { mResultSet = null; + mHasNext = false; } } } @@ -75,6 +86,11 @@ class JDBCCursor extends AbstractCursor { try { mHasNext = rs.next(); } catch (SQLException e) { + try { + close(); + } catch (FetchException e2) { + // Don't care. + } throw mStorage.getJDBCRepository().toFetchException(e); } if (!mHasNext) { @@ -93,6 +109,11 @@ class JDBCCursor extends AbstractCursor { mHasNext = false; return obj; } catch (SQLException e) { + try { + close(); + } catch (FetchException e2) { + // Don't care. + } throw mStorage.getJDBCRepository().toFetchException(e); } } -- cgit v1.2.3