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:17 UTC
[commons-dbutils] 03/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 7ca9187c1fa5f1f52006f46ee0e800eb2b84a2d0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 31 14:53:53 2023 -0400
Method that allocates a resources should release it.
Use try-with-resources
---
.../org/apache/commons/dbutils/QueryRunner.java | 76 ++++++++--------------
1 file changed, 27 insertions(+), 49 deletions(-)
diff --git a/src/main/java/org/apache/commons/dbutils/QueryRunner.java b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
index 8e03c50..ca1673e 100644
--- a/src/main/java/org/apache/commons/dbutils/QueryRunner.java
+++ b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
@@ -353,34 +353,42 @@ public class QueryRunner extends AbstractQueryRunner {
}
/**
- * Executes the given INSERT SQL statement.
- * @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.
+ * Execute an SQL INSERT query without replacement parameters.
+ * @param <T> The type of object that the handler returns
+ * @param conn The connection to use to run the query.
+ * @param sql The SQL to execute.
+ * @param rsh The handler used to create the result object from
+ * the {@code ResultSet} of auto-generated keys.
+ * @return An object generated by the handler.
+ * @throws SQLException if a database access error occurs
+ * @since 1.6
+ */
+ public <T> T insert(final Connection conn, final String sql, final ResultSetHandler<T> rsh) throws SQLException {
+ return insert(conn, sql, rsh, (Object[]) null);
+ }
+
+ /**
+ * Execute an SQL INSERT query.
+ * @param <T> The type of object that the handler returns
+ * @param conn The connection to use to run the query.
+ * @param sql The SQL to execute.
* @param rsh The handler used to create the result object from
* the {@code ResultSet} of auto-generated keys.
* @param params The query replacement parameters.
* @return An object generated by the handler.
- * @throws SQLException If there are database or parameter errors.
+ * @throws SQLException if a database access error occurs
* @since 1.6
*/
- private <T> T insert(final Connection conn, final boolean closeConn, final String sql, final ResultSetHandler<T> rsh, final Object... params)
- throws SQLException {
+ public <T> T insert(final Connection conn, 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");
}
@@ -403,45 +411,11 @@ public class QueryRunner extends AbstractQueryRunner {
this.rethrow(e, sql, params);
} finally {
close(stmt);
- if (closeConn) {
- close(conn);
- }
}
return generatedKeys;
}
- /**
- * Execute an SQL INSERT query without replacement parameters.
- * @param <T> The type of object that the handler returns
- * @param conn The connection to use to run the query.
- * @param sql The SQL to execute.
- * @param rsh The handler used to create the result object from
- * the {@code ResultSet} of auto-generated keys.
- * @return An object generated by the handler.
- * @throws SQLException if a database access error occurs
- * @since 1.6
- */
- public <T> T insert(final Connection conn, final String sql, final ResultSetHandler<T> rsh) throws SQLException {
- return insert(conn, false, sql, rsh, (Object[]) null);
- }
-
- /**
- * Execute an SQL INSERT query.
- * @param <T> The type of object that the handler returns
- * @param conn The connection to use to run the query.
- * @param sql The SQL to execute.
- * @param rsh The handler used to create the result object from
- * the {@code ResultSet} of auto-generated keys.
- * @param params The query replacement parameters.
- * @return An object generated by the handler.
- * @throws SQLException if a database access error occurs
- * @since 1.6
- */
- public <T> T insert(final Connection conn, final String sql, final ResultSetHandler<T> rsh, final Object... params) throws SQLException {
- return insert(conn, false, sql, rsh, params);
- }
-
/**
* Executes the given INSERT SQL without any replacement parameters.
* The {@code Connection} is retrieved from the
@@ -455,7 +429,9 @@ public class QueryRunner extends AbstractQueryRunner {
* @since 1.6
*/
public <T> T insert(final String sql, final ResultSetHandler<T> rsh) throws SQLException {
- return insert(this.prepareConnection(), true, sql, rsh, (Object[]) null);
+ try (Connection conn = this.prepareConnection()) {
+ return insert(conn, sql, rsh, (Object[]) null);
+ }
}
/**
@@ -473,7 +449,9 @@ public class QueryRunner extends AbstractQueryRunner {
* @since 1.6
*/
public <T> T insert(final String sql, final ResultSetHandler<T> rsh, final Object... params) throws SQLException {
- return insert(this.prepareConnection(), true, sql, rsh, params);
+ try (Connection conn = this.prepareConnection()) {
+ return insert(conn, sql, rsh, params);
+ }
}
/**