You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2025/12/15 10:51:41 UTC

(commons-dbcp) branch master updated: Fix potential resource leak on exception in initializeConnection()

This is an automated email from the ASF dual-hosted git repository.

markt 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 914130fb Fix potential resource leak on exception in initializeConnection()
914130fb is described below

commit 914130fbb13dd86a117096f6a725092390dcbd02
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Mon Dec 15 10:51:35 2025 +0000

    Fix potential resource leak on exception in initializeConnection()
---
 .../org/apache/commons/dbcp2/PoolableConnectionFactory.java    | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
index bdcdddc6..a2ab9772 100644
--- a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
@@ -425,6 +425,16 @@ public class PoolableConnectionFactory implements PooledObjectFactory<PoolableCo
                 for (final String sql : sqls) {
                     statement.execute(Objects.requireNonNull(sql, "null connectionInitSqls element"));
                 }
+            } catch (SQLException sqle) {
+                /*
+                 * Need to close the connection here as the reference to it will be lost once the SQLEXception is
+                 * thrown.
+                 *
+                 * Cast to AutoCloseable to avoid calling the deprecated method. The cast can be removed once the
+                 * deprecated method has been removed.s
+                 */
+                Utils.closeQuietly((AutoCloseable) conn);
+                throw sqle;
             }
         }
     }


Re: (commons-dbcp) branch master updated: Fix potential resource leak on exception in initializeConnection()

Posted by Gary Gregory <ga...@gmail.com>.
On Mon, Dec 15, 2025 at 5:52 AM <ma...@apache.org> wrote:
>
> This is an automated email from the ASF dual-hosted git repository.
>
> markt 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 914130fb Fix potential resource leak on exception in initializeConnection()
> 914130fb is described below
>
> commit 914130fbb13dd86a117096f6a725092390dcbd02
> Author: Mark Thomas <ma...@apache.org>
> AuthorDate: Mon Dec 15 10:51:35 2025 +0000
>
>     Fix potential resource leak on exception in initializeConnection()
> ---
>  .../org/apache/commons/dbcp2/PoolableConnectionFactory.java    | 10 ++++++++++
>  1 file changed, 10 insertions(+)
>
> diff --git a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
> index bdcdddc6..a2ab9772 100644
> --- a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
> +++ b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
> @@ -425,6 +425,16 @@ public class PoolableConnectionFactory implements PooledObjectFactory<PoolableCo
>                  for (final String sql : sqls) {
>                      statement.execute(Objects.requireNonNull(sql, "null connectionInitSqls element"));
>                  }
> +            } catch (SQLException sqle) {
> +                /*
> +                 * Need to close the connection here as the reference to it will be lost once the SQLEXception is
> +                 * thrown.
> +                 *
> +                 * Cast to AutoCloseable to avoid calling the deprecated method. The cast can be removed once the
> +                 * deprecated method has been removed.s
> +                 */
> +                Utils.closeQuietly((AutoCloseable) conn);
> +                throw sqle;

Hi Mark and all,

This change appears incorrect: it frees a resource that it didn't
allocate. Note that the other caller of initializeConnection() closes
the resource _it_ allocates when an SQL exception is thrown.
Subclasses in custom code may have additional work to do in this case
and would not be allowed to do so.

I propose https://github.com/apache/commons-dbcp/pull/540

Or, am I missing something?

Gary

>              }
>          }
>      }
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: (commons-dbcp) branch master updated: Fix potential resource leak on exception in initializeConnection()

Posted by Mark Thomas <ma...@apache.org>.
On 15/12/2025 10:51, markt@apache.org wrote:
> This is an automated email from the ASF dual-hosted git repository.
> 
> markt 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 914130fb Fix potential resource leak on exception in initializeConnection()
> 914130fb is described below
> 
> commit 914130fbb13dd86a117096f6a725092390dcbd02
> Author: Mark Thomas <ma...@apache.org>
> AuthorDate: Mon Dec 15 10:51:35 2025 +0000
> 
>      Fix potential resource leak on exception in initializeConnection()

With impeccable timing, the Coverity scan of Tomcat's DBCP fork reported 
this issue after 2.14.0 was tagged.

There is also a second issue reported that I haven't looked at yet.

I don't think there is any reason not to release 2.14.0 but I haven't 
added this fix to the change log yet in case there is another 2.14.0 RC.

I'm also in the process of updating Tomcat's DBCP fork to the 2.14.0-RC1 
tag. I found a couple of minor Javadoc issues that I need to check 
aren't fork specific before I apply any fixes to DBCP. I want to finish 
checking the Coverity issues before I look at those. Again, I haven't 
seen anything in the 2.14.0-RC1 that causes me any concern with a release.

Mark

> ---
>   .../org/apache/commons/dbcp2/PoolableConnectionFactory.java    | 10 ++++++++++
>   1 file changed, 10 insertions(+)
> 
> diff --git a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
> index bdcdddc6..a2ab9772 100644
> --- a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
> +++ b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
> @@ -425,6 +425,16 @@ public class PoolableConnectionFactory implements PooledObjectFactory<PoolableCo
>                   for (final String sql : sqls) {
>                       statement.execute(Objects.requireNonNull(sql, "null connectionInitSqls element"));
>                   }
> +            } catch (SQLException sqle) {
> +                /*
> +                 * Need to close the connection here as the reference to it will be lost once the SQLEXception is
> +                 * thrown.
> +                 *
> +                 * Cast to AutoCloseable to avoid calling the deprecated method. The cast can be removed once the
> +                 * deprecated method has been removed.s
> +                 */
> +                Utils.closeQuietly((AutoCloseable) conn);
> +                throw sqle;
>               }
>           }
>       }
>