From 48d34d65928010cf022913bf0126629604a6398d Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Tue, 30 Jan 2007 02:52:04 +0000 Subject: More work to ensure JDBC resources are properly cleaned up. --- .../com/amazon/carbonado/repo/jdbc/JDBCCursor.java | 16 +++------ .../amazon/carbonado/repo/jdbc/JDBCStorage.java | 40 ++++++++++++++++------ 2 files changed, 34 insertions(+), 22 deletions(-) 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 fa80fde..13c7548 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java @@ -41,6 +41,10 @@ class JDBCCursor extends AbstractCursor { private ResultSet mResultSet; private boolean mHasNext; + /** + * @throws SQLException from executeQuery on statement. Caller must clean + * up when this happens by closing statement and connection. + */ JDBCCursor(JDBCStorage storage, Connection con, PreparedStatement statement) @@ -49,17 +53,7 @@ class JDBCCursor extends AbstractCursor { mStorage = storage; mConnection = con; mStatement = statement; - try { - mResultSet = statement.executeQuery(); - } catch (SQLException e) { - try { - statement.close(); - storage.mRepository.yieldConnection(con); - } catch (Exception e2) { - // Don't care. - } - throw e; - } + mResultSet = statement.executeQuery(); } public void close() throws FetchException { diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java index d6ceb62..6034227 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java @@ -460,14 +460,24 @@ class JDBCStorage extends StandardQueryFactory Connection con = mRepository.getConnection(); try { PreparedStatement ps = con.prepareStatement(prepareSelect(values, forUpdate)); - setParameters(ps, values); - return new JDBCCursor(JDBCStorage.this, con, ps); + try { + setParameters(ps, values); + return new JDBCCursor(JDBCStorage.this, con, ps); + } catch (Exception e) { + // in case of exception, close statement + try { + ps.close(); + } catch (SQLException e2) { + // ignore and allow triggering exception to propagate + } + throw e; + } } catch (Exception e) { - //in case of exception, yield connection + // in case of exception, yield connection try { mRepository.yieldConnection(con); } catch (FetchException e2) { - //ignore and allow triggering exception to propagate + // ignore and allow triggering exception to propagate } throw mRepository.toFetchException(e); } @@ -478,13 +488,17 @@ class JDBCStorage extends StandardQueryFactory Connection con = mRepository.getConnection(); try { PreparedStatement ps = con.prepareStatement(prepareCount(values)); - setParameters(ps, values); - ResultSet rs = ps.executeQuery(); try { - rs.next(); - return rs.getLong(1); + setParameters(ps, values); + ResultSet rs = ps.executeQuery(); + try { + rs.next(); + return rs.getLong(1); + } finally { + rs.close(); + } } finally { - rs.close(); + ps.close(); } } catch (Exception e) { throw mRepository.toFetchException(e); @@ -536,8 +550,12 @@ class JDBCStorage extends StandardQueryFactory } try { PreparedStatement ps = con.prepareStatement(prepareDelete(filterValues)); - setParameters(ps, filterValues); - return ps.executeUpdate(); + try { + setParameters(ps, filterValues); + return ps.executeUpdate(); + } finally { + ps.close(); + } } catch (Exception e) { throw mRepository.toPersistException(e); } finally { -- cgit v1.2.3