summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/amazon/carbonado')
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCCursor.java16
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java40
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<S extends Storable> extends AbstractCursor<S> {
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<S> storage,
Connection con,
PreparedStatement statement)
@@ -49,17 +53,7 @@ class JDBCCursor<S extends Storable> extends AbstractCursor<S> {
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<S extends Storable> extends StandardQueryFactory<S>
Connection con = mRepository.getConnection();
try {
PreparedStatement ps = con.prepareStatement(prepareSelect(values, forUpdate));
- setParameters(ps, values);
- return new JDBCCursor<S>(JDBCStorage.this, con, ps);
+ try {
+ setParameters(ps, values);
+ return new JDBCCursor<S>(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<S extends Storable> extends StandardQueryFactory<S>
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<S extends Storable> extends StandardQueryFactory<S>
}
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 {