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 3100156 Add @SuppressWarnings with comment and make some internals simpler looking. 3100156 is described below commit 31001566f826c18b3794256154225eb7c316d2fd Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun Jun 6 11:15:26 2021 -0400 Add @SuppressWarnings with comment and make some internals simpler looking. --- .../apache/commons/dbcp2/DelegatingConnection.java | 112 ++++++++++----------- .../apache/commons/dbcp2/DelegatingStatement.java | 3 + .../commons/dbcp2/PoolableConnectionFactory.java | 26 ++--- 3 files changed, 70 insertions(+), 71 deletions(-) diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java b/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java index 2080716..969f250 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java @@ -76,11 +76,10 @@ public class DelegatingConnection<C extends Connection> extends AbandonedTrace i /** * Creates a wrapper for the Connection which traces this Connection in the AbandonedObjectPool. * - * @param c - * the {@link Connection} to delegate all calls to. + * @param connection the {@link Connection} to delegate all calls to. */ - public DelegatingConnection(final C c) { - connection = c; + public DelegatingConnection(final C connection) { + this.connection = connection; } /** @@ -124,6 +123,11 @@ public class DelegatingConnection<C extends Connection> extends AbandonedTrace i return getDelegateInternal(); } + /** + * Gets the delegate connection. + * + * @return the delegate connection. + */ protected final C getDelegateInternal() { return connection; } @@ -264,90 +268,91 @@ public class DelegatingConnection<C extends Connection> extends AbandonedTrace i return e; } - private void initializeStatement(final DelegatingStatement ds) throws SQLException { - if (defaultQueryTimeoutSeconds != null && defaultQueryTimeoutSeconds != ds.getQueryTimeout()) { - ds.setQueryTimeout(defaultQueryTimeoutSeconds); + /** + * Initializes the given statement with this connection's settings. + * + * @param <T> The DelegatingStatement type. + * @param delegatingStatement The DelegatingStatement to initialize. + * @return The given DelegatingStatement. + * @throws SQLException if a database access error occurs, this method is called on a closed Statement. + */ + private <T extends DelegatingStatement> T init(final T delegatingStatement) throws SQLException { + if (defaultQueryTimeoutSeconds != null && defaultQueryTimeoutSeconds != delegatingStatement.getQueryTimeout()) { + delegatingStatement.setQueryTimeout(defaultQueryTimeoutSeconds); } + return delegatingStatement; } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public Statement createStatement() throws SQLException { checkOpen(); try { - final DelegatingStatement ds = new DelegatingStatement(this, connection.createStatement()); - initializeStatement(ds); - return ds; + return init(new DelegatingStatement(this, connection.createStatement())); } catch (final SQLException e) { handleException(e); return null; } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public Statement createStatement(final int resultSetType, final int resultSetConcurrency) throws SQLException { checkOpen(); try { - final DelegatingStatement ds = new DelegatingStatement(this, - connection.createStatement(resultSetType, resultSetConcurrency)); - initializeStatement(ds); - return ds; + return init(new DelegatingStatement(this, connection.createStatement(resultSetType, resultSetConcurrency))); } catch (final SQLException e) { handleException(e); return null; } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public PreparedStatement prepareStatement(final String sql) throws SQLException { checkOpen(); try { - final DelegatingPreparedStatement dps = new DelegatingPreparedStatement(this, - connection.prepareStatement(sql)); - initializeStatement(dps); - return dps; + return init(new DelegatingPreparedStatement(this, connection.prepareStatement(sql))); } catch (final SQLException e) { handleException(e); return null; } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) - throws SQLException { + throws SQLException { checkOpen(); try { - final DelegatingPreparedStatement dps = new DelegatingPreparedStatement(this, - connection.prepareStatement(sql, resultSetType, resultSetConcurrency)); - initializeStatement(dps); - return dps; + return init(new DelegatingPreparedStatement(this, + connection.prepareStatement(sql, resultSetType, resultSetConcurrency))); } catch (final SQLException e) { handleException(e); return null; } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public CallableStatement prepareCall(final String sql) throws SQLException { checkOpen(); try { - final DelegatingCallableStatement dcs = new DelegatingCallableStatement(this, connection.prepareCall(sql)); - initializeStatement(dcs); - return dcs; + return init(new DelegatingCallableStatement(this, connection.prepareCall(sql))); } catch (final SQLException e) { handleException(e); return null; } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) - throws SQLException { + throws SQLException { checkOpen(); try { - final DelegatingCallableStatement dcs = new DelegatingCallableStatement(this, - connection.prepareCall(sql, resultSetType, resultSetConcurrency)); - initializeStatement(dcs); - return dcs; + return init(new DelegatingCallableStatement(this, + connection.prepareCall(sql, resultSetType, resultSetConcurrency))); } catch (final SQLException e) { handleException(e); return null; @@ -713,87 +718,78 @@ public class DelegatingConnection<C extends Connection> extends AbandonedTrace i } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public Statement createStatement(final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { + final int resultSetHoldability) throws SQLException { checkOpen(); try { - final DelegatingStatement ds = new DelegatingStatement(this, - connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability)); - initializeStatement(ds); - return ds; + return init(new DelegatingStatement(this, + connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability))); } catch (final SQLException e) { handleException(e); return null; } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { + final int resultSetHoldability) throws SQLException { checkOpen(); try { - final DelegatingPreparedStatement dps = new DelegatingPreparedStatement(this, - connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); - initializeStatement(dps); - return dps; + return init(new DelegatingPreparedStatement(this, + connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability))); } catch (final SQLException e) { handleException(e); return null; } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { + final int resultSetHoldability) throws SQLException { checkOpen(); try { - final DelegatingCallableStatement dcs = new DelegatingCallableStatement(this, - connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); - initializeStatement(dcs); - return dcs; + return init(new DelegatingCallableStatement(this, + connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability))); } catch (final SQLException e) { handleException(e); return null; } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException { checkOpen(); try { - final DelegatingPreparedStatement dps = new DelegatingPreparedStatement(this, - connection.prepareStatement(sql, autoGeneratedKeys)); - initializeStatement(dps); - return dps; + return init(new DelegatingPreparedStatement(this, connection.prepareStatement(sql, autoGeneratedKeys))); } catch (final SQLException e) { handleException(e); return null; } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws SQLException { checkOpen(); try { - final DelegatingPreparedStatement dps = new DelegatingPreparedStatement(this, - connection.prepareStatement(sql, columnIndexes)); - initializeStatement(dps); - return dps; + return init(new DelegatingPreparedStatement(this, connection.prepareStatement(sql, columnIndexes))); } catch (final SQLException e) { handleException(e); return null; } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws SQLException { checkOpen(); try { - final DelegatingPreparedStatement dps = new DelegatingPreparedStatement(this, - connection.prepareStatement(sql, columnNames)); - initializeStatement(dps); - return dps; + return init(new DelegatingPreparedStatement(this, connection.prepareStatement(sql, columnNames))); } catch (final SQLException e) { handleException(e); return null; diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java index 467ad9c..0fd5783 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java @@ -320,6 +320,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public ResultSet executeQuery(final String sql) throws SQLException { checkOpen(); @@ -436,6 +437,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public ResultSet getGeneratedKeys() throws SQLException { checkOpen(); @@ -558,6 +560,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } } + @SuppressWarnings("resource") // Caller is responsible for closing the resource. @Override public ResultSet getResultSet() throws SQLException { checkOpen(); diff --git a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java index ee0930f..f820037 100644 --- a/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java @@ -113,26 +113,26 @@ public class PoolableConnectionFactory implements PooledObjectFactory<PoolableCo validateLifetime(p); - final PoolableConnection conn = p.getObject(); - conn.activate(); + final PoolableConnection pConnection = p.getObject(); + pConnection.activate(); - if (defaultAutoCommit != null && conn.getAutoCommit() != defaultAutoCommit) { - conn.setAutoCommit(defaultAutoCommit); + if (defaultAutoCommit != null && pConnection.getAutoCommit() != defaultAutoCommit) { + pConnection.setAutoCommit(defaultAutoCommit); } if (defaultTransactionIsolation != UNKNOWN_TRANSACTION_ISOLATION - && conn.getTransactionIsolation() != defaultTransactionIsolation) { - conn.setTransactionIsolation(defaultTransactionIsolation); + && pConnection.getTransactionIsolation() != defaultTransactionIsolation) { + pConnection.setTransactionIsolation(defaultTransactionIsolation); } - if (defaultReadOnly != null && conn.isReadOnly() != defaultReadOnly) { - conn.setReadOnly(defaultReadOnly); + if (defaultReadOnly != null && pConnection.isReadOnly() != defaultReadOnly) { + pConnection.setReadOnly(defaultReadOnly); } - if (defaultCatalog != null && !defaultCatalog.equals(conn.getCatalog())) { - conn.setCatalog(defaultCatalog); + if (defaultCatalog != null && !defaultCatalog.equals(pConnection.getCatalog())) { + pConnection.setCatalog(defaultCatalog); } - if (defaultSchema != null && !defaultSchema.equals(Jdbc41Bridge.getSchema(conn))) { - Jdbc41Bridge.setSchema(conn, defaultSchema); + if (defaultSchema != null && !defaultSchema.equals(Jdbc41Bridge.getSchema(pConnection))) { + Jdbc41Bridge.setSchema(pConnection, defaultSchema); } - conn.setDefaultQueryTimeout(defaultQueryTimeoutSeconds); + pConnection.setDefaultQueryTimeout(defaultQueryTimeoutSeconds); } @Override