Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Grgz #5

Merged
merged 3 commits into from

2 participants

@grgz

Some fixes to make sure PreparedStatements and ResultSets (your issue #4) get closed properly.

grgz added some commits
@grgz grgz fixed missing closedPreparedStatement() calls
fixed missing resultSet.close() calls
803d9f2
@grgz grgz set lastPreparedStatement=null in closeLastPreparedStatement() instea…
…d of closePreparedStatement()

(closePreparedStatement() might be called on a different statement)
added null check to closePreparedStatement()
0a88114
@grgz grgz more PreparedStatement leak fixes - from jwshin's fork 9ce5e38
@rufiao rufiao merged commit 173ed36 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 30, 2011
  1. @grgz

    fixed missing closedPreparedStatement() calls

    grgz authored
    fixed missing resultSet.close() calls
  2. @grgz

    set lastPreparedStatement=null in closeLastPreparedStatement() instea…

    grgz authored
    …d of closePreparedStatement()
    
    (closePreparedStatement() might be called on a different statement)
    added null check to closePreparedStatement()
  3. @grgz
This page is out of date. Refresh to see the latest.
View
125 src/main/net/sf/persist/Persist.java
@@ -366,7 +366,9 @@ public PreparedStatement getPreparedStatement(final String sql) {
*/
public void closePreparedStatement(final PreparedStatement statement) {
try {
- statement.close();
+ if (statement != null) {
+ statement.close();
+ }
} catch (SQLException e) {
throw new RuntimeSQLException("Error closing prepared statement: " + e.getMessage(), e);
}
@@ -374,8 +376,6 @@ public void closePreparedStatement(final PreparedStatement statement) {
if (Log.isDebugEnabled(Log.ENGINE)) {
Log.debug(Log.ENGINE, "Closed PreparedStatement [" + statement + "]");
}
-
- lastPreparedStatement = null;
}
/**
@@ -395,6 +395,7 @@ public PreparedStatement getLastPreparedStatement() {
*/
public void closeLastPreparedStatement() {
closePreparedStatement(lastPreparedStatement);
+ lastPreparedStatement = null;
}
/**
@@ -1107,49 +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) {
- 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) {
- 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);
+ }
+
+ 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");
+ }
}
- }
- Result result = new Result(rowsModified, generatedKeys);
+ 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 + "]");
- }
+ if (Log.isDebugEnabled(Log.PROFILING)) {
+ final long end = System.currentTimeMillis();
+ Log.debug(Log.PROFILING, "executeUpdate in [" + (end - begin) + "ms] for sql [" + sql + "]");
+ }
- return result;
+ return result;
+ } finally {
+ closePreparedStatement(stmt);
+ }
}
/**
@@ -1173,9 +1178,10 @@ public int executeUpdate(final String sql, final Object...parameters) {
} catch (SQLException e) {
throw new RuntimeSQLException("Error executing sql [" + sql + "] with parameters "
+ Arrays.toString(parameters) + ": " + e.getMessage(), e);
+ } finally {
+ closePreparedStatement(stmt);
}
- closePreparedStatement(stmt);
return rowsModified;
}
@@ -1361,16 +1367,17 @@ 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);
- if (closePreparedStatementsAfterRead) {
- closePreparedStatement(statement);
- }
return ret;
} catch (SQLException e) {
throw new RuntimeSQLException(e);
+ } finally {
+ if (closePreparedStatementsAfterRead) {
+ closePreparedStatement(statement);
+ }
}
}
@@ -1495,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;
}
/**
@@ -1660,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;
}
/**
@@ -1804,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;
}
/**
@@ -1911,4 +1924,4 @@ public Iterator readMapIterator(final String sql) {
return readMapIterator(sql, (Object[]) null);
}
-}
+}
View
2  src/main/net/sf/persist/TableMapping.java
@@ -84,6 +84,7 @@ public TableMapping(final DatabaseMetaData metaData, final Class objectClass, fi
final String columnName = resultSet.getString(4);
columnsList.add(columnName);
}
+ resultSet.close();
columns = toArray(columnsList);
// all primary keys (from db)
@@ -94,6 +95,7 @@ public TableMapping(final DatabaseMetaData metaData, final Class objectClass, fi
final String columnName = resultSet.getString(4);
primaryKeysList.add(columnName);
}
+ resultSet.close();
primaryKeys = toArray(primaryKeysList);
// not primary keys
Something went wrong with that request. Please try again.