[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jakarta-commons-dev
Subject:    [commons-dbutils] 04/08: Method that allocates a resources should release it.
From:       ggregory () apache ! org
Date:       2023-07-31 19:36:18
Message-ID: 20230731193614.3076A440219 () gitbox2-he-fi ! apache ! org
[Download RAW message or body]

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 e0352600617a0787796aaba4f56c5cf79a2976ec
Author: Gary Gregory <garydgregory@gmail.com>
AuthorDate: Mon Jul 31 14:56:21 2023 -0400

    Method that allocates a resources should release it.
    
    Use try-with-resources
---
 .../org/apache/commons/dbutils/QueryRunner.java    | 40 +++++-----------------
 1 file changed, 8 insertions(+), 32 deletions(-)

diff --git a/src/main/java/org/apache/commons/dbutils/QueryRunner.java \
b/src/main/java/org/apache/commons/dbutils/QueryRunner.java index ca1673e..d08dbde \
                100644
--- a/src/main/java/org/apache/commons/dbutils/QueryRunner.java
+++ b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
@@ -456,33 +456,26 @@ public class QueryRunner extends AbstractQueryRunner {
 
     /**
      * Executes the given batch of INSERT SQL statements.
-     * @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 <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 The result 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 insertBatch(final Connection conn, final boolean closeConn, final \
                String sql, final ResultSetHandler<T> rsh, final Object[][] params)
-            throws SQLException {
+    public <T> T insertBatch(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 (params == null) {
-            if (closeConn) {
-                close(conn);
-            }
             throw new SQLException("Null parameters. If parameters aren't need, pass \
an empty array.");  }
 
@@ -503,30 +496,11 @@ public class QueryRunner extends AbstractQueryRunner {
             this.rethrow(e, sql, (Object[])params);
         } finally {
             close(stmt);
-            if (closeConn) {
-                close(conn);
-            }
         }
 
         return generatedKeys;
     }
 
-    /**
-     * Executes the given batch of INSERT SQL statements.
-     * @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 The result generated by the handler.
-     * @throws SQLException if a database access error occurs
-     * @since 1.6
-     */
-    public <T> T insertBatch(final Connection conn, final String sql, final \
                ResultSetHandler<T> rsh, final Object[][] params) throws SQLException \
                {
-        return insertBatch(conn, false, sql, rsh, params);
-    }
-
     /**
      * Executes the given batch of INSERT SQL statements. The
      * {@code Connection} is retrieved from the {@code DataSource}
@@ -542,7 +516,9 @@ public class QueryRunner extends AbstractQueryRunner {
      * @since 1.6
      */
     public <T> T insertBatch(final String sql, final ResultSetHandler<T> rsh, final \
                Object[][] params) throws SQLException {
-        return insertBatch(this.prepareConnection(), true, sql, rsh, params);
+        try (Connection conn = this.prepareConnection()) {
+            return insertBatch(conn, sql, rsh, params);
+        }
     }
 
     /**


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic