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 63ab089  Update to PR#36 - PrepareStatement and prepareCall methods 
are extracted (#37)
63ab089 is described below

commit 63ab089fbd4d408b04d5fe893581088b0ac2b4bc
Author: Alexander Norz <github....@norz.de>
AuthorDate: Mon Aug 10 22:17:07 2020 +0200

    Update to PR#36 - PrepareStatement and prepareCall methods are extracted 
(#37)
    
    * PrepareCall methods are extracted
    
    * PrepareStatement methods are extracted
    
    * Refinement - final PStmtKey, JavaDoc variable name
    
    - Set parameter PStmtKey in extracted methods final
    - Added and completed some JavaDoc
    - Renamed variable in method getSchemaOrNull()
    
    Co-authored-by: DoiMasayuki <m-...@ist.osaka-u.ac.jp>
---
 .../apache/commons/dbcp2/PoolingConnection.java    | 164 ++++++++++-----------
 1 file changed, 77 insertions(+), 87 deletions(-)

diff --git a/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java 
b/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java
index 8237dc7..f713428 100644
--- a/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java
+++ b/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java
@@ -136,7 +136,8 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
      * @param sql
      *            the SQL string used to define the statement
      * @param columnIndexes
-     *            column indexes
+     *            An array of column indexes indicating the columns that 
should be returned from the inserted row or
+     *            rows.
      *
      * @return the PStmtKey created for the given arguments.
      */
@@ -144,6 +145,17 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
         return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), 
getSchemaOrNull(), columnIndexes);
     }
 
+    /**
+     * Creates a PStmtKey for the given arguments.
+     *
+     * @param sql
+     *            the SQL string used to define the statement
+     * @param autoGeneratedKeys
+     *            A flag indicating whether auto-generated keys should be 
returned; one of
+     *            <code>Statement.RETURN_GENERATED_KEYS</code> or 
<code>Statement.NO_GENERATED_KEYS</code>.
+     *
+     * @return the PStmtKey created for the given arguments.
+     */
     protected PStmtKey createKey(final String sql, final int 
autoGeneratedKeys) {
         return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), 
getSchemaOrNull(), autoGeneratedKeys);
     }
@@ -279,13 +291,13 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
     }
 
     private String getSchemaOrNull() {
-        String catalog = null;
+        String schema = null;
         try {
-            catalog = getSchema();
+            schema = getSchema();
         } catch (final SQLException e) {
             // Ignored
         }
-        return catalog;
+        return schema;
     }
 
     /**
@@ -354,15 +366,7 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
      */
     @Override
     public CallableStatement prepareCall(final String sql) throws SQLException 
{
-        try {
-            return (CallableStatement) pstmtPool.borrowObject(createKey(sql, 
StatementType.CALLABLE_STATEMENT));
-        } catch (final NoSuchElementException e) {
-            throw new SQLException("MaxOpenCallableStatements limit reached", 
e);
-        } catch (final RuntimeException e) {
-            throw e;
-        } catch (final Exception e) {
-            throw new SQLException("Borrow callableStatement from pool 
failed", e);
-        }
+        return prepareCall(createKey(sql, StatementType.CALLABLE_STATEMENT));
     }
 
     /**
@@ -381,16 +385,7 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
     @Override
     public CallableStatement prepareCall(final String sql, final int 
resultSetType, final int resultSetConcurrency)
             throws SQLException {
-        try {
-            return (CallableStatement) pstmtPool.borrowObject(
-                    createKey(sql, resultSetType, resultSetConcurrency, 
StatementType.CALLABLE_STATEMENT));
-        } catch (final NoSuchElementException e) {
-            throw new SQLException("MaxOpenCallableStatements limit reached", 
e);
-        } catch (final RuntimeException e) {
-            throw e;
-        } catch (final Exception e) {
-            throw new SQLException("Borrow callableStatement from pool 
failed", e);
-        }
+        return prepareCall(createKey(sql, resultSetType, resultSetConcurrency, 
StatementType.CALLABLE_STATEMENT));
     }
 
     /**
@@ -411,9 +406,22 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
     @Override
     public CallableStatement prepareCall(final String sql, final int 
resultSetType, final int resultSetConcurrency,
             final int resultSetHoldability) throws SQLException {
+        return prepareCall(createKey(sql, resultSetType, resultSetConcurrency,
+                resultSetHoldability, StatementType.CALLABLE_STATEMENT));
+    }
+
+    /**
+     * Creates or obtains a {@link CallableStatement} from the pool.
+     *
+     * @param key 
+     *            a {@link PStmtKey} for the given arguments
+     * @return a {@link PoolableCallableStatement}
+     * @throws SQLException
+     *             Wraps an underlying exception.
+     */
+    private CallableStatement prepareCall(final PStmtKey key) throws 
SQLException {
         try {
-            return (CallableStatement) pstmtPool.borrowObject(createKey(sql, 
resultSetType, resultSetConcurrency,
-                    resultSetHoldability, StatementType.CALLABLE_STATEMENT));
+            return (CallableStatement) pstmtPool.borrowObject(key);
         } catch (final NoSuchElementException e) {
             throw new SQLException("MaxOpenCallableStatements limit reached", 
e);
         } catch (final RuntimeException e) {
@@ -429,37 +437,29 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
      * @param sql
      *            the SQL string used to define the PreparedStatement
      * @return a {@link PoolablePreparedStatement}
+     * @throws SQLException
+     *             Wraps an underlying exception.
      */
     @Override
     public PreparedStatement prepareStatement(final String sql) throws 
SQLException {
-        if (null == pstmtPool) {
-            throw new SQLException("Statement pool is null - closed or invalid 
PoolingConnection.");
-        }
-        try {
-            return pstmtPool.borrowObject(createKey(sql));
-        } catch (final NoSuchElementException e) {
-            throw new SQLException("MaxOpenPreparedStatements limit reached", 
e);
-        } catch (final RuntimeException e) {
-            throw e;
-        } catch (final Exception e) {
-            throw new SQLException("Borrow prepareStatement from pool failed", 
e);
-        }
+        return prepareStatement(createKey(sql));
     }
 
+    /*
+     * Creates or obtains a {@link PreparedStatement} from the pool.
+     *
+     * @param sql
+     *            the SQL string used to define the PreparedStatement
+     * @param autoGeneratedKeys
+     *            A flag indicating whether auto-generated keys should be 
returned; one of
+     *            <code>Statement.RETURN_GENERATED_KEYS</code> or 
<code>Statement.NO_GENERATED_KEYS</code>.
+     * @return a {@link PoolablePreparedStatement}
+     * @throws SQLException
+     *             Wraps an underlying exception.
+     */
     @Override
     public PreparedStatement prepareStatement(final String sql, final int 
autoGeneratedKeys) throws SQLException {
-        if (null == pstmtPool) {
-            throw new SQLException("Statement pool is null - closed or invalid 
PoolingConnection.");
-        }
-        try {
-            return pstmtPool.borrowObject(createKey(sql, autoGeneratedKeys));
-        } catch (final NoSuchElementException e) {
-            throw new SQLException("MaxOpenPreparedStatements limit reached", 
e);
-        } catch (final RuntimeException e) {
-            throw e;
-        } catch (final Exception e) {
-            throw new SQLException("Borrow prepareStatement from pool failed", 
e);
-        }
+        return prepareStatement(createKey(sql, autoGeneratedKeys));
     }
 
     /**
@@ -468,23 +468,16 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
      * @param sql
      *            the SQL string used to define the PreparedStatement
      * @param columnIndexes
-     *            column indexes
+     *            An array of column indexes indicating the columns that 
should be returned from the inserted row or
+     *            rows.
      * @return a {@link PoolablePreparedStatement}
+     * @throws SQLException
+     *             Wraps an underlying exception.
+     *             
      */
     @Override
     public PreparedStatement prepareStatement(final String sql, final int 
columnIndexes[]) throws SQLException {
-        if (null == pstmtPool) {
-            throw new SQLException("Statement pool is null - closed or invalid 
PoolingConnection.");
-        }
-        try {
-            return pstmtPool.borrowObject(createKey(sql, columnIndexes));
-        } catch (final NoSuchElementException e) {
-            throw new SQLException("MaxOpenPreparedStatements limit reached", 
e);
-        } catch (final RuntimeException e) {
-            throw e;
-        } catch (final Exception e) {
-            throw new SQLException("Borrow prepareStatement from pool failed", 
e);
-        }
+        return prepareStatement(createKey(sql, columnIndexes));
     }
 
     /**
@@ -497,22 +490,13 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
      * @param resultSetConcurrency
      *            result set concurrency
      * @return a {@link PoolablePreparedStatement}
+     * @throws SQLException
+     *             Wraps an underlying exception.
      */
     @Override
     public PreparedStatement prepareStatement(final String sql, final int 
resultSetType, final int resultSetConcurrency)
             throws SQLException {
-        if (null == pstmtPool) {
-            throw new SQLException("Statement pool is null - closed or invalid 
PoolingConnection.");
-        }
-        try {
-            return pstmtPool.borrowObject(createKey(sql, resultSetType, 
resultSetConcurrency));
-        } catch (final NoSuchElementException e) {
-            throw new SQLException("MaxOpenPreparedStatements limit reached", 
e);
-        } catch (final RuntimeException e) {
-            throw e;
-        } catch (final Exception e) {
-            throw new SQLException("Borrow prepareStatement from pool failed", 
e);
-        }
+        return prepareStatement(createKey(sql, resultSetType, 
resultSetConcurrency));
     }
 
     /**
@@ -527,22 +511,13 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
      * @param resultSetHoldability
      *            result set holdability
      * @return a {@link PoolablePreparedStatement}
+     * @throws SQLException
+     *             Wraps an underlying exception.
      */
     @Override
     public PreparedStatement prepareStatement(final String sql, final int 
resultSetType, final int resultSetConcurrency,
             final int resultSetHoldability) throws SQLException {
-        if (null == pstmtPool) {
-            throw new SQLException("Statement pool is null - closed or invalid 
PoolingConnection.");
-        }
-        try {
-            return pstmtPool.borrowObject(createKey(sql, resultSetType, 
resultSetConcurrency, resultSetHoldability));
-        } catch (final NoSuchElementException e) {
-            throw new SQLException("MaxOpenPreparedStatements limit reached", 
e);
-        } catch (final RuntimeException e) {
-            throw e;
-        } catch (final Exception e) {
-            throw new SQLException("Borrow prepareStatement from pool failed", 
e);
-        }
+        return prepareStatement(createKey(sql, resultSetType, 
resultSetConcurrency, resultSetHoldability));
     }
 
     /**
@@ -553,14 +528,29 @@ public class PoolingConnection extends 
DelegatingConnection<Connection>
      * @param columnNames
      *            column names
      * @return a {@link PoolablePreparedStatement}
+     * @throws SQLException
+     *             Wraps an underlying exception.
      */
     @Override
     public PreparedStatement prepareStatement(final String sql, final String 
columnNames[]) throws SQLException {
+        return prepareStatement(createKey(sql, columnNames));
+    }
+
+    /**
+     * Creates or obtains a {@link PreparedStatement} from the pool.
+     *
+     * @param key 
+     *            a {@link PStmtKey} for the given arguments
+     * @return a {@link PoolablePreparedStatement}
+     * @throws SQLException
+     *             Wraps an underlying exception.
+     */
+    private PreparedStatement prepareStatement(final PStmtKey key) throws 
SQLException {
         if (null == pstmtPool) {
             throw new SQLException("Statement pool is null - closed or invalid 
PoolingConnection.");
         }
         try {
-            return pstmtPool.borrowObject(createKey(sql, columnNames));
+            return pstmtPool.borrowObject(key);
         } catch (final NoSuchElementException e) {
             throw new SQLException("MaxOpenPreparedStatements limit reached", 
e);
         } catch (final RuntimeException e) {

Reply via email to