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 2018/06/07 23:08:16 UTC

commons-dbcp git commit: Sort methods.

Repository: commons-dbcp
Updated Branches:
  refs/heads/master 377dd0a46 -> 79a242ec3


Sort methods.

Project: http://git-wip-us.apache.org/repos/asf/commons-dbcp/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-dbcp/commit/79a242ec
Tree: http://git-wip-us.apache.org/repos/asf/commons-dbcp/tree/79a242ec
Diff: http://git-wip-us.apache.org/repos/asf/commons-dbcp/diff/79a242ec

Branch: refs/heads/master
Commit: 79a242ec3ec8208a9187777cb40d8c269f3cdffb
Parents: 377dd0a
Author: Gary Gregory <ga...@gmail.com>
Authored: Thu Jun 7 17:08:12 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Thu Jun 7 17:08:12 2018 -0600

----------------------------------------------------------------------
 .../dbcp2/cpdsadapter/PooledConnectionImpl.java | 412 +++++++++----------
 1 file changed, 206 insertions(+), 206 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/79a242ec/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java
----------------------------------------------------------------------
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 0507dde..9f67b5a 100644
--- a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java
+++ b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java
@@ -101,9 +101,17 @@ class PooledConnectionImpl
         isClosed = false;
     }
 
-    public void setStatementPool(
-            final KeyedObjectPool<PStmtKeyCPDS, PoolablePreparedStatement<PStmtKeyCPDS>> statementPool) {
-        pstmtPool = statementPool;
+    /**
+     * My {@link KeyedPooledObjectFactory} method for activating
+     * {@link PreparedStatement}s.
+     * @param key ignored
+     * @param p ignored
+     */
+    @Override
+    public void activateObject(final PStmtKeyCPDS key,
+            final PooledObject<PoolablePreparedStatement<PStmtKeyCPDS>> p)
+            throws Exception {
+        p.getObject().activate();
     }
 
     /**
@@ -126,6 +134,15 @@ class PooledConnectionImpl
     /* JDBC_4_ANT_KEY_END */
 
     /**
+     * Throws an SQLException, if isClosed is true
+     */
+    private void assertOpen() throws SQLException {
+        if (isClosed) {
+            throw new SQLException(CLOSED);
+        }
+    }
+
+    /**
      * Closes the physical connection and marks this
      * <code>PooledConnection</code> so that it may not be used
      * to generate any more logical <code>Connection</code>s.
@@ -158,11 +175,84 @@ class PooledConnectionImpl
     }
 
     /**
-     * Throws an SQLException, if isClosed is true
+     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
      */
-    private void assertOpen() throws SQLException {
-        if (isClosed) {
-            throw new SQLException(CLOSED);
+    protected PStmtKeyCPDS createKey(final String sql) {
+        return new PStmtKeyCPDS(normalizeSQL(sql));
+    }
+
+    /**
+     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
+     */
+    protected PStmtKeyCPDS createKey(final String sql, final int autoGeneratedKeys) {
+        return new PStmtKeyCPDS(normalizeSQL(sql), autoGeneratedKeys);
+    }
+
+    /**
+     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
+     */
+    protected PStmtKeyCPDS createKey(final String sql, final int columnIndexes[]) {
+        return new PStmtKeyCPDS(normalizeSQL(sql), columnIndexes);
+    }
+
+    /**
+     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
+     */
+    protected PStmtKeyCPDS createKey(final String sql, final int resultSetType,
+                               final int resultSetConcurrency) {
+        return new PStmtKeyCPDS(normalizeSQL(sql), resultSetType,
+                            resultSetConcurrency);
+    }
+
+    /**
+     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
+     */
+    protected PStmtKeyCPDS createKey(final String sql, final int resultSetType,
+            final int resultSetConcurrency, final int resultSetHoldability) {
+        return new PStmtKeyCPDS(normalizeSQL(sql), resultSetType,
+                resultSetConcurrency, resultSetHoldability);
+    }
+
+    // -------------------------------------------------------------------
+    // The following code implements a PreparedStatement pool
+
+    /**
+     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
+     */
+    protected PStmtKeyCPDS createKey(final String sql, final String columnNames[]) {
+        return new PStmtKeyCPDS(normalizeSQL(sql), columnNames);
+    }
+
+    /**
+     * My {@link KeyedPooledObjectFactory} method for destroying
+     * {@link PreparedStatement}s.
+     * @param key ignored
+     * @param p the wrapped {@link PreparedStatement} to be destroyed.
+     */
+    @Override
+    public void destroyObject(final PStmtKeyCPDS key,
+            final PooledObject<PoolablePreparedStatement<PStmtKeyCPDS>> p)
+            throws Exception {
+        p.getObject().getInnermostDelegate().close();
+    }
+
+    /**
+     * Closes the physical connection and checks that the logical connection
+     * was closed as well.
+     */
+    @Override
+    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) {
+        }
+
+        // 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.");
         }
     }
 
@@ -190,39 +280,50 @@ class PooledConnectionImpl
     }
 
     /**
-     * {@inheritDoc}
+     * Returns the value of the accessToUnderlyingConnectionAllowed property.
+     *
+     * @return true if access to the underlying is allowed, false otherwise.
      */
-    @Override
-    public void removeConnectionEventListener(
-            final ConnectionEventListener listener) {
-        eventListeners.remove(listener);
-    }
-
-    /* JDBC_4_ANT_KEY_BEGIN */
-    @Override
-    public void removeStatementEventListener(final StatementEventListener listener) {
-        statementEventListeners.remove(listener);
+    public synchronized boolean isAccessToUnderlyingConnectionAllowed() {
+        return this.accessToUnderlyingConnectionAllowed;
     }
-    /* JDBC_4_ANT_KEY_END */
 
     /**
-     * Closes the physical connection and checks that the logical connection
-     * was closed as well.
+     * My {@link KeyedPooledObjectFactory} method for creating
+     * {@link PreparedStatement}s.
+     * @param key the key for the {@link PreparedStatement} to be created
      */
     @Override
-    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) {
+    public PooledObject<PoolablePreparedStatement<PStmtKeyCPDS>> makeObject(final PStmtKeyCPDS key) throws Exception {
+        if (null == key) {
+            throw new IllegalArgumentException();
         }
-
-        // 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.");
+        // _openPstmts++;
+        if (null == key.getResultSetType()
+                && null == key.getResultSetConcurrency()) {
+            if (null == key.getAutoGeneratedKeys()) {
+                return new DefaultPooledObject<>(new PoolablePreparedStatement<>(
+                        connection.prepareStatement(key.getSql()),
+                        key, pstmtPool, delegatingConnection));
+            }
+            return new DefaultPooledObject<>(new PoolablePreparedStatement<>(
+                            connection.prepareStatement(key.getSql(),
+                                    key.getAutoGeneratedKeys().intValue()),
+                            key, pstmtPool, delegatingConnection));
         }
+        return new DefaultPooledObject<>(new PoolablePreparedStatement<>(
+                connection.prepareStatement(key.getSql(),
+                        key.getResultSetType().intValue(),
+                        key.getResultSetConcurrency().intValue()),
+                        key, pstmtPool, delegatingConnection));
+    }
+
+    /**
+     * Normalize the given SQL statement, producing a
+     * canonical form that is semantically equivalent to the original.
+     */
+    protected String normalizeSQL(final String sql) {
+        return sql.trim();
     }
 
     /**
@@ -236,8 +337,20 @@ class PooledConnectionImpl
         }
     }
 
-    // -------------------------------------------------------------------
-    // The following code implements a PreparedStatement pool
+    /**
+     * My {@link KeyedPooledObjectFactory} method for passivating
+     * {@link PreparedStatement}s.  Currently invokes {@link PreparedStatement#clearParameters}.
+     * @param key ignored
+     * @param p a wrapped {@link PreparedStatement}
+     */
+    @Override
+    public void passivateObject(final PStmtKeyCPDS key,
+            final PooledObject<PoolablePreparedStatement<PStmtKeyCPDS>> p)
+            throws Exception {
+        final PoolablePreparedStatement<PStmtKeyCPDS> ppss = p.getObject();
+        ppss.clearParameters();
+        ppss.passivate();
+    }
 
     /**
      * Create or obtain a {@link PreparedStatement} from my pool.
@@ -259,6 +372,45 @@ class PooledConnectionImpl
 
     /**
      * Create or obtain a {@link PreparedStatement} from my pool.
+     * @param sql an SQL statement that may contain one or more '?' IN
+     *        parameter placeholders
+     * @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}
+     * @see Connection#prepareStatement(String, int)
+     */
+    PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys)
+            throws SQLException {
+        if (pstmtPool == null) {
+            return connection.prepareStatement(sql, autoGeneratedKeys);
+        }
+        try {
+            return pstmtPool.borrowObject(createKey(sql,autoGeneratedKeys));
+        } catch (final RuntimeException e) {
+            throw e;
+        } catch (final Exception e) {
+            throw new SQLException("Borrow prepareStatement from pool failed", e);
+        }
+    }
+
+    PreparedStatement prepareStatement(final String sql, final int columnIndexes[])
+    throws SQLException {
+        if (pstmtPool == null) {
+            return connection.prepareStatement(sql, columnIndexes);
+        }
+        try {
+            return pstmtPool.borrowObject(createKey(sql, columnIndexes));
+        } catch (final RuntimeException e) {
+            throw e;
+        } catch (final Exception e) {
+            throw new SQLException("Borrow prepareStatement from pool failed", e);
+        }
+    }
+
+    /**
+     * Create or obtain a {@link PreparedStatement} from my pool.
      * @param sql a <code>String</code> object that is the SQL statement to
      *            be sent to the database; may contain one or more '?' IN
      *            parameters
@@ -289,31 +441,6 @@ class PooledConnectionImpl
         }
     }
 
-    /**
-     * Create or obtain a {@link PreparedStatement} from my pool.
-     * @param sql an SQL statement that may contain one or more '?' IN
-     *        parameter placeholders
-     * @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}
-     * @see Connection#prepareStatement(String, int)
-     */
-    PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys)
-            throws SQLException {
-        if (pstmtPool == null) {
-            return connection.prepareStatement(sql, autoGeneratedKeys);
-        }
-        try {
-            return pstmtPool.borrowObject(createKey(sql,autoGeneratedKeys));
-        } catch (final RuntimeException e) {
-            throw e;
-        } catch (final Exception e) {
-            throw new SQLException("Borrow prepareStatement from pool failed", e);
-        }
-    }
-
     PreparedStatement prepareStatement(final String sql, final int resultSetType,
             final int resultSetConcurrency, final int resultSetHoldability)
     throws SQLException {
@@ -331,20 +458,6 @@ class PooledConnectionImpl
         }
     }
 
-    PreparedStatement prepareStatement(final String sql, final int columnIndexes[])
-    throws SQLException {
-        if (pstmtPool == null) {
-            return connection.prepareStatement(sql, columnIndexes);
-        }
-        try {
-            return pstmtPool.borrowObject(createKey(sql, columnIndexes));
-        } catch (final RuntimeException e) {
-            throw e;
-        } catch (final Exception e) {
-            throw new SQLException("Borrow prepareStatement from pool failed", e);
-        }
-    }
-
     PreparedStatement prepareStatement(final String sql, final String columnNames[])
     throws SQLException {
         if (pstmtPool == null) {
@@ -360,100 +473,35 @@ class PooledConnectionImpl
     }
 
     /**
-     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
-     */
-    protected PStmtKeyCPDS createKey(final String sql, final int autoGeneratedKeys) {
-        return new PStmtKeyCPDS(normalizeSQL(sql), autoGeneratedKeys);
-    }
-
-    /**
-     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
-     */
-    protected PStmtKeyCPDS createKey(final String sql, final int resultSetType,
-            final int resultSetConcurrency, final int resultSetHoldability) {
-        return new PStmtKeyCPDS(normalizeSQL(sql), resultSetType,
-                resultSetConcurrency, resultSetHoldability);
-    }
-
-    /**
-     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
-     */
-    protected PStmtKeyCPDS createKey(final String sql, final int columnIndexes[]) {
-        return new PStmtKeyCPDS(normalizeSQL(sql), columnIndexes);
-    }
-
-    /**
-     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
-     */
-    protected PStmtKeyCPDS createKey(final String sql, final String columnNames[]) {
-        return new PStmtKeyCPDS(normalizeSQL(sql), columnNames);
-    }
-
-    /**
-     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
-     */
-    protected PStmtKeyCPDS createKey(final String sql, final int resultSetType,
-                               final int resultSetConcurrency) {
-        return new PStmtKeyCPDS(normalizeSQL(sql), resultSetType,
-                            resultSetConcurrency);
-    }
-
-    /**
-     * Create a {@link PooledConnectionImpl.PStmtKey} for the given arguments.
+     * {@inheritDoc}
      */
-    protected PStmtKeyCPDS createKey(final String sql) {
-        return new PStmtKeyCPDS(normalizeSQL(sql));
+    @Override
+    public void removeConnectionEventListener(
+            final ConnectionEventListener listener) {
+        eventListeners.remove(listener);
     }
 
-    /**
-     * Normalize the given SQL statement, producing a
-     * canonical form that is semantically equivalent to the original.
-     */
-    protected String normalizeSQL(final String sql) {
-        return sql.trim();
+    /* JDBC_4_ANT_KEY_BEGIN */
+    @Override
+    public void removeStatementEventListener(final StatementEventListener listener) {
+        statementEventListeners.remove(listener);
     }
+    /* JDBC_4_ANT_KEY_END */
 
     /**
-     * My {@link KeyedPooledObjectFactory} method for creating
-     * {@link PreparedStatement}s.
-     * @param key the key for the {@link PreparedStatement} to be created
+     * Sets the value of the accessToUnderlyingConnectionAllowed property.
+     * It controls if the PoolGuard allows access to the underlying connection.
+     * (Default: false)
+     *
+     * @param allow Access to the underlying connection is granted when true.
      */
-    @Override
-    public PooledObject<PoolablePreparedStatement<PStmtKeyCPDS>> makeObject(final PStmtKeyCPDS key) throws Exception {
-        if (null == key) {
-            throw new IllegalArgumentException();
-        }
-        // _openPstmts++;
-        if (null == key.getResultSetType()
-                && null == key.getResultSetConcurrency()) {
-            if (null == key.getAutoGeneratedKeys()) {
-                return new DefaultPooledObject<>(new PoolablePreparedStatement<>(
-                        connection.prepareStatement(key.getSql()),
-                        key, pstmtPool, delegatingConnection));
-            }
-            return new DefaultPooledObject<>(new PoolablePreparedStatement<>(
-                            connection.prepareStatement(key.getSql(),
-                                    key.getAutoGeneratedKeys().intValue()),
-                            key, pstmtPool, delegatingConnection));
-        }
-        return new DefaultPooledObject<>(new PoolablePreparedStatement<>(
-                connection.prepareStatement(key.getSql(),
-                        key.getResultSetType().intValue(),
-                        key.getResultSetConcurrency().intValue()),
-                        key, pstmtPool, delegatingConnection));
+    public synchronized void setAccessToUnderlyingConnectionAllowed(final boolean allow) {
+        this.accessToUnderlyingConnectionAllowed = allow;
     }
 
-    /**
-     * My {@link KeyedPooledObjectFactory} method for destroying
-     * {@link PreparedStatement}s.
-     * @param key ignored
-     * @param p the wrapped {@link PreparedStatement} to be destroyed.
-     */
-    @Override
-    public void destroyObject(final PStmtKeyCPDS key,
-            final PooledObject<PoolablePreparedStatement<PStmtKeyCPDS>> p)
-            throws Exception {
-        p.getObject().getInnermostDelegate().close();
+    public void setStatementPool(
+            final KeyedObjectPool<PStmtKeyCPDS, PoolablePreparedStatement<PStmtKeyCPDS>> statementPool) {
+        pstmtPool = statementPool;
     }
 
     /**
@@ -468,52 +516,4 @@ class PooledConnectionImpl
             final PooledObject<PoolablePreparedStatement<PStmtKeyCPDS>> p) {
         return true;
     }
-
-    /**
-     * My {@link KeyedPooledObjectFactory} method for activating
-     * {@link PreparedStatement}s.
-     * @param key ignored
-     * @param p ignored
-     */
-    @Override
-    public void activateObject(final PStmtKeyCPDS key,
-            final PooledObject<PoolablePreparedStatement<PStmtKeyCPDS>> p)
-            throws Exception {
-        p.getObject().activate();
-    }
-
-    /**
-     * My {@link KeyedPooledObjectFactory} method for passivating
-     * {@link PreparedStatement}s.  Currently invokes {@link PreparedStatement#clearParameters}.
-     * @param key ignored
-     * @param p a wrapped {@link PreparedStatement}
-     */
-    @Override
-    public void passivateObject(final PStmtKeyCPDS key,
-            final PooledObject<PoolablePreparedStatement<PStmtKeyCPDS>> p)
-            throws Exception {
-        final PoolablePreparedStatement<PStmtKeyCPDS> ppss = p.getObject();
-        ppss.clearParameters();
-        ppss.passivate();
-    }
-
-    /**
-     * Returns the value of the accessToUnderlyingConnectionAllowed property.
-     *
-     * @return true if access to the underlying is allowed, false otherwise.
-     */
-    public synchronized boolean isAccessToUnderlyingConnectionAllowed() {
-        return this.accessToUnderlyingConnectionAllowed;
-    }
-
-    /**
-     * Sets the value of the accessToUnderlyingConnectionAllowed property.
-     * It controls if the PoolGuard allows access to the underlying connection.
-     * (Default: false)
-     *
-     * @param allow Access to the underlying connection is granted when true.
-     */
-    public synchronized void setAccessToUnderlyingConnectionAllowed(final boolean allow) {
-        this.accessToUnderlyingConnectionAllowed = allow;
-    }
 }