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/01/20 14:22:42 UTC

[commons-dbcp] 04/04: Throw SQLException instead of NullPointerException when the connection is already closed in PooledConnectionImpl

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

commit 7d5fcc9182c038e77e39511077d0ac3d005cc077
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jan 20 09:22:33 2023 -0500

    Throw SQLException instead of NullPointerException when the connection
    is already closed in PooledConnectionImpl
---
 src/changes/changes.xml                            |  3 ++
 .../dbcp2/cpdsadapter/PooledConnectionImpl.java    | 32 ++++++++++++++++------
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7942f2a7..fc8a880b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -113,6 +113,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix" due-to="Gary Gregory">
         Wrong property name logged in ConnectionFactoryFactory.createConnectionFactory(BasicDataSource, Driver).
       </action>
+      <action dev="ggregory" type="fix" due-to="Gary Gregory">
+        Throw SQLException instead of NullPointerException when the connection is already closed.
+      </action>
       <!-- ADD -->
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add and use AbandonedTrace#setLastUsed(Instant).
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 33b0c724..5de13c9a 100644
--- a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java
+++ b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java
@@ -380,6 +380,11 @@ final class PooledConnectionImpl
         return logicalConnection;
     }
 
+    private Connection getRawConnection() throws SQLException {
+        assertOpen();
+        return connection;
+    }
+
     private String getSchemaOrNull() {
         try {
             return connection == null ? null : Jdbc41Bridge.getSchema(connection);
@@ -460,9 +465,10 @@ final class PooledConnectionImpl
      *                Thrown if a database access error occurs or this method is called on a closed connection.
      * @since 2.4.0
      */
+    @SuppressWarnings("resource") // getRawConnection() does not allocate
     CallableStatement prepareCall(final String sql) throws SQLException {
         if (pStmtPool == null) {
-            return connection.prepareCall(sql);
+            return getRawConnection().prepareCall(sql);
         }
         try {
             return (CallableStatement) pStmtPool.borrowObject(createKey(sql, StatementType.CALLABLE_STATEMENT));
@@ -492,10 +498,11 @@ final class PooledConnectionImpl
      *             parameters are not {@code ResultSet} constants indicating type and concurrency.
      * @since 2.4.0
      */
+    @SuppressWarnings("resource") // getRawConnection() does not allocate
     CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency)
             throws SQLException {
         if (pStmtPool == null) {
-            return connection.prepareCall(sql, resultSetType, resultSetConcurrency);
+            return getRawConnection().prepareCall(sql, resultSetType, resultSetConcurrency);
         }
         try {
             return (CallableStatement) pStmtPool.borrowObject(
@@ -529,10 +536,11 @@ final class PooledConnectionImpl
      *             parameters are not {@code ResultSet} constants indicating type, concurrency, and holdability.
      * @since 2.4.0
      */
+    @SuppressWarnings("resource") // getRawConnection() does not allocate
     CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency,
             final int resultSetHoldability) throws SQLException {
         if (pStmtPool == null) {
-            return connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
+            return getRawConnection().prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
         }
         try {
             return (CallableStatement) pStmtPool.borrowObject(createKey(sql, resultSetType, resultSetConcurrency,
@@ -552,9 +560,10 @@ final class PooledConnectionImpl
      * @throws SQLException Thrown if a database access error occurs, this method is called on a closed connection, or
      *         the borrow failed.
      */
+    @SuppressWarnings("resource") // getRawConnection() does not allocate
     PreparedStatement prepareStatement(final String sql) throws SQLException {
         if (pStmtPool == null) {
-            return connection.prepareStatement(sql);
+            return getRawConnection().prepareStatement(sql);
         }
         try {
             return pStmtPool.borrowObject(createKey(sql));
@@ -578,9 +587,10 @@ final class PooledConnectionImpl
      *         the borrow failed.
      * @see Connection#prepareStatement(String, int)
      */
+    @SuppressWarnings("resource") // getRawConnection() does not allocate
     PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException {
         if (pStmtPool == null) {
-            return connection.prepareStatement(sql, autoGeneratedKeys);
+            return getRawConnection().prepareStatement(sql, autoGeneratedKeys);
         }
         try {
             return pStmtPool.borrowObject(createKey(sql, autoGeneratedKeys));
@@ -609,10 +619,11 @@ final class PooledConnectionImpl
      *         the borrow failed.
      * @see Connection#prepareStatement(String, int, int)
      */
+    @SuppressWarnings("resource") // getRawConnection() does not allocate
     PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency)
             throws SQLException {
         if (pStmtPool == null) {
-            return connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
+            return getRawConnection().prepareStatement(sql, resultSetType, resultSetConcurrency);
         }
         try {
             return pStmtPool.borrowObject(createKey(sql, resultSetType, resultSetConcurrency));
@@ -623,10 +634,11 @@ final class PooledConnectionImpl
         }
     }
 
+    @SuppressWarnings("resource") // getRawConnection() does not allocate
     PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency,
             final int resultSetHoldability) throws SQLException {
         if (pStmtPool == null) {
-            return connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
+            return getRawConnection().prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
         }
         try {
             return pStmtPool.borrowObject(createKey(sql, resultSetType, resultSetConcurrency, resultSetHoldability));
@@ -637,9 +649,10 @@ final class PooledConnectionImpl
         }
     }
 
+    @SuppressWarnings("resource") // getRawConnection() does not allocate
     PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws SQLException {
         if (pStmtPool == null) {
-            return connection.prepareStatement(sql, columnIndexes);
+            return getRawConnection().prepareStatement(sql, columnIndexes);
         }
         try {
             return pStmtPool.borrowObject(createKey(sql, columnIndexes));
@@ -650,9 +663,10 @@ final class PooledConnectionImpl
         }
     }
 
+    @SuppressWarnings("resource") // getRawConnection() does not allocate
     PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws SQLException {
         if (pStmtPool == null) {
-            return connection.prepareStatement(sql, columnNames);
+            return getRawConnection().prepareStatement(sql, columnNames);
         }
         try {
             return pStmtPool.borrowObject(createKey(sql, columnNames));