commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject commons-dbcp git commit: Sort methods.
Date Fri, 08 Jun 2018 20:01:06 GMT
Repository: commons-dbcp
Updated Branches:
  refs/heads/master 45ce66540 -> 3e65ff983


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/3e65ff98
Tree: http://git-wip-us.apache.org/repos/asf/commons-dbcp/tree/3e65ff98
Diff: http://git-wip-us.apache.org/repos/asf/commons-dbcp/diff/3e65ff98

Branch: refs/heads/master
Commit: 3e65ff983dcaba073337d3a15b21dfbee2eee4c7
Parents: 45ce665
Author: Gary Gregory <garydgregory@gmail.com>
Authored: Fri Jun 8 14:01:04 2018 -0600
Committer: Gary Gregory <garydgregory@gmail.com>
Committed: Fri Jun 8 14:01:04 2018 -0600

----------------------------------------------------------------------
 .../apache/commons/dbcp2/PoolingConnection.java | 586 +++++++++----------
 1 file changed, 293 insertions(+), 293 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/3e65ff98/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java b/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java
index c000a65..aedc372 100644
--- a/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java
+++ b/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java
@@ -47,9 +47,19 @@ import org.apache.commons.pool2.impl.DefaultPooledObject;
 public class PoolingConnection extends DelegatingConnection<Connection>
         implements KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement> {
 
+    /**
+     * The possible statement types.
+     * @since 2.0
+     */
+    protected static enum StatementType {
+        CALLABLE_STATEMENT,
+        PREPARED_STATEMENT
+    }
+
     /** Pool of {@link PreparedStatement}s. and {@link CallableStatement}s */
     private KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> _pstmtPool = null;
 
+
     /**
      * Constructor.
      * @param c the underlying {@link Connection}.
@@ -59,12 +69,19 @@ public class PoolingConnection extends DelegatingConnection<Connection>
     }
 
 
-    public void setStatementPool(
-            final KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool) {
-        _pstmtPool = pool;
+    /**
+     * {@link KeyedPooledObjectFactory} method for activating
+     * pooled statements.
+     *
+     * @param key ignored
+     * @param p wrapped pooled statement to be activated
+     */
+    @Override
+    public void activateObject(final PStmtKey key,
+            final PooledObject<DelegatingPreparedStatement> p) throws Exception {
+        p.getObject().activate();
     }
 
-
     /**
      * Close and free all {@link PreparedStatement}s or
      * {@link CallableStatement}s from the pool, and close the underlying
@@ -94,218 +111,58 @@ public class PoolingConnection extends DelegatingConnection<Connection>
     }
 
     /**
-     * Create or obtain a {@link PreparedStatement} from the pool.
-     * @param sql the sql string used to define the PreparedStatement
-     * @return a {@link PoolablePreparedStatement}
-     */
-    @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);
-        }
-    }
-
-    @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);
-        }
-    }
-
-    /**
-     * Create or obtain a {@link PreparedStatement} from the pool.
-     * @param sql the sql string used to define the PreparedStatement
-     * @param resultSetType result set type
-     * @param resultSetConcurrency result set concurrency
-     * @return a {@link PoolablePreparedStatement}
-     */
-    @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);
-        }
-    }
-
-    /**
-     * Create or obtain a {@link CallableStatement} from the pool.
-     * @param sql the sql string used to define the CallableStatement
-     * @return a {@link PoolableCallableStatement}
-     * @throws SQLException
-     */
-    @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);
-        }
-    }
-
-    /**
-     * Create or obtain a {@link CallableStatement} from the pool.
-     * @param sql the sql string used to define the CallableStatement
-     * @param resultSetType result set type
-     * @param resultSetConcurrency result set concurrency
-     * @return a {@link PoolableCallableStatement}
-     * @throws SQLException
+     * Create a PStmtKey for the given arguments.
+     * @param sql the sql string used to define the statement
      */
-    @Override
-    public CallableStatement prepareCall(final String sql, final int resultSetType, final
int resultSetConcurrency) throws SQLException {
+    protected PStmtKey createKey(final String sql) {
+        String catalog = null;
         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);
+            catalog = getCatalog();
+        } catch (final SQLException e) {
+            // Ignored
         }
+        return new PStmtKey(normalizeSQL(sql), catalog);
     }
 
-    /**
-     * Create or obtain a {@link PreparedStatement} from the pool.
-     * @param sql the sql string used to define the PreparedStatement
-     * @param resultSetType result set type
-     * @param resultSetConcurrency result set concurrency
-     * @param resultSetHoldability result set holdability
-     * @return a {@link PoolablePreparedStatement}
-     */
-    @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.");
-        }
+    protected PStmtKey createKey(final String sql, final int autoGeneratedKeys) {
+        String catalog = null;
         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);
+            catalog = getCatalog();
+        } catch (final SQLException e) {
+            // Ignored
         }
+        return new PStmtKey(normalizeSQL(sql), catalog, autoGeneratedKeys);
     }
 
     /**
-     * Create or obtain a {@link PreparedStatement} from the pool.
-     * @param sql the sql string used to define the PreparedStatement
+     * Create a PStmtKey for the given arguments.
+     * @param sql the sql string used to define the statement
      * @param columnIndexes column indexes
-     * @return a {@link PoolablePreparedStatement}
-     */
-    @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);
-        }
-    }
-
-    /**
-     * Create or obtain a {@link PreparedStatement} from the pool.
-     * @param sql the sql string used to define the PreparedStatement
-     * @param columnNames column names
-     * @return a {@link PoolablePreparedStatement}
      */
-    @Override
-    public PreparedStatement prepareStatement(final String sql, final String columnNames[])
-            throws SQLException {
-        if (null == _pstmtPool) {
-            throw new SQLException(
-                    "Statement pool is null - closed or invalid PoolingConnection.");
-        }
+    protected PStmtKey createKey(final String sql, final int columnIndexes[]) {
+        String catalog = null;
         try {
-            return _pstmtPool.borrowObject(createKey(sql, columnNames));
-        } 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);
+            catalog = getCatalog();
+        } catch (final SQLException e) {
+            // Ignored
         }
+        return new PStmtKey(normalizeSQL(sql), catalog, columnIndexes);
     }
 
     /**
-     * Create or obtain a {@link CallableStatement} from the pool.
-     * @param sql the sql string used to define the CallableStatement
+     * Create a PStmtKey for the given arguments.
+     * @param sql the sql string used to define the statement
      * @param resultSetType result set type
      * @param resultSetConcurrency result set concurrency
-     * @param resultSetHoldability result set holdability
-     * @return a {@link PoolableCallableStatement}
-     * @throws SQLException
      */
-    @Override
-    public CallableStatement prepareCall(final String sql, final int resultSetType,
-            final int resultSetConcurrency, final int resultSetHoldability) throws SQLException
{
-        try {
-            return (CallableStatement) _pstmtPool.borrowObject(createKey(sql, resultSetType,
-                            resultSetConcurrency, resultSetHoldability, 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);
-        }
-    }
-
-    protected PStmtKey createKey(final String sql, final int autoGeneratedKeys) {
+    protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency)
{
         String catalog = null;
         try {
             catalog = getCatalog();
         } catch (final SQLException e) {
             // Ignored
         }
-        return new PStmtKey(normalizeSQL(sql), catalog, autoGeneratedKeys);
+        return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency);
     }
 
     /**
@@ -313,15 +170,17 @@ public class PoolingConnection extends DelegatingConnection<Connection>
      * @param sql the sql string used to define the statement
      * @param resultSetType result set type
      * @param resultSetConcurrency result set concurrency
+     * @param resultSetHoldability result set holdability
      */
-    protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency)
{
+    protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency,
+            final int resultSetHoldability) {
         String catalog = null;
         try {
             catalog = getCatalog();
         } catch (final SQLException e) {
             // Ignored
         }
-        return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency);
+        return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency,
resultSetHoldability);
     }
 
     /**
@@ -329,30 +188,35 @@ public class PoolingConnection extends DelegatingConnection<Connection>
      * @param sql the sql string used to define the statement
      * @param resultSetType result set type
      * @param resultSetConcurrency result set concurrency
+     * @param resultSetHoldability result set holdability
      * @param stmtType statement type
      */
-    protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency,
final StatementType stmtType) {
+    protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency,
+            final int resultSetHoldability, final StatementType stmtType) {
         String catalog = null;
         try {
             catalog = getCatalog();
         } catch (final SQLException e) {
             // Ignored
         }
-        return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency,
stmtType);
+        return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency,
resultSetHoldability,  stmtType);
     }
 
     /**
      * Create a PStmtKey for the given arguments.
      * @param sql the sql string used to define the statement
+     * @param resultSetType result set type
+     * @param resultSetConcurrency result set concurrency
+     * @param stmtType statement type
      */
-    protected PStmtKey createKey(final String sql) {
+    protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency,
final StatementType stmtType) {
         String catalog = null;
         try {
             catalog = getCatalog();
         } catch (final SQLException e) {
             // Ignored
         }
-        return new PStmtKey(normalizeSQL(sql), catalog);
+        return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency,
stmtType);
     }
 
     /**
@@ -373,58 +237,6 @@ public class PoolingConnection extends DelegatingConnection<Connection>
     /**
      * Create a PStmtKey for the given arguments.
      * @param sql the sql string used to define the statement
-     * @param resultSetType result set type
-     * @param resultSetConcurrency result set concurrency
-     * @param resultSetHoldability result set holdability
-     */
-    protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency,
-            final int resultSetHoldability) {
-        String catalog = null;
-        try {
-            catalog = getCatalog();
-        } catch (final SQLException e) {
-            // Ignored
-        }
-        return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency,
resultSetHoldability);
-    }
-
-    /**
-     * Create a PStmtKey for the given arguments.
-     * @param sql the sql string used to define the statement
-     * @param resultSetType result set type
-     * @param resultSetConcurrency result set concurrency
-     * @param resultSetHoldability result set holdability
-     * @param stmtType statement type
-     */
-    protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency,
-            final int resultSetHoldability, final StatementType stmtType) {
-        String catalog = null;
-        try {
-            catalog = getCatalog();
-        } catch (final SQLException e) {
-            // Ignored
-        }
-        return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency,
resultSetHoldability,  stmtType);
-    }
-
-    /**
-     * Create a PStmtKey for the given arguments.
-     * @param sql the sql string used to define the statement
-     * @param columnIndexes column indexes
-     */
-    protected PStmtKey createKey(final String sql, final int columnIndexes[]) {
-        String catalog = null;
-        try {
-            catalog = getCatalog();
-        } catch (final SQLException e) {
-            // Ignored
-        }
-        return new PStmtKey(normalizeSQL(sql), catalog, columnIndexes);
-    }
-
-    /**
-     * Create a PStmtKey for the given arguments.
-     * @param sql the sql string used to define the statement
      * @param columnNames column names
      */
     protected PStmtKey createKey(final String sql, final String columnNames[]) {
@@ -438,11 +250,18 @@ public class PoolingConnection extends DelegatingConnection<Connection>
     }
 
     /**
-     * Normalize the given SQL statement, producing a
-     * canonical form that is semantically equivalent to the original.
+     * {@link KeyedPooledObjectFactory} method for destroying
+     * PoolablePreparedStatements and PoolableCallableStatements.
+     * Closes the underlying statement.
+     *
+     * @param key ignored
+     * @param p the wrapped pooled statement to be destroyed.
      */
-    protected String normalizeSQL(final String sql) {
-        return sql.trim();
+    @Override
+    public void destroyObject(final PStmtKey key,
+            final PooledObject<DelegatingPreparedStatement> p)
+            throws Exception {
+        p.getObject().getInnermostDelegate().close();
     }
 
     /**
@@ -473,61 +292,237 @@ public class PoolingConnection extends DelegatingConnection<Connection>
     }
 
     /**
-     * {@link KeyedPooledObjectFactory} method for destroying
-     * PoolablePreparedStatements and PoolableCallableStatements.
-     * Closes the underlying statement.
+     * 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();
+    }
+
+    /**
+     * {@link KeyedPooledObjectFactory} method for passivating
+     * {@link PreparedStatement}s or {@link CallableStatement}s.
+     * Invokes {@link PreparedStatement#clearParameters}.
      *
      * @param key ignored
-     * @param p the wrapped pooled statement to be destroyed.
+     * @param p a wrapped {@link PreparedStatement}
      */
     @Override
-    public void destroyObject(final PStmtKey key,
-            final PooledObject<DelegatingPreparedStatement> p)
-            throws Exception {
-        p.getObject().getInnermostDelegate().close();
+    public void passivateObject(final PStmtKey key,
+            final PooledObject<DelegatingPreparedStatement> p) throws Exception {
+        final DelegatingPreparedStatement dps = p.getObject();
+        dps.clearParameters();
+        dps.passivate();
     }
 
     /**
-     * {@link KeyedPooledObjectFactory} method for validating
-     * pooled statements. Currently always returns true.
-     *
-     * @param key ignored
-     * @param p ignored
-     * @return {@code true}
+     * Create or obtain a {@link CallableStatement} from the pool.
+     * @param sql the sql string used to define the CallableStatement
+     * @return a {@link PoolableCallableStatement}
+     * @throws SQLException
      */
     @Override
-    public boolean validateObject(final PStmtKey key,
-            final PooledObject<DelegatingPreparedStatement> p) {
-        return true;
+    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);
+        }
     }
 
     /**
-     * {@link KeyedPooledObjectFactory} method for activating
-     * pooled statements.
-     *
-     * @param key ignored
-     * @param p wrapped pooled statement to be activated
+     * Create or obtain a {@link CallableStatement} from the pool.
+     * @param sql the sql string used to define the CallableStatement
+     * @param resultSetType result set type
+     * @param resultSetConcurrency result set concurrency
+     * @return a {@link PoolableCallableStatement}
+     * @throws SQLException
      */
     @Override
-    public void activateObject(final PStmtKey key,
-            final PooledObject<DelegatingPreparedStatement> p) throws Exception {
-        p.getObject().activate();
+    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);
+        }
     }
 
     /**
-     * {@link KeyedPooledObjectFactory} method for passivating
-     * {@link PreparedStatement}s or {@link CallableStatement}s.
-     * Invokes {@link PreparedStatement#clearParameters}.
-     *
-     * @param key ignored
-     * @param p a wrapped {@link PreparedStatement}
+     * Create or obtain a {@link CallableStatement} from the pool.
+     * @param sql the sql string used to define the CallableStatement
+     * @param resultSetType result set type
+     * @param resultSetConcurrency result set concurrency
+     * @param resultSetHoldability result set holdability
+     * @return a {@link PoolableCallableStatement}
+     * @throws SQLException
      */
     @Override
-    public void passivateObject(final PStmtKey key,
-            final PooledObject<DelegatingPreparedStatement> p) throws Exception {
-        final DelegatingPreparedStatement dps = p.getObject();
-        dps.clearParameters();
-        dps.passivate();
+    public CallableStatement prepareCall(final String sql, final int resultSetType,
+            final int resultSetConcurrency, final int resultSetHoldability) throws SQLException
{
+        try {
+            return (CallableStatement) _pstmtPool.borrowObject(createKey(sql, resultSetType,
+                            resultSetConcurrency, resultSetHoldability, 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);
+        }
+    }
+
+    /**
+     * Create or obtain a {@link PreparedStatement} from the pool.
+     * @param sql the sql string used to define the PreparedStatement
+     * @return a {@link PoolablePreparedStatement}
+     */
+    @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);
+        }
+    }
+
+    @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);
+        }
+    }
+
+    /**
+     * Create or obtain a {@link PreparedStatement} from the pool.
+     * @param sql the sql string used to define the PreparedStatement
+     * @param columnIndexes column indexes
+     * @return a {@link PoolablePreparedStatement}
+     */
+    @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);
+        }
+    }
+
+    /**
+     * Create or obtain a {@link PreparedStatement} from the pool.
+     * @param sql the sql string used to define the PreparedStatement
+     * @param resultSetType result set type
+     * @param resultSetConcurrency result set concurrency
+     * @return a {@link PoolablePreparedStatement}
+     */
+    @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);
+        }
+    }
+
+    /**
+     * Create or obtain a {@link PreparedStatement} from the pool.
+     * @param sql the sql string used to define the PreparedStatement
+     * @param resultSetType result set type
+     * @param resultSetConcurrency result set concurrency
+     * @param resultSetHoldability result set holdability
+     * @return a {@link PoolablePreparedStatement}
+     */
+    @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);
+        }
+    }
+
+    /**
+     * Create or obtain a {@link PreparedStatement} from the pool.
+     * @param sql the sql string used to define the PreparedStatement
+     * @param columnNames column names
+     * @return a {@link PoolablePreparedStatement}
+     */
+    @Override
+    public PreparedStatement prepareStatement(final String sql, final String columnNames[])
+            throws SQLException {
+        if (null == _pstmtPool) {
+            throw new SQLException(
+                    "Statement pool is null - closed or invalid PoolingConnection.");
+        }
+        try {
+            return _pstmtPool.borrowObject(createKey(sql, columnNames));
+        } 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);
+        }
+    }
+
+    public void setStatementPool(
+            final KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool) {
+        _pstmtPool = pool;
     }
 
     @Override
@@ -539,11 +534,16 @@ public class PoolingConnection extends DelegatingConnection<Connection>
     }
 
     /**
-     * The possible statement types.
-     * @since 2.0
+     * {@link KeyedPooledObjectFactory} method for validating
+     * pooled statements. Currently always returns true.
+     *
+     * @param key ignored
+     * @param p ignored
+     * @return {@code true}
      */
-    protected static enum StatementType {
-        CALLABLE_STATEMENT,
-        PREPARED_STATEMENT
+    @Override
+    public boolean validateObject(final PStmtKey key,
+            final PooledObject<DelegatingPreparedStatement> p) {
+        return true;
     }
 }


Mime
View raw message