Repository: commons-dbcp Updated Branches: refs/heads/master 37daf894a -> 812121b8e
[DBCP-506] Support JDBC 4.2: new Statement 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/812121b8 Tree: http://git-wip-us.apache.org/repos/asf/commons-dbcp/tree/812121b8 Diff: http://git-wip-us.apache.org/repos/asf/commons-dbcp/diff/812121b8 Branch: refs/heads/master Commit: 812121b8e36f8588973c7ed4deb44838a5616d61 Parents: 37daf89 Author: Gary Gregory <garydgreg...@gmail.com> Authored: Sun Jun 17 09:00:10 2018 -0600 Committer: Gary Gregory <garydgreg...@gmail.com> Committed: Sun Jun 17 09:00:10 2018 -0600 ---------------------------------------------------------------------- .../commons/dbcp2/DelegatingStatement.java | 162 +++++++++++++++---- .../dbcp2/TestAbandonedBasicDataSource.java | 33 ++++ .../commons/dbcp2/TesterPreparedStatement.java | 34 +++- .../apache/commons/dbcp2/TesterStatement.java | 70 ++++++-- 4 files changed, 253 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/812121b8/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 00f440f..38980f6 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java @@ -170,9 +170,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { @Override public boolean execute(final String sql) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } + setLastUsedInParent(); try { return statement.execute(sql); } catch (final SQLException e) { @@ -184,9 +182,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { @Override public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } + setLastUsedInParent(); try { return statement.execute(sql, autoGeneratedKeys); } catch (final SQLException e) { @@ -198,9 +194,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { @Override public boolean execute(final String sql, final int columnIndexes[]) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } + setLastUsedInParent(); try { return statement.execute(sql, columnIndexes); } catch (final SQLException e) { @@ -212,9 +206,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { @Override public boolean execute(final String sql, final String columnNames[]) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } + setLastUsedInParent(); try { return statement.execute(sql, columnNames); } catch (final SQLException e) { @@ -226,9 +218,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { @Override public int[] executeBatch() throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } + setLastUsedInParent(); try { return statement.executeBatch(); } catch (final SQLException e) { @@ -237,12 +227,85 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } } + /** + * @since 2.5.0 + */ @Override - public ResultSet executeQuery(final String sql) throws SQLException { + public long[] executeLargeBatch() throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); + setLastUsedInParent(); + try { + return statement.executeLargeBatch(); + } catch (final SQLException e) { + handleException(e); + return null; } + } + + /** + * @since 2.5.0 + */ + @Override + public long executeLargeUpdate(final String sql) throws SQLException { + checkOpen(); + setLastUsedInParent(); + try { + return statement.executeLargeUpdate(sql); + } catch (final SQLException e) { + handleException(e); + return 0; + } + } + + /** + * @since 2.5.0 + */ + @Override + public long executeLargeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException { + checkOpen(); + setLastUsedInParent(); + try { + return statement.executeLargeUpdate(sql, autoGeneratedKeys); + } catch (final SQLException e) { + handleException(e); + return 0; + } + } + + /** + * @since 2.5.0 + */ + @Override + public long executeLargeUpdate(final String sql, final int[] columnIndexes) throws SQLException { + checkOpen(); + setLastUsedInParent(); + try { + return statement.executeLargeUpdate(sql, columnIndexes); + } catch (final SQLException e) { + handleException(e); + return 0; + } + } + + /** + * @since 2.5.0 + */ + @Override + public long executeLargeUpdate(final String sql, final String[] columnNames) throws SQLException { + checkOpen(); + setLastUsedInParent(); + try { + return statement.executeLargeUpdate(sql, columnNames); + } catch (final SQLException e) { + handleException(e); + return 0; + } + } + + @Override + public ResultSet executeQuery(final String sql) throws SQLException { + checkOpen(); + setLastUsedInParent(); try { return DelegatingResultSet.wrapResultSet(this, statement.executeQuery(sql)); } catch (final SQLException e) { @@ -254,9 +317,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { @Override public int executeUpdate(final String sql) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } + setLastUsedInParent(); try { return statement.executeUpdate(sql); } catch (final SQLException e) { @@ -268,9 +329,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { @Override public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } + setLastUsedInParent(); try { return statement.executeUpdate(sql, autoGeneratedKeys); } catch (final SQLException e) { @@ -282,9 +341,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { @Override public int executeUpdate(final String sql, final int columnIndexes[]) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } + setLastUsedInParent(); try { return statement.executeUpdate(sql, columnIndexes); } catch (final SQLException e) { @@ -296,9 +353,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { @Override public int executeUpdate(final String sql, final String columnNames[]) throws SQLException { checkOpen(); - if (connection != null) { - connection.setLastUsed(); - } + setLastUsedInParent(); try { return statement.executeUpdate(sql, columnNames); } catch (final SQLException e) { @@ -402,6 +457,34 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { return s; } + /** + * @since 2.5.0 + */ + @Override + public long getLargeMaxRows() throws SQLException { + checkOpen(); + try { + return statement.getLargeMaxRows(); + } catch (final SQLException e) { + handleException(e); + return 0; + } + } + + /** + * @since 2.5.0 + */ + @Override + public long getLargeUpdateCount() throws SQLException { + checkOpen(); + try { + return statement.getLargeUpdateCount(); + } catch (final SQLException e) { + handleException(e); + return 0; + } + } + @Override public int getMaxFieldSize() throws SQLException { checkOpen(); @@ -642,6 +725,25 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { } } + /** + * @since 2.5.0 + */ + @Override + public void setLargeMaxRows(final long max) throws SQLException { + checkOpen(); + try { + statement.setLargeMaxRows(max); + } catch (final SQLException e) { + handleException(e); + } + } + + private void setLastUsedInParent() { + if (connection != null) { + connection.setLastUsed(); + } + } + @Override public void setMaxFieldSize(final int max) throws SQLException { checkOpen(); http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/812121b8/src/test/java/org/apache/commons/dbcp2/TestAbandonedBasicDataSource.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbcp2/TestAbandonedBasicDataSource.java b/src/test/java/org/apache/commons/dbcp2/TestAbandonedBasicDataSource.java index 6f0eb9a..e5d8a5e 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestAbandonedBasicDataSource.java +++ b/src/test/java/org/apache/commons/dbcp2/TestAbandonedBasicDataSource.java @@ -207,6 +207,31 @@ public class TestAbandonedBasicDataSource extends TestBasicDataSource { } /** + * DBCP-343 - verify that using a DelegatingStatement updates + * the lastUsed on the parent connection + */ + @Test + public void testLastUsedLargePreparedStatementUse() throws Exception { + ds.setRemoveAbandonedTimeout(1); + ds.setMaxTotal(2); + try (Connection conn1 = ds.getConnection(); + Statement st = conn1.createStatement()) { + final String querySQL = "SELECT 1 FROM DUAL"; + Thread.sleep(500); + Assert.assertNotNull(st.executeQuery(querySQL)); // Should reset lastUsed + Thread.sleep(800); + final Connection conn2 = ds.getConnection(); // triggers abandoned cleanup + Assert.assertNotNull(st.executeQuery(querySQL)); // Should still be OK + conn2.close(); + Thread.sleep(500); + st.executeLargeUpdate(""); // Should also reset + Thread.sleep(800); + try (Connection c = ds.getConnection()) {} // trigger abandoned cleanup again + try (Statement s = conn1.createStatement()) {} // Connection should still be good + } + } + + /** * DBCP-343 - verify additional operations reset lastUsed on * the parent connection */ @@ -287,16 +312,24 @@ public class TestAbandonedBasicDataSource extends TestBasicDataSource { assertAndReset(conn); st.executeBatch(); assertAndReset(conn); + st.executeLargeBatch(); + assertAndReset(conn); Assert.assertNotNull(st.executeQuery("")); assertAndReset(conn); st.executeUpdate(""); assertAndReset(conn); st.executeUpdate("", new int[] {}); assertAndReset(conn); + st.executeLargeUpdate("", new int[] {}); + assertAndReset(conn); st.executeUpdate("", 0); assertAndReset(conn); + st.executeLargeUpdate("", 0); + assertAndReset(conn); st.executeUpdate("", new String[] {}); assertAndReset(conn); + st.executeLargeUpdate("", new String[] {}); + assertAndReset(conn); } /** http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/812121b8/src/test/java/org/apache/commons/dbcp2/TesterPreparedStatement.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbcp2/TesterPreparedStatement.java b/src/test/java/org/apache/commons/dbcp2/TesterPreparedStatement.java index 292ecb9..6aae73c 100644 --- a/src/test/java/org/apache/commons/dbcp2/TesterPreparedStatement.java +++ b/src/test/java/org/apache/commons/dbcp2/TesterPreparedStatement.java @@ -152,6 +152,36 @@ public class TesterPreparedStatement extends TesterStatement implements Prepared } @Override + public long executeLargeUpdate() throws SQLException { + checkOpen(); + return _rowsUpdated; + } + + @Override + public long executeLargeUpdate(final String sql) throws SQLException { + checkOpen(); + return _rowsUpdated; + } + + @Override + public long executeLargeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException { + checkOpen(); + return 0; + } + + @Override + public long executeLargeUpdate(final String sql, final int columnIndexes[]) throws SQLException { + checkOpen(); + return 0; + } + + @Override + public long executeLargeUpdate(final String sql, final String columnNames[]) throws SQLException { + checkOpen(); + return 0; + } + + @Override public ResultSet executeQuery() throws SQLException { checkOpen(); if("null".equals(_sql)) { @@ -176,13 +206,13 @@ public class TesterPreparedStatement extends TesterStatement implements Prepared @Override public int executeUpdate() throws SQLException { checkOpen(); - return _rowsUpdated; + return (int) _rowsUpdated; } @Override public int executeUpdate(final String sql) throws SQLException { checkOpen(); - return _rowsUpdated; + return (int) _rowsUpdated; } @Override http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/812121b8/src/test/java/org/apache/commons/dbcp2/TesterStatement.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbcp2/TesterStatement.java b/src/test/java/org/apache/commons/dbcp2/TesterStatement.java index c4b0a17..d5ce08c 100644 --- a/src/test/java/org/apache/commons/dbcp2/TesterStatement.java +++ b/src/test/java/org/apache/commons/dbcp2/TesterStatement.java @@ -31,11 +31,11 @@ public class TesterStatement implements Statement { protected boolean _open = true; - protected int _rowsUpdated = 1; + protected long _rowsUpdated = 1; protected boolean _executeResponse = true; protected int _maxFieldSize = 1024; - protected int _maxRows = 1024; + protected long _maxRows = 1024; protected boolean _escapeProcessing = false; protected int _queryTimeout = 1000; protected String _cursorName = null; @@ -45,7 +45,7 @@ public class TesterStatement implements Statement { protected int _resultSetType = 1; private int _resultSetHoldability = 1; protected ResultSet _resultSet = null; - + public TesterStatement(final Connection conn) { _connection = conn; } @@ -141,6 +141,33 @@ public class TesterStatement implements Statement { } @Override + public long[] executeLargeBatch() throws SQLException { + checkOpen(); + return new long[0]; + } + + @Override + public long executeLargeUpdate(final String sql) throws SQLException { + checkOpen(); + return _rowsUpdated; + } + + @Override + public long executeLargeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public long executeLargeUpdate(final String sql, final int[] columnIndexes) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override + public long executeLargeUpdate(final String sql, final String[] columnNames) throws SQLException { + throw new SQLException("Not implemented."); + } + + @Override public ResultSet executeQuery(final String sql) throws SQLException { checkOpen(); if("null".equals(sql)) { @@ -167,24 +194,21 @@ public class TesterStatement implements Statement { @Override public int executeUpdate(final String sql) throws SQLException { checkOpen(); - return _rowsUpdated; + return (int) _rowsUpdated; } @Override - public int executeUpdate(final String sql, final int autoGeneratedKeys) - throws SQLException { + public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException { throw new SQLException("Not implemented."); } @Override - public int executeUpdate(final String sql, final int columnIndexes[]) - throws SQLException { + public int executeUpdate(final String sql, final int columnIndexes[]) throws SQLException { throw new SQLException("Not implemented."); } @Override - public int executeUpdate(final String sql, final String columnNames[]) - throws SQLException { + public int executeUpdate(final String sql, final String columnNames[]) throws SQLException { throw new SQLException("Not implemented."); } @@ -212,6 +236,18 @@ public class TesterStatement implements Statement { } @Override + public long getLargeMaxRows() throws SQLException { + checkOpen(); + return _maxRows; + } + + @Override + public long getLargeUpdateCount() throws SQLException { + checkOpen(); + return _rowsUpdated; + } + + @Override public int getMaxFieldSize() throws SQLException { checkOpen(); return _maxFieldSize; @@ -220,7 +256,7 @@ public class TesterStatement implements Statement { @Override public int getMaxRows() throws SQLException { checkOpen(); - return _maxRows; + return (int) _maxRows; } @Override @@ -270,9 +306,10 @@ public class TesterStatement implements Statement { @Override public int getUpdateCount() throws SQLException { checkOpen(); - return _rowsUpdated; + return (int) _rowsUpdated; } + @Override public SQLWarning getWarnings() throws SQLException { checkOpen(); @@ -310,8 +347,7 @@ public class TesterStatement implements Statement { checkOpen(); _escapeProcessing = enable; } - - + @Override public void setFetchDirection(final int direction) throws SQLException { checkOpen(); @@ -325,6 +361,12 @@ public class TesterStatement implements Statement { } @Override + public void setLargeMaxRows(final long max) throws SQLException { + checkOpen(); + _maxRows = max; + } + + @Override public void setMaxFieldSize(final int max) throws SQLException { checkOpen(); _maxFieldSize = max;