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 2022/07/10 19:22:01 UTC

[commons-dbcp] branch master updated: Refactor duplicate pattern

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-dbcp.git


The following commit(s) were added to refs/heads/master by this push:
     new 79597041 Refactor duplicate pattern
79597041 is described below

commit 795970414dd5ed4754edb59c0471d2eaeeca6afa
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 10 15:21:55 2022 -0400

    Refactor duplicate pattern
---
 .../org/apache/commons/dbcp2/AbandonedTrace.java   | 15 +++++--------
 .../apache/commons/dbcp2/DelegatingStatement.java  | 16 +++++--------
 src/main/java/org/apache/commons/dbcp2/Utils.java  | 26 +++++++++++++++++-----
 .../dbcp2/cpdsadapter/PooledConnectionImpl.java    |  8 ++-----
 .../dbcp2/datasources/InstanceKeyDataSource.java   |  7 ++----
 .../datasources/InstanceKeyDataSourceFactory.java  |  8 +------
 6 files changed, 37 insertions(+), 43 deletions(-)

diff --git a/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java b/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java
index 2c7629f7..62b88457 100644
--- a/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java
+++ b/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java
@@ -35,7 +35,7 @@ import org.apache.commons.pool2.TrackedUse;
  *
  * @since 2.0
  */
-public class AbandonedTrace implements TrackedUse {
+public class AbandonedTrace implements TrackedUse, AutoCloseable {
 
     /** A list of objects created by children of this object. */
     private final List<WeakReference<AbandonedTrace>> traceList = new ArrayList<>();
@@ -88,22 +88,19 @@ public class AbandonedTrace implements TrackedUse {
      * @throws SQLException Ignored here, for subclasses.
      * @since 2.10.0
      */
-    protected void close() throws SQLException {
+    @Override
+    public void close() throws SQLException {
         // nop
     }
 
     /**
-     * Calls {@link #close()} and if an exception is caught, then {@code exceptionHandler}.
-     * 
+     * Closes this resource and if an exception is caught, then calls {@code exceptionHandler}.
+     *
      * @param exceptionHandler Consumes exception thrown closing this resource.
      * @since 2.10.0
      */
     protected void close(final Consumer<Exception> exceptionHandler) {
-        try {
-            close();
-        } catch (final Exception e) {
-            exceptionHandler.accept(e);
-        }   
+        Utils.close(this, exceptionHandler);
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java
index 7b751ec2..ba9ccc08 100644
--- a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java
+++ b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java
@@ -148,17 +148,13 @@ public class DelegatingStatement extends AbandonedTrace implements Statement {
                 }));
                 clearTrace();
             }
-            if (statement != null) {
-                try {
-                    statement.close();
-                } catch (final Exception e) {
-                    if (connection != null) {
-                        // Does not rethrow e.
-                        connection.handleExceptionNoThrow(e);
-                    }
-                    thrownList.add(e);
+            Utils.close(statement, e -> {
+                if (connection != null) {
+                    // Does not rethrow e.
+                    connection.handleExceptionNoThrow(e);
                 }
-            }
+                thrownList.add(e);
+            });
         } finally {
             closed = true;
             statement = null;
diff --git a/src/main/java/org/apache/commons/dbcp2/Utils.java b/src/main/java/org/apache/commons/dbcp2/Utils.java
index afe64f51..0bf8ed06 100644
--- a/src/main/java/org/apache/commons/dbcp2/Utils.java
+++ b/src/main/java/org/apache/commons/dbcp2/Utils.java
@@ -27,6 +27,7 @@ import java.util.HashSet;
 import java.util.Properties;
 import java.util.ResourceBundle;
 import java.util.Set;
+import java.util.function.Consumer;
 
 import org.apache.commons.pool2.PooledObject;
 
@@ -107,21 +108,34 @@ public final class Utils {
     }
 
     /**
-     * Closes the AutoCloseable (which may be null).
+     * Closes the given {@link AutoCloseable} and if an exception is caught, then calls {@code exceptionHandler}.
      *
-     * @param autoCloseable an AutoCloseable, may be {@code null}
-     * @since 2.6.0
+     * @param autoCloseable The resource to close.
+     * @param exceptionHandler Consumes exception thrown closing this resource.
+     * @since 2.10.0
      */
-    public static void closeQuietly(final AutoCloseable autoCloseable) {
+    public static void close(AutoCloseable autoCloseable, final Consumer<Exception> exceptionHandler) {
         if (autoCloseable != null) {
             try {
                 autoCloseable.close();
-            } catch (final Exception ignored) {
-                // ignored
+            } catch (final Exception e) {
+                if (exceptionHandler != null) {
+                    exceptionHandler.accept(e);
+                }
             }
         }
     }
 
+    /**
+     * Closes the AutoCloseable (which may be null).
+     *
+     * @param autoCloseable an AutoCloseable, may be {@code null}
+     * @since 2.6.0
+     */
+    public static void closeQuietly(final AutoCloseable autoCloseable) {
+        close(autoCloseable, null);
+    }
+
     /**
      * Closes the Connection (which may be null).
      *
diff --git a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java
index 1e412bc9..8e271c5c 100644
--- a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java
+++ b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java
@@ -35,6 +35,7 @@ import org.apache.commons.dbcp2.Jdbc41Bridge;
 import org.apache.commons.dbcp2.PStmtKey;
 import org.apache.commons.dbcp2.PoolableCallableStatement;
 import org.apache.commons.dbcp2.PoolablePreparedStatement;
+import org.apache.commons.dbcp2.Utils;
 import org.apache.commons.dbcp2.PoolingConnection.StatementType;
 import org.apache.commons.pool2.KeyedObjectPool;
 import org.apache.commons.pool2.KeyedPooledObjectFactory;
@@ -343,12 +344,7 @@ final class PooledConnectionImpl
     protected void finalize() throws Throwable {
         // Closing the Connection ensures that if anyone tries to use it,
         // an error will occur.
-        try {
-            connection.close();
-        } catch (final Exception ignored) {
-            // ignore
-        }
-
+        Utils.close(connection, null);
         // make sure the last connection is marked as closed
         if (logicalConnection != null && !logicalConnection.isClosed()) {
             throw new SQLException("PooledConnection was gc'ed, without its last Connection being closed.");
diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java
index 87b4b673..3361832c 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java
@@ -34,6 +34,7 @@ import javax.sql.ConnectionPoolDataSource;
 import javax.sql.DataSource;
 import javax.sql.PooledConnection;
 
+import org.apache.commons.dbcp2.Utils;
 import org.apache.commons.pool2.impl.BaseObjectPoolConfig;
 import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
 import org.apache.commons.pool2.impl.GenericObjectPool;
@@ -263,11 +264,7 @@ public abstract class InstanceKeyDataSource implements DataSource, Referenceable
             connection.clearWarnings();
             return connection;
         } catch (final SQLException ex) {
-            try {
-                connection.close();
-            } catch (final Exception exc) {
-                getLogWriter().println("ignoring exception during close: " + exc);
-            }
+            Utils.close(connection, e -> getLogWriter().println("ignoring exception during close: " + e));
             throw ex;
         }
     }
diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
index adf29c33..afd5c8f2 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
@@ -62,13 +62,7 @@ abstract class InstanceKeyDataSourceFactory implements ObjectFactory {
             if (entry != null) {
                 @SuppressWarnings("resource")
                 final InstanceKeyDataSource value = entry.getValue();
-                if (value != null) {
-                    try {
-                        value.close();
-                    } catch (final Exception e) {
-                        exceptionList.add(e);
-                    }
-                }
+                Utils.close(value, exceptionList::add);
             }
         });
         INSTANCE_MAP.clear();