You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/07/31 19:36:16 UTC
[commons-dbutils] 02/08: Method that allocates a resources should release it.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbutils.git
commit 3f9a36ee48825257793f7d913f66ce49ad2d557b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 31 14:49:03 2023 -0400
Method that allocates a resources should release it.
Use try-with-resources
---
.../org/apache/commons/dbutils/QueryRunner.java | 109 ++++++++-------------
1 file changed, 42 insertions(+), 67 deletions(-)
diff --git a/src/main/java/org/apache/commons/dbutils/QueryRunner.java b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
index 39676eb..8e03c50 100644
--- a/src/main/java/org/apache/commons/dbutils/QueryRunner.java
+++ b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
@@ -185,69 +185,6 @@ public class QueryRunner extends AbstractQueryRunner {
}
}
- /**
- * Invokes the stored procedure via update after checking the parameters to
- * ensure nothing is null.
- * @param conn The connection to use for the update call.
- * @param closeConn True if the connection should be closed, false otherwise.
- * @param sql The SQL statement to execute.
- * @param rsh The result set handler
- * @param params An array of update replacement parameters. Each row in
- * this array is one set of update replacement values.
- * @return List of all objects generated by the ResultSetHandler for all result sets handled.
- * @throws SQLException If there are database or parameter errors.
- */
- private <T> List<T> execute(final Connection conn, final boolean closeConn, final String sql, final ResultSetHandler<T> rsh, final Object... params) throws SQLException {
- if (conn == null) {
- throw new SQLException("Null connection");
- }
-
- if (sql == null) {
- if (closeConn) {
- close(conn);
- }
- throw new SQLException("Null SQL statement");
- }
-
- if (rsh == null) {
- if (closeConn) {
- close(conn);
- }
- throw new SQLException("Null ResultSetHandler");
- }
-
- CallableStatement stmt = null;
- final List<T> results = new LinkedList<>();
-
- try {
- stmt = this.prepareCall(conn, sql);
- this.fillStatement(stmt, params);
- boolean moreResultSets = stmt.execute();
- // Handle multiple result sets by passing them through the handler
- // retaining the final result
- while (moreResultSets) {
- try (@SuppressWarnings("resource")
- // assume the ResultSet wrapper properly closes
- ResultSet rs = this.wrap(stmt.getResultSet())) {
- results.add(rsh.handle(rs));
- moreResultSets = stmt.getMoreResults();
- }
- }
- this.retrieveOutParameters(stmt, params);
-
- } catch (final SQLException e) {
- this.rethrow(e, sql, params);
-
- } finally {
- close(stmt);
- if (closeConn) {
- close(conn);
- }
- }
-
- return results;
- }
-
/**
* Execute an SQL statement, including a stored procedure call, which does
* not return any result sets.
@@ -319,7 +256,45 @@ public class QueryRunner extends AbstractQueryRunner {
* @throws SQLException if a database access error occurs
*/
public <T> List<T> execute(final Connection conn, final String sql, final ResultSetHandler<T> rsh, final Object... params) throws SQLException {
- return this.execute(conn, false, sql, rsh, params);
+ if (conn == null) {
+ throw new SQLException("Null connection");
+ }
+
+ if (sql == null) {
+ throw new SQLException("Null SQL statement");
+ }
+
+ if (rsh == null) {
+ throw new SQLException("Null ResultSetHandler");
+ }
+
+ CallableStatement stmt = null;
+ final List<T> results = new LinkedList<>();
+
+ try {
+ stmt = this.prepareCall(conn, sql);
+ this.fillStatement(stmt, params);
+ boolean moreResultSets = stmt.execute();
+ // Handle multiple result sets by passing them through the handler
+ // retaining the final result
+ while (moreResultSets) {
+ try (@SuppressWarnings("resource")
+ // assume the ResultSet wrapper properly closes
+ ResultSet rs = this.wrap(stmt.getResultSet())) {
+ results.add(rsh.handle(rs));
+ moreResultSets = stmt.getMoreResults();
+ }
+ }
+ this.retrieveOutParameters(stmt, params);
+
+ } catch (final SQLException e) {
+ this.rethrow(e, sql, params);
+
+ } finally {
+ close(stmt);
+ }
+
+ return results;
}
/**
@@ -372,9 +347,9 @@ public class QueryRunner extends AbstractQueryRunner {
* @throws SQLException if a database access error occurs
*/
public <T> List<T> execute(final String sql, final ResultSetHandler<T> rsh, final Object... params) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.execute(conn, true, sql, rsh, params);
+ try (Connection conn = this.prepareConnection()) {
+ return this.execute(conn, sql, rsh, params);
+ }
}
/**