Permalink
Browse files

more PreparedStatement leak fixes - from jwshin's fork

  • Loading branch information...
1 parent 0a88114 commit 9ce5e38d4d5ea8151cb1edefb24d8b09cc93e4e6 @grgz grgz committed Apr 30, 2011
Showing with 58 additions and 51 deletions.
  1. +58 −51 src/main/net/sf/persist/Persist.java
View
109 src/main/net/sf/persist/Persist.java
@@ -1108,52 +1108,53 @@ public Result executeUpdate(final Class objectClass, final String sql, final Str
final PreparedStatement stmt = getPreparedStatement(sql, autoGeneratedKeys);
- setParameters(stmt, parameters);
-
- int rowsModified = 0;
try {
- rowsModified = stmt.executeUpdate();
- } catch (SQLException e) {
- closePreparedStatement(stmt);
- throw new RuntimeSQLException("Error executing sql [" + sql + "] with parameters "
- + Arrays.toString(parameters) + ": " + e.getMessage(), e);
- }
+ setParameters(stmt, parameters);
- final List generatedKeys = new ArrayList();
- if (autoGeneratedKeys.length != 0) {
+ int rowsModified = 0;
try {
- final Mapping mapping = getMapping(objectClass);
- final ResultSet resultSet = stmt.getGeneratedKeys();
- for (int i = 0; i < autoGeneratedKeys.length; i++) {
- resultSet.next();
-
- // get the auto-generated key using the ResultSet.get method
- // that matches
- // the bean setter parameter type
- final Method setter = mapping.getSetterForColumn(autoGeneratedKeys[i]);
- final Class type = setter.getParameterTypes()[0];
- final Object value = Persist.getValueFromResultSet(resultSet, 1, type);
-
- generatedKeys.add(value);
- }
- resultSet.close();
+ rowsModified = stmt.executeUpdate();
} catch (SQLException e) {
- closePreparedStatement(stmt);
- throw new RuntimeSQLException("This JDBC driver does not support PreparedStatement.getGeneratedKeys()."
- + " Please use setUpdateAutoGeneratedKeys(false) in your Persist instance"
- + " to disable attempts to use that feature");
+ throw new RuntimeSQLException("Error executing sql [" + sql + "] with parameters "
+ + Arrays.toString(parameters) + ": " + e.getMessage(), e);
}
- }
- Result result = new Result(rowsModified, generatedKeys);
+ final List generatedKeys = new ArrayList();
+ if (autoGeneratedKeys.length != 0) {
+ try {
+ final Mapping mapping = getMapping(objectClass);
+ final ResultSet resultSet = stmt.getGeneratedKeys();
+ for (int i = 0; i < autoGeneratedKeys.length; i++) {
+ resultSet.next();
+
+ // get the auto-generated key using the ResultSet.get method
+ // that matches
+ // the bean setter parameter type
+ final Method setter = mapping.getSetterForColumn(autoGeneratedKeys[i]);
+ final Class type = setter.getParameterTypes()[0];
+ final Object value = Persist.getValueFromResultSet(resultSet, 1, type);
+
+ generatedKeys.add(value);
+ }
+ resultSet.close();
+ } catch (SQLException e) {
+ throw new RuntimeSQLException("This JDBC driver does not support PreparedStatement.getGeneratedKeys()."
+ + " Please use setUpdateAutoGeneratedKeys(false) in your Persist instance"
+ + " to disable attempts to use that feature");
+ }
+ }
- if (Log.isDebugEnabled(Log.PROFILING)) {
- final long end = System.currentTimeMillis();
- Log.debug(Log.PROFILING, "executeUpdate in [" + (end - begin) + "ms] for sql [" + sql + "]");
- }
+ Result result = new Result(rowsModified, generatedKeys);
+
+ if (Log.isDebugEnabled(Log.PROFILING)) {
+ final long end = System.currentTimeMillis();
+ Log.debug(Log.PROFILING, "executeUpdate in [" + (end - begin) + "ms] for sql [" + sql + "]");
+ }
- closePreparedStatement(stmt);
- return result;
+ return result;
+ } finally {
+ closePreparedStatement(stmt);
+ }
}
/**
@@ -1366,8 +1367,8 @@ public int delete(final Object object) {
* @since 1.0
*/
public <T> T read(final Class<T> objectClass, final PreparedStatement statement, final Object...parameters) {
- setParameters(statement, parameters);
try {
+ setParameters(statement, parameters);
final ResultSet resultSet = statement.executeQuery();
final T ret = read(objectClass, resultSet);
return ret;
@@ -1501,11 +1502,13 @@ public int delete(final Object object) {
*/
public <T> List<T> readList(final Class<T> objectClass, final String sql, final Object...parameters) {
final PreparedStatement stmt = getPreparedStatement(sql);
- final List ret = readList(objectClass, stmt, parameters);
- if (closePreparedStatementsAfterRead) {
- closePreparedStatement(stmt);
+ try {
+ return readList(objectClass, stmt, parameters);
+ } finally {
+ if (closePreparedStatementsAfterRead) {
+ closePreparedStatement(stmt);
+ }
}
- return ret;
}
/**
@@ -1666,11 +1669,13 @@ public int delete(final Object object) {
*/
public Map<String, Object> readMap(final String sql, final Object...parameters) {
final PreparedStatement stmt = getPreparedStatement(sql);
- final Map<String, Object> ret = readMap(stmt, parameters);
- if (closePreparedStatementsAfterRead) {
- closePreparedStatement(stmt);
+ try {
+ return readMap(stmt, parameters);
+ } finally {
+ if (closePreparedStatementsAfterRead) {
+ closePreparedStatement(stmt);
+ }
}
- return ret;
}
/**
@@ -1810,11 +1815,13 @@ public int delete(final Object object) {
*/
public List<Map<String, Object>> readMapList(final String sql, final Object...parameters) {
final PreparedStatement stmt = getPreparedStatement(sql);
- final List<Map<String, Object>> ret = readMapList(stmt, parameters);
- if (closePreparedStatementsAfterRead) {
- closePreparedStatement(stmt);
+ try {
+ return readMapList(stmt, parameters);
+ } finally {
+ if (closePreparedStatementsAfterRead) {
+ closePreparedStatement(stmt);
+ }
}
- return ret;
}
/**

0 comments on commit 9ce5e38

Please sign in to comment.