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:19 UTC
[commons-dbutils] 05/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 6b966ea77d40e0a1c721f063a26e3f78c699354d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 31 15:00:20 2023 -0400
Method that allocates a resources should release it.
Use try-with-resources
---
.../org/apache/commons/dbutils/QueryRunner.java | 128 +++++++++------------
1 file changed, 52 insertions(+), 76 deletions(-)
diff --git a/src/main/java/org/apache/commons/dbutils/QueryRunner.java b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
index d08dbde..f8854b7 100644
--- a/src/main/java/org/apache/commons/dbutils/QueryRunner.java
+++ b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
@@ -521,66 +521,6 @@ public class QueryRunner extends AbstractQueryRunner {
}
}
- /**
- * Calls query after checking the parameters to ensure nothing is null.
- * @param conn The connection to use for the query call.
- * @param closeConn True if the connection should be closed, false otherwise.
- * @param sql The SQL statement to execute.
- * @param params An array of query replacement parameters. Each row in
- * this array is one set of batch replacement values.
- * @return The results of the query.
- * @throws SQLException If there are database or parameter errors.
- */
- private <T> T query(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");
- }
-
- Statement stmt = null;
- ResultSet rs = null;
- T result = null;
-
- try {
- if (params != null && params.length > 0) {
- final PreparedStatement ps = this.prepareStatement(conn, sql);
- stmt = ps;
- this.fillStatement(ps, params);
- rs = this.wrap(ps.executeQuery());
- } else {
- stmt = conn.createStatement();
- rs = this.wrap(stmt.executeQuery(sql));
- }
- result = rsh.handle(rs);
-
- } catch (final SQLException e) {
- this.rethrow(e, sql, params);
-
- } finally {
- closeQuietly(rs);
- closeQuietly(stmt);
- if (closeConn) {
- close(conn);
- }
- }
-
- return result;
- }
-
/**
* Execute an SQL SELECT query with a single replacement parameter. The
* caller is responsible for closing the connection.
@@ -595,7 +535,7 @@ public class QueryRunner extends AbstractQueryRunner {
*/
@Deprecated
public <T> T query(final Connection conn, final String sql, final Object param, final ResultSetHandler<T> rsh) throws SQLException {
- return this.<T>query(conn, false, sql, rsh, param);
+ return this.<T>query(conn, sql, rsh, param);
}
/**
@@ -612,7 +552,7 @@ public class QueryRunner extends AbstractQueryRunner {
*/
@Deprecated
public <T> T query(final Connection conn, final String sql, final Object[] params, final ResultSetHandler<T> rsh) throws SQLException {
- return this.<T>query(conn, false, sql, rsh, params);
+ return this.<T>query(conn, sql, rsh, params);
}
/**
@@ -626,7 +566,7 @@ public class QueryRunner extends AbstractQueryRunner {
* @throws SQLException if a database access error occurs
*/
public <T> T query(final Connection conn, final String sql, final ResultSetHandler<T> rsh) throws SQLException {
- return this.<T>query(conn, false, sql, rsh, (Object[]) null);
+ return this.<T>query(conn, sql, rsh, (Object[]) null);
}
/**
@@ -641,7 +581,43 @@ public class QueryRunner extends AbstractQueryRunner {
* @throws SQLException if a database access error occurs
*/
public <T> T query(final Connection conn, final String sql, final ResultSetHandler<T> rsh, final Object... params) throws SQLException {
- return this.<T>query(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");
+ }
+
+ Statement stmt = null;
+ ResultSet rs = null;
+ T result = null;
+
+ try {
+ if (params != null && params.length > 0) {
+ final PreparedStatement ps = this.prepareStatement(conn, sql);
+ stmt = ps;
+ this.fillStatement(ps, params);
+ rs = this.wrap(ps.executeQuery());
+ } else {
+ stmt = conn.createStatement();
+ rs = this.wrap(stmt.executeQuery(sql));
+ }
+ result = rsh.handle(rs);
+
+ } catch (final SQLException e) {
+ this.rethrow(e, sql, params);
+
+ } finally {
+ closeQuietly(rs);
+ closeQuietly(stmt);
+ }
+
+ return result;
}
/**
@@ -660,9 +636,9 @@ public class QueryRunner extends AbstractQueryRunner {
*/
@Deprecated
public <T> T query(final String sql, final Object param, final ResultSetHandler<T> rsh) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.<T>query(conn, true, sql, rsh, param);
+ try (Connection conn = this.prepareConnection()) {
+ return this.<T>query(conn, sql, rsh, param);
+ }
}
/**
@@ -683,9 +659,9 @@ public class QueryRunner extends AbstractQueryRunner {
*/
@Deprecated
public <T> T query(final String sql, final Object[] params, final ResultSetHandler<T> rsh) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.<T>query(conn, true, sql, rsh, params);
+ try (Connection conn = this.prepareConnection()) {
+ return this.<T>query(conn, sql, rsh, params);
+ }
}
/**
@@ -701,9 +677,9 @@ public class QueryRunner extends AbstractQueryRunner {
* @throws SQLException if a database access error occurs
*/
public <T> T query(final String sql, final ResultSetHandler<T> rsh) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.<T>query(conn, true, sql, rsh, (Object[]) null);
+ try (Connection conn = this.prepareConnection()) {
+ return this.<T>query(conn, sql, rsh, (Object[]) null);
+ }
}
/**
@@ -720,9 +696,9 @@ public class QueryRunner extends AbstractQueryRunner {
* @throws SQLException if a database access error occurs
*/
public <T> T query(final String sql, final ResultSetHandler<T> rsh, final Object... params) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.<T>query(conn, true, sql, rsh, params);
+ try (Connection conn = this.prepareConnection()) {
+ return this.<T>query(conn, sql, rsh, params);
+ }
}
/**