Repository: commons-dbcp Updated Branches: refs/heads/master 11dca5f87 -> f05518d99
Sort members in AB order so it's easier to find stuff. Project: http://git-wip-us.apache.org/repos/asf/commons-dbcp/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-dbcp/commit/f05518d9 Tree: http://git-wip-us.apache.org/repos/asf/commons-dbcp/tree/f05518d9 Diff: http://git-wip-us.apache.org/repos/asf/commons-dbcp/diff/f05518d9 Branch: refs/heads/master Commit: f05518d99227ade0b7928592c28a67ff1f0b3bf3 Parents: 11dca5f Author: Gary Gregory <garydgreg...@gmail.com> Authored: Sat Jun 16 17:39:08 2018 -0600 Committer: Gary Gregory <garydgreg...@gmail.com> Committed: Sat Jun 16 17:39:08 2018 -0600 ---------------------------------------------------------------------- .../commons/dbcp2/DelegatingStatement.java | 538 +++++++++---------- 1 file changed, 269 insertions(+), 269 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/f05518d9/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java index 76557d3..00f440f 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java @@ -42,6 +42,8 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { /** The connection that created me. **/ private DelegatingConnection<?> connection; + private boolean closed = false; + /** * Create a wrapper for the Statement which traces this Statement to the Connection which created it and the code * which created it. @@ -58,66 +60,60 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } /** - * Returns my underlying {@link Statement}. * - * @return my underlying {@link Statement}. - * @see #getInnermostDelegate + * @throws SQLException + * thrown by the delegating statement. + * @since 2.4.0 made public, was protected in 2.3.0. */ - public Statement getDelegate() { - return statement; + public void activate() throws SQLException { + if (statement instanceof DelegatingStatement) { + ((DelegatingStatement) statement).activate(); + } } - /** - * If my underlying {@link Statement} is not a {@code DelegatingStatement}, returns it, otherwise recursively - * invokes this method on my delegate. - * <p> - * Hence this method will return the first delegate that is not a {@code DelegatingStatement} or {@code null} when - * no non-{@code DelegatingStatement} delegate can be found by traversing this chain. - * </p> - * <p> - * This method is useful when you may have nested {@code DelegatingStatement}s, and you want to make sure to obtain - * a "genuine" {@link Statement}. - * </p> - * - * @return The innermost delegate. - * - * @see #getDelegate - */ - @SuppressWarnings("resource") - public Statement getInnermostDelegate() { - Statement s = statement; - while (s != null && s instanceof DelegatingStatement) { - s = ((DelegatingStatement) s).getDelegate(); - if (this == s) { - return null; - } + @Override + public void addBatch(final String sql) throws SQLException { + checkOpen(); + try { + statement.addBatch(sql); + } catch (final SQLException e) { + handleException(e); } - return s; } - /** - * Sets my delegate. - * - * @param statement - * my delegate. - */ - public void setDelegate(final Statement statement) { - this.statement = statement; + @Override + public void cancel() throws SQLException { + checkOpen(); + try { + statement.cancel(); + } catch (final SQLException e) { + handleException(e); + } } - private boolean closed = false; - - protected boolean isClosedInternal() { - return closed; + protected void checkOpen() throws SQLException { + if (isClosed()) { + throw new SQLException(this.getClass().getName() + " with address: \"" + this.toString() + "\" is closed."); + } } - protected void setClosedInternal(final boolean closed) { - this.closed = closed; + @Override + public void clearBatch() throws SQLException { + checkOpen(); + try { + statement.clearBatch(); + } catch (final SQLException e) { + handleException(e); + } } - protected void checkOpen() throws SQLException { - if (isClosed()) { - throw new SQLException(this.getClass().getName() + " with address: \"" + this.toString() + "\" is closed."); + @Override + public void clearWarnings() throws SQLException { + checkOpen(); + try { + statement.clearWarnings(); + } catch (final SQLException e) { + handleException(e); } } @@ -161,144 +157,136 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } } - protected void handleException(final SQLException e) throws SQLException { - if (connection != null) { - connection.handleException(e); - } else { - throw e; - } - } - - /** - * - * @throws SQLException - * thrown by the delegating statement. - * @since 2.4.0 made public, was protected in 2.3.0. - */ - public void activate() throws SQLException { - if (statement instanceof DelegatingStatement) { - ((DelegatingStatement) statement).activate(); - } - } - - /** - * - * @throws SQLException - * thrown by the delegating statement. - * @since 2.4.0 made public, was protected in 2.3.0. - */ - public void passivate() throws SQLException { - if (statement instanceof DelegatingStatement) { - ((DelegatingStatement) statement).passivate(); - } - } - @Override - public Connection getConnection() throws SQLException { + public void closeOnCompletion() throws SQLException { checkOpen(); - return getConnectionInternal(); // return the delegating connection that created this - } - - protected DelegatingConnection<?> getConnectionInternal() { - return connection; + try { + statement.closeOnCompletion(); + } catch (final SQLException e) { + handleException(e); + } } @Override - public ResultSet executeQuery(final String sql) throws SQLException { + public boolean execute(final String sql) throws SQLException { checkOpen(); if (connection != null) { connection.setLastUsed(); } try { - return DelegatingResultSet.wrapResultSet(this, statement.executeQuery(sql)); + return statement.execute(sql); } catch (final SQLException e) { handleException(e); - throw new AssertionError(); + return false; } } @Override - public ResultSet getResultSet() throws SQLException { + public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException { checkOpen(); + if (connection != null) { + connection.setLastUsed(); + } try { - return DelegatingResultSet.wrapResultSet(this, statement.getResultSet()); + return statement.execute(sql, autoGeneratedKeys); } catch (final SQLException e) { handleException(e); - throw new AssertionError(); + return false; } } @Override - public int executeUpdate(final String sql) throws SQLException { + public boolean execute(final String sql, final int columnIndexes[]) throws SQLException { checkOpen(); if (connection != null) { connection.setLastUsed(); } try { - return statement.executeUpdate(sql); + return statement.execute(sql, columnIndexes); } catch (final SQLException e) { handleException(e); - return 0; + return false; } } @Override - public int getMaxFieldSize() throws SQLException { + public boolean execute(final String sql, final String columnNames[]) throws SQLException { checkOpen(); + if (connection != null) { + connection.setLastUsed(); + } try { - return statement.getMaxFieldSize(); + return statement.execute(sql, columnNames); } catch (final SQLException e) { handleException(e); - return 0; + return false; } } @Override - public void setMaxFieldSize(final int max) throws SQLException { + public int[] executeBatch() throws SQLException { checkOpen(); + if (connection != null) { + connection.setLastUsed(); + } try { - statement.setMaxFieldSize(max); + return statement.executeBatch(); } catch (final SQLException e) { handleException(e); + throw new AssertionError(); } } @Override - public int getMaxRows() throws SQLException { + public ResultSet executeQuery(final String sql) throws SQLException { checkOpen(); + if (connection != null) { + connection.setLastUsed(); + } try { - return statement.getMaxRows(); + return DelegatingResultSet.wrapResultSet(this, statement.executeQuery(sql)); } catch (final SQLException e) { handleException(e); - return 0; + throw new AssertionError(); } } @Override - public void setMaxRows(final int max) throws SQLException { + public int executeUpdate(final String sql) throws SQLException { checkOpen(); + if (connection != null) { + connection.setLastUsed(); + } try { - statement.setMaxRows(max); + return statement.executeUpdate(sql); } catch (final SQLException e) { handleException(e); + return 0; } } @Override - public void setEscapeProcessing(final boolean enable) throws SQLException { + public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException { checkOpen(); + if (connection != null) { + connection.setLastUsed(); + } try { - statement.setEscapeProcessing(enable); + return statement.executeUpdate(sql, autoGeneratedKeys); } catch (final SQLException e) { handleException(e); + return 0; } } @Override - public int getQueryTimeout() throws SQLException { + public int executeUpdate(final String sql, final int columnIndexes[]) throws SQLException { checkOpen(); + if (connection != null) { + connection.setLastUsed(); + } try { - return statement.getQueryTimeout(); + return statement.executeUpdate(sql, columnIndexes); } catch (final SQLException e) { handleException(e); return 0; @@ -306,75 +294,130 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } @Override - public void setQueryTimeout(final int seconds) throws SQLException { + public int executeUpdate(final String sql, final String columnNames[]) throws SQLException { checkOpen(); + if (connection != null) { + connection.setLastUsed(); + } try { - statement.setQueryTimeout(seconds); + return statement.executeUpdate(sql, columnNames); } catch (final SQLException e) { handleException(e); + return 0; } } @Override - public void cancel() throws SQLException { + protected void finalize() throws Throwable { + // This is required because of statement pooling. The poolable + // statements will always be strongly held by the statement pool. If the + // delegating statements that wrap the poolable statement are not + // strongly held they will be garbage collected but at that point the + // poolable statements need to be returned to the pool else there will + // be a leak of statements from the pool. Closing this statement will + // close all the wrapped statements and return any poolable statements + // to the pool. + close(); + super.finalize(); + } + + @Override + public Connection getConnection() throws SQLException { checkOpen(); - try { - statement.cancel(); - } catch (final SQLException e) { - handleException(e); - } + return getConnectionInternal(); // return the delegating connection that created this + } + + protected DelegatingConnection<?> getConnectionInternal() { + return connection; + } + + /** + * Returns my underlying {@link Statement}. + * + * @return my underlying {@link Statement}. + * @see #getInnermostDelegate + */ + public Statement getDelegate() { + return statement; } @Override - public SQLWarning getWarnings() throws SQLException { + public int getFetchDirection() throws SQLException { checkOpen(); try { - return statement.getWarnings(); + return statement.getFetchDirection(); } catch (final SQLException e) { handleException(e); - throw new AssertionError(); + return 0; } } @Override - public void clearWarnings() throws SQLException { + public int getFetchSize() throws SQLException { checkOpen(); try { - statement.clearWarnings(); + return statement.getFetchSize(); } catch (final SQLException e) { handleException(e); + return 0; } } @Override - public void setCursorName(final String name) throws SQLException { + public ResultSet getGeneratedKeys() throws SQLException { checkOpen(); try { - statement.setCursorName(name); + return DelegatingResultSet.wrapResultSet(this, statement.getGeneratedKeys()); } catch (final SQLException e) { handleException(e); + throw new AssertionError(); + } + } + + /** + * If my underlying {@link Statement} is not a {@code DelegatingStatement}, returns it, otherwise recursively + * invokes this method on my delegate. + * <p> + * Hence this method will return the first delegate that is not a {@code DelegatingStatement} or {@code null} when + * no non-{@code DelegatingStatement} delegate can be found by traversing this chain. + * </p> + * <p> + * This method is useful when you may have nested {@code DelegatingStatement}s, and you want to make sure to obtain + * a "genuine" {@link Statement}. + * </p> + * + * @return The innermost delegate. + * + * @see #getDelegate + */ + @SuppressWarnings("resource") + public Statement getInnermostDelegate() { + Statement s = statement; + while (s != null && s instanceof DelegatingStatement) { + s = ((DelegatingStatement) s).getDelegate(); + if (this == s) { + return null; + } } + return s; } @Override - public boolean execute(final String sql) throws SQLException { + public int getMaxFieldSize() throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } try { - return statement.execute(sql); + return statement.getMaxFieldSize(); } catch (final SQLException e) { handleException(e); - return false; + return 0; } } @Override - public int getUpdateCount() throws SQLException { + public int getMaxRows() throws SQLException { checkOpen(); try { - return statement.getUpdateCount(); + return statement.getMaxRows(); } catch (final SQLException e) { handleException(e); return 0; @@ -393,20 +436,21 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } @Override - public void setFetchDirection(final int direction) throws SQLException { + public boolean getMoreResults(final int current) throws SQLException { checkOpen(); try { - statement.setFetchDirection(direction); + return statement.getMoreResults(current); } catch (final SQLException e) { handleException(e); + return false; } } @Override - public int getFetchDirection() throws SQLException { + public int getQueryTimeout() throws SQLException { checkOpen(); try { - return statement.getFetchDirection(); + return statement.getQueryTimeout(); } catch (final SQLException e) { handleException(e); return 0; @@ -414,20 +458,21 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } @Override - public void setFetchSize(final int rows) throws SQLException { + public ResultSet getResultSet() throws SQLException { checkOpen(); try { - statement.setFetchSize(rows); + return DelegatingResultSet.wrapResultSet(this, statement.getResultSet()); } catch (final SQLException e) { handleException(e); + throw new AssertionError(); } } @Override - public int getFetchSize() throws SQLException { + public int getResultSetConcurrency() throws SQLException { checkOpen(); try { - return statement.getFetchSize(); + return statement.getResultSetConcurrency(); } catch (final SQLException e) { handleException(e); return 0; @@ -435,10 +480,10 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } @Override - public int getResultSetConcurrency() throws SQLException { + public int getResultSetHoldability() throws SQLException { checkOpen(); try { - return statement.getResultSetConcurrency(); + return statement.getResultSetHoldability(); } catch (final SQLException e) { handleException(e); return 0; @@ -457,54 +502,52 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } @Override - public void addBatch(final String sql) throws SQLException { + public int getUpdateCount() throws SQLException { checkOpen(); try { - statement.addBatch(sql); + return statement.getUpdateCount(); } catch (final SQLException e) { handleException(e); + return 0; } } @Override - public void clearBatch() throws SQLException { + public SQLWarning getWarnings() throws SQLException { checkOpen(); try { - statement.clearBatch(); + return statement.getWarnings(); } catch (final SQLException e) { handleException(e); + throw new AssertionError(); } } - @Override - public int[] executeBatch() throws SQLException { - checkOpen(); + protected void handleException(final SQLException e) throws SQLException { if (connection != null) { - connection.setLastUsed(); - } - try { - return statement.executeBatch(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); + connection.handleException(e); + } else { + throw e; } } - /** - * Returns a String representation of this object. - * - * @return String + /* + * Note was protected prior to JDBC 4 */ @Override - public String toString() { - return statement == null ? "NULL" : statement.toString(); + public boolean isClosed() throws SQLException { + return closed; + } + + protected boolean isClosedInternal() { + return closed; } @Override - public boolean getMoreResults(final int current) throws SQLException { + public boolean isCloseOnCompletion() throws SQLException { checkOpen(); try { - return statement.getMoreResults(current); + return statement.isCloseOnCompletion(); } catch (final SQLException e) { handleException(e); return false; @@ -512,138 +555,110 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } @Override - public ResultSet getGeneratedKeys() throws SQLException { + public boolean isPoolable() throws SQLException { checkOpen(); try { - return DelegatingResultSet.wrapResultSet(this, statement.getGeneratedKeys()); + return statement.isPoolable(); } catch (final SQLException e) { handleException(e); - throw new AssertionError(); + return false; } } @Override - public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException { - checkOpen(); - if (connection != null) { - connection.setLastUsed(); + public boolean isWrapperFor(final Class<?> iface) throws SQLException { + if (iface.isAssignableFrom(getClass())) { + return true; + } else if (iface.isAssignableFrom(statement.getClass())) { + return true; + } else { + return statement.isWrapperFor(iface); } - try { - return statement.executeUpdate(sql, autoGeneratedKeys); - } catch (final SQLException e) { - handleException(e); - return 0; + } + + /** + * + * @throws SQLException + * thrown by the delegating statement. + * @since 2.4.0 made public, was protected in 2.3.0. + */ + public void passivate() throws SQLException { + if (statement instanceof DelegatingStatement) { + ((DelegatingStatement) statement).passivate(); } } + protected void setClosedInternal(final boolean closed) { + this.closed = closed; + } + @Override - public int executeUpdate(final String sql, final int columnIndexes[]) throws SQLException { + public void setCursorName(final String name) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } try { - return statement.executeUpdate(sql, columnIndexes); + statement.setCursorName(name); } catch (final SQLException e) { handleException(e); - return 0; } } + /** + * Sets my delegate. + * + * @param statement + * my delegate. + */ + public void setDelegate(final Statement statement) { + this.statement = statement; + } + @Override - public int executeUpdate(final String sql, final String columnNames[]) throws SQLException { + public void setEscapeProcessing(final boolean enable) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } try { - return statement.executeUpdate(sql, columnNames); + statement.setEscapeProcessing(enable); } catch (final SQLException e) { handleException(e); - return 0; } } @Override - public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException { + public void setFetchDirection(final int direction) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } try { - return statement.execute(sql, autoGeneratedKeys); + statement.setFetchDirection(direction); } catch (final SQLException e) { handleException(e); - return false; } } @Override - public boolean execute(final String sql, final int columnIndexes[]) throws SQLException { + public void setFetchSize(final int rows) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } try { - return statement.execute(sql, columnIndexes); + statement.setFetchSize(rows); } catch (final SQLException e) { handleException(e); - return false; } } @Override - public boolean execute(final String sql, final String columnNames[]) throws SQLException { + public void setMaxFieldSize(final int max) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } try { - return statement.execute(sql, columnNames); + statement.setMaxFieldSize(max); } catch (final SQLException e) { handleException(e); - return false; } } @Override - public int getResultSetHoldability() throws SQLException { + public void setMaxRows(final int max) throws SQLException { checkOpen(); try { - return statement.getResultSetHoldability(); + statement.setMaxRows(max); } catch (final SQLException e) { handleException(e); - return 0; - } - } - - /* - * Note was protected prior to JDBC 4 - */ - @Override - public boolean isClosed() throws SQLException { - return closed; - } - - @Override - public boolean isWrapperFor(final Class<?> iface) throws SQLException { - if (iface.isAssignableFrom(getClass())) { - return true; - } else if (iface.isAssignableFrom(statement.getClass())) { - return true; - } else { - return statement.isWrapperFor(iface); - } - } - - @Override - public <T> T unwrap(final Class<T> iface) throws SQLException { - if (iface.isAssignableFrom(getClass())) { - return iface.cast(this); - } else if (iface.isAssignableFrom(statement.getClass())) { - return iface.cast(statement); - } else { - return statement.unwrap(iface); } } @@ -658,48 +673,33 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } @Override - public boolean isPoolable() throws SQLException { + public void setQueryTimeout(final int seconds) throws SQLException { checkOpen(); try { - return statement.isPoolable(); + statement.setQueryTimeout(seconds); } catch (final SQLException e) { handleException(e); - return false; } } + /** + * Returns a String representation of this object. + * + * @return String + */ @Override - public void closeOnCompletion() throws SQLException { - checkOpen(); - try { - statement.closeOnCompletion(); - } catch (final SQLException e) { - handleException(e); - } + public String toString() { + return statement == null ? "NULL" : statement.toString(); } @Override - public boolean isCloseOnCompletion() throws SQLException { - checkOpen(); - try { - return statement.isCloseOnCompletion(); - } catch (final SQLException e) { - handleException(e); - return false; + public <T> T unwrap(final Class<T> iface) throws SQLException { + if (iface.isAssignableFrom(getClass())) { + return iface.cast(this); + } else if (iface.isAssignableFrom(statement.getClass())) { + return iface.cast(statement); + } else { + return statement.unwrap(iface); } } - - @Override - protected void finalize() throws Throwable { - // This is required because of statement pooling. The poolable - // statements will always be strongly held by the statement pool. If the - // delegating statements that wrap the poolable statement are not - // strongly held they will be garbage collected but at that point the - // poolable statements need to be returned to the pool else there will - // be a leak of statements from the pool. Closing this statement will - // close all the wrapped statements and return any poolable statements - // to the pool. - close(); - super.finalize(); - } }