diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 000000000..b9587f323 --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,34 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Java CI + +on: [push] + +jobs: + build: + strategy: + matrix: + java: [ '1.8', '9.0.4', '11.0.4', '12.0.2' ] + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - name: Set up Java ${{ matrix.java }} + uses: actions/setup-java@v1 + with: + java-version: ${{ matrix.java }} + - name: Build with Maven + run: mvn -V package --file pom.xml diff --git a/pom.xml b/pom.xml index 3b5536ca3..185e08639 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ 4.0.0 commons-dbcp2 - 2.7.1-SNAPSHOT + 2.8.0-SNAPSHOT Apache Commons DBCP 2001 @@ -293,7 +293,7 @@ RC1 org.apache.commons.dbcp2 - 2.7.0 + 2.8.0 for JDBC 4.2 on Java 8 sha512 @@ -331,7 +331,7 @@ javax.transaction;version="1.1.0",javax.transaction.xa;version="1.1.0";partial=true;mandatory:=partial,* true - 2.6.0 + 2.7.0 true Gary Gregory 86fdc7e2a11262cb diff --git a/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java b/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java index c53428b30..ec7c06e9c 100644 --- a/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java +++ b/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java @@ -32,7 +32,7 @@ * * @since 2.0 */ -public class AbandonedTrace implements TrackedUse { +public class AbandonedTrace extends ResourceFunctions implements TrackedUse { /** A list of objects created by children of this object. */ private final List> traceList = new ArrayList<>(); @@ -50,8 +50,7 @@ public AbandonedTrace() { /** * Constructs a new AbandonedTrace with a parent object. * - * @param parent - * AbandonedTrace parent object. + * @param parent AbandonedTrace parent object. */ public AbandonedTrace(final AbandonedTrace parent) { init(parent); @@ -60,8 +59,7 @@ public AbandonedTrace(final AbandonedTrace parent) { /** * Adds an object to the list of objects being traced. * - * @param trace - * AbandonedTrace object to add. + * @param trace AbandonedTrace object to add. */ protected void addTrace(final AbandonedTrace trace) { synchronized (this.traceList) { @@ -118,8 +116,7 @@ protected List getTrace() { /** * Initializes abandoned tracing for this object. * - * @param parent - * AbandonedTrace parent object. + * @param parent AbandonedTrace parent object. */ private void init(final AbandonedTrace parent) { if (parent != null) { @@ -142,8 +139,7 @@ protected void removeThisTrace(final Object source) { /** * Removes a child object this object is tracing. * - * @param trace - * AbandonedTrace object to remove. + * @param trace AbandonedTrace object to remove. */ protected void removeTrace(final AbandonedTrace trace) { synchronized (this.traceList) { @@ -171,8 +167,7 @@ protected void setLastUsed() { /** * Sets the time in milliseconds this object was last used. * - * @param lastUsedMillis - * time in milliseconds. + * @param lastUsedMillis time in milliseconds. */ protected void setLastUsed(final long lastUsedMillis) { this.lastUsedMillis = lastUsedMillis; diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingCallableStatement.java b/src/main/java/org/apache/commons/dbcp2/DelegatingCallableStatement.java index 2e36c2bbd..2ac88526e 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingCallableStatement.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingCallableStatement.java @@ -56,10 +56,8 @@ public class DelegatingCallableStatement extends DelegatingPreparedStatement imp * Creates a wrapper for the Statement which traces this Statement to the Connection which created it and the code * which created it. * - * @param connection - * the {@link DelegatingConnection} that created this statement - * @param statement - * the {@link CallableStatement} to delegate all calls to + * @param connection the {@link DelegatingConnection} that created this statement + * @param statement the {@link CallableStatement} to delegate all calls to */ public DelegatingCallableStatement(final DelegatingConnection connection, final CallableStatement statement) { super(connection, statement); @@ -67,235 +65,109 @@ public DelegatingCallableStatement(final DelegatingConnection connection, fin @Override public Array getArray(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getArray(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getArray, getDelegateCallableStatement(), parameterIndex); } @Override public Array getArray(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getArray(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getArray, getDelegateCallableStatement(), parameterName); } @Override public BigDecimal getBigDecimal(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getBigDecimal(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getBigDecimal, getDelegateCallableStatement(), parameterIndex); } /** @deprecated Use {@link #getBigDecimal(int)} or {@link #getBigDecimal(String)} */ @Override @Deprecated public BigDecimal getBigDecimal(final int parameterIndex, final int scale) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getBigDecimal(parameterIndex, scale); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getBigDecimal, getDelegateCallableStatement(), parameterIndex, scale); } @Override public BigDecimal getBigDecimal(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getBigDecimal(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getBigDecimal, getDelegateCallableStatement(), parameterName); } @Override public Blob getBlob(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getBlob(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getBlob, getDelegateCallableStatement(), parameterIndex); } @Override public Blob getBlob(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getBlob(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getBlob, getDelegateCallableStatement(), parameterName); } @Override public boolean getBoolean(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getBoolean(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(CallableStatement::getBoolean, getDelegateCallableStatement(), parameterIndex, false); } @Override public boolean getBoolean(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getBoolean(parameterName); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(CallableStatement::getBoolean, getDelegateCallableStatement(), parameterName, false); } @Override public byte getByte(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getByte(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getByte, getDelegateCallableStatement(), parameterIndex, (byte) 0); } @Override public byte getByte(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getByte(parameterName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getByte, getDelegateCallableStatement(), parameterName, (byte) 0); } @Override public byte[] getBytes(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getBytes(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getBytes, getDelegateCallableStatement(), parameterIndex); } @Override public byte[] getBytes(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getBytes(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getBytes, getDelegateCallableStatement(), parameterName); } @Override public Reader getCharacterStream(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getCharacterStream(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getCharacterStream, getDelegateCallableStatement(), parameterIndex); } @Override public Reader getCharacterStream(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getCharacterStream(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getCharacterStream, getDelegateCallableStatement(), parameterName); } @Override public Clob getClob(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getClob(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getClob, getDelegateCallableStatement(), parameterIndex); } @Override public Clob getClob(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getClob(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getClob, getDelegateCallableStatement(), parameterName); } @Override public Date getDate(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getDate(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getDate, getDelegateCallableStatement(), parameterIndex); } @Override public Date getDate(final int parameterIndex, final Calendar cal) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getDate(parameterIndex, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getDate, getDelegateCallableStatement(), parameterIndex, cal); } @Override public Date getDate(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getDate(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getDate, getDelegateCallableStatement(), parameterName); } @Override public Date getDate(final String parameterName, final Calendar cal) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getDate(parameterName, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getDate, getDelegateCallableStatement(), parameterName, cal); } private CallableStatement getDelegateCallableStatement() { @@ -304,167 +176,77 @@ private CallableStatement getDelegateCallableStatement() { @Override public double getDouble(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getDouble(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getDouble, getDelegateCallableStatement(), parameterIndex, 0d); } @Override public double getDouble(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getDouble(parameterName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getDouble, getDelegateCallableStatement(), parameterName, 0d); } @Override public float getFloat(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getFloat(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getFloat, getDelegateCallableStatement(), parameterIndex, 0f); } @Override public float getFloat(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getFloat(parameterName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getFloat, getDelegateCallableStatement(), parameterName, 0f); } @Override public int getInt(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getInt(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getInt, getDelegateCallableStatement(), parameterIndex, 0); } @Override public int getInt(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getInt(parameterName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getInt, getDelegateCallableStatement(), parameterName, 0); } @Override public long getLong(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getLong(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getLong, getDelegateCallableStatement(), parameterIndex, 0L); } @Override public long getLong(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getLong(parameterName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getLong, getDelegateCallableStatement(), parameterName, 0L); } @Override public Reader getNCharacterStream(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getNCharacterStream(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getNCharacterStream, getDelegateCallableStatement(), parameterIndex); } @Override public Reader getNCharacterStream(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getNCharacterStream(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getNCharacterStream, getDelegateCallableStatement(), parameterName); } @Override public NClob getNClob(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getNClob(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getNClob, getDelegateCallableStatement(), parameterIndex); } @Override public NClob getNClob(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getNClob(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getNClob, getDelegateCallableStatement(), parameterName); } @Override public String getNString(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getNString(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getNString, getDelegateCallableStatement(), parameterIndex); } @Override public String getNString(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getNString(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getNString, getDelegateCallableStatement(), parameterName); } @Override public Object getObject(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getObject(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getObject, getDelegateCallableStatement(), parameterIndex); } @Override @@ -479,25 +261,13 @@ public T getObject(final int parameterIndex, final Class type) throws SQL } @Override - public Object getObject(final int i, final Map> map) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getObject(i, map); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Object getObject(final int parameterIndex, final Map> map) throws SQLException { + return apply(CallableStatement::getObject, getDelegateCallableStatement(), parameterIndex, map); } @Override public Object getObject(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getObject(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getObject, getDelegateCallableStatement(), parameterName); } @Override @@ -524,253 +294,119 @@ public Object getObject(final String parameterName, final Map> @Override public Ref getRef(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getRef(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getRef, getDelegateCallableStatement(), parameterIndex); } @Override public Ref getRef(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getRef(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getRef, getDelegateCallableStatement(), parameterName); } @Override public RowId getRowId(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getRowId(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getRowId, getDelegateCallableStatement(), parameterIndex); } @Override public RowId getRowId(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getRowId(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getRowId, getDelegateCallableStatement(), parameterName); } @Override public short getShort(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getShort(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getShort, getDelegateCallableStatement(), parameterIndex, (short) 0); } @Override public short getShort(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getShort(parameterName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(CallableStatement::getShort, getDelegateCallableStatement(), parameterName, (short) 0); } @Override public SQLXML getSQLXML(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getSQLXML(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getSQLXML, getDelegateCallableStatement(), parameterIndex); } @Override public SQLXML getSQLXML(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getSQLXML(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getSQLXML, getDelegateCallableStatement(), parameterName); } @Override public String getString(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getString(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getString, getDelegateCallableStatement(), parameterIndex); } @Override public String getString(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getString(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getString, getDelegateCallableStatement(), parameterName); } @Override public Time getTime(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getTime(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getTime, getDelegateCallableStatement(), parameterIndex); } @Override public Time getTime(final int parameterIndex, final Calendar cal) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getTime(parameterIndex, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getTime, getDelegateCallableStatement(), parameterIndex, cal); } @Override public Time getTime(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getTime(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getTime, getDelegateCallableStatement(), parameterName); } @Override public Time getTime(final String parameterName, final Calendar cal) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getTime(parameterName, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getTime, getDelegateCallableStatement(), parameterName, cal); } @Override public Timestamp getTimestamp(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getTimestamp(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getTimestamp, getDelegateCallableStatement(), parameterIndex); } @Override public Timestamp getTimestamp(final int parameterIndex, final Calendar cal) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getTimestamp(parameterIndex, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getTimestamp, getDelegateCallableStatement(), parameterIndex, cal); } @Override public Timestamp getTimestamp(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getTimestamp(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getTimestamp, getDelegateCallableStatement(), parameterName); } @Override public Timestamp getTimestamp(final String parameterName, final Calendar cal) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getTimestamp(parameterName, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getTimestamp, getDelegateCallableStatement(), parameterName, cal); } @Override public URL getURL(final int parameterIndex) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getURL(parameterIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getURL, getDelegateCallableStatement(), parameterIndex); } @Override public URL getURL(final String parameterName) throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().getURL(parameterName); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(CallableStatement::getURL, getDelegateCallableStatement(), parameterName); } @Override public void registerOutParameter(final int parameterIndex, final int sqlType) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterIndex, sqlType); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterIndex, sqlType); } @Override public void registerOutParameter(final int parameterIndex, final int sqlType, final int scale) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterIndex, sqlType, scale); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterIndex, sqlType, scale); } @Override - public void registerOutParameter(final int paramIndex, final int sqlType, final String typeName) + public void registerOutParameter(final int parameterIndex, final int sqlType, final String typeName) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(paramIndex, sqlType, typeName); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterIndex, sqlType, + typeName); } /** @@ -778,12 +414,7 @@ public void registerOutParameter(final int paramIndex, final int sqlType, final */ @Override public void registerOutParameter(final int parameterIndex, final SQLType sqlType) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterIndex, sqlType); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterIndex, sqlType); } /** @@ -792,12 +423,7 @@ public void registerOutParameter(final int parameterIndex, final SQLType sqlType @Override public void registerOutParameter(final int parameterIndex, final SQLType sqlType, final int scale) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterIndex, sqlType, scale); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterIndex, sqlType, scale); } /** @@ -806,44 +432,26 @@ public void registerOutParameter(final int parameterIndex, final SQLType sqlType @Override public void registerOutParameter(final int parameterIndex, final SQLType sqlType, final String typeName) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterIndex, sqlType, typeName); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterIndex, sqlType, + typeName); } @Override public void registerOutParameter(final String parameterName, final int sqlType) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterName, sqlType); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterName, sqlType); } @Override public void registerOutParameter(final String parameterName, final int sqlType, final int scale) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterName, sqlType, scale); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterName, sqlType, scale); } @Override public void registerOutParameter(final String parameterName, final int sqlType, final String typeName) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterName, sqlType, typeName); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterName, sqlType, + typeName); } /** @@ -851,12 +459,7 @@ public void registerOutParameter(final String parameterName, final int sqlType, */ @Override public void registerOutParameter(final String parameterName, final SQLType sqlType) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterName, sqlType); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterName, sqlType); } /** @@ -865,12 +468,7 @@ public void registerOutParameter(final String parameterName, final SQLType sqlTy @Override public void registerOutParameter(final String parameterName, final SQLType sqlType, final int scale) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterName, sqlType, scale); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterName, sqlType, scale); } /** @@ -879,503 +477,267 @@ public void registerOutParameter(final String parameterName, final SQLType sqlTy @Override public void registerOutParameter(final String parameterName, final SQLType sqlType, final String typeName) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().registerOutParameter(parameterName, sqlType, typeName); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::registerOutParameter, getDelegateCallableStatement(), parameterName, sqlType, + typeName); } @Override - public void setAsciiStream(final String parameterName, final InputStream inputStream) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setAsciiStream(parameterName, inputStream); - } catch (final SQLException e) { - handleException(e); - } + public void setAsciiStream(final String parameterName, final InputStream value) throws SQLException { + accept(CallableStatement::setAsciiStream, getDelegateCallableStatement(), parameterName, value); } @Override - public void setAsciiStream(final String parameterName, final InputStream x, final int length) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setAsciiStream(parameterName, x, length); - } catch (final SQLException e) { - handleException(e); - } + public void setAsciiStream(final String parameterName, final InputStream value, final int length) + throws SQLException { + accept(CallableStatement::setAsciiStream, getDelegateCallableStatement(), parameterName, value, length); } @Override - public void setAsciiStream(final String parameterName, final InputStream inputStream, final long length) + public void setAsciiStream(final String parameterName, final InputStream value, final long length) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setAsciiStream(parameterName, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setAsciiStream, getDelegateCallableStatement(), parameterName, value, length); } @Override - public void setBigDecimal(final String parameterName, final BigDecimal x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setBigDecimal(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setBigDecimal(final String parameterName, final BigDecimal value) throws SQLException { + accept(CallableStatement::setBigDecimal, getDelegateCallableStatement(), parameterName, value); } @Override - public void setBinaryStream(final String parameterName, final InputStream inputStream) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setBinaryStream(parameterName, inputStream); - } catch (final SQLException e) { - handleException(e); - } + public void setBinaryStream(final String parameterName, final InputStream value) throws SQLException { + accept(CallableStatement::setBinaryStream, getDelegateCallableStatement(), parameterName, value); } @Override - public void setBinaryStream(final String parameterName, final InputStream x, final int length) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setBinaryStream(parameterName, x, length); - } catch (final SQLException e) { - handleException(e); - } + public void setBinaryStream(final String parameterName, final InputStream value, final int length) + throws SQLException { + accept(CallableStatement::setBinaryStream, getDelegateCallableStatement(), parameterName, value, length); } @Override - public void setBinaryStream(final String parameterName, final InputStream inputStream, final long length) + public void setBinaryStream(final String parameterName, final InputStream value, final long length) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setBinaryStream(parameterName, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setBinaryStream, getDelegateCallableStatement(), parameterName, value, length); } @Override - public void setBlob(final String parameterName, final Blob blob) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setBlob(parameterName, blob); - } catch (final SQLException e) { - handleException(e); - } + public void setBlob(final String parameterName, final Blob value) throws SQLException { + accept(CallableStatement::setBlob, getDelegateCallableStatement(), parameterName, value); } @Override - public void setBlob(final String parameterName, final InputStream inputStream) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setBlob(parameterName, inputStream); - } catch (final SQLException e) { - handleException(e); - } + public void setBlob(final String parameterName, final InputStream value) throws SQLException { + accept(CallableStatement::setBlob, getDelegateCallableStatement(), parameterName, value); } @Override - public void setBlob(final String parameterName, final InputStream inputStream, final long length) - throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setBlob(parameterName, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + public void setBlob(final String parameterName, final InputStream value, final long length) throws SQLException { + accept(CallableStatement::setBlob, getDelegateCallableStatement(), parameterName, value, length); } @Override - public void setBoolean(final String parameterName, final boolean x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setBoolean(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setBoolean(final String parameterName, final boolean value) throws SQLException { + accept(CallableStatement::setBoolean, getDelegateCallableStatement(), parameterName, value); } @Override - public void setByte(final String parameterName, final byte x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setByte(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setByte(final String parameterName, final byte value) throws SQLException { + accept(CallableStatement::setByte, getDelegateCallableStatement(), parameterName, value); } @Override - public void setBytes(final String parameterName, final byte[] x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setBytes(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setBytes(final String parameterName, final byte[] value) throws SQLException { + accept(CallableStatement::setBytes, getDelegateCallableStatement(), parameterName, value); } @Override - public void setCharacterStream(final String parameterName, final Reader reader) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setCharacterStream(parameterName, reader); - } catch (final SQLException e) { - handleException(e); - } + public void setCharacterStream(final String parameterName, final Reader value) throws SQLException { + accept(CallableStatement::setCharacterStream, getDelegateCallableStatement(), parameterName, value); } @Override - public void setCharacterStream(final String parameterName, final Reader reader, final int length) + public void setCharacterStream(final String parameterName, final Reader value, final int length) throws SQLException { - checkOpen(); - getDelegateCallableStatement().setCharacterStream(parameterName, reader, length); + accept(CallableStatement::setCharacterStream, getDelegateCallableStatement(), parameterName, value, length); } @Override - public void setCharacterStream(final String parameterName, final Reader reader, final long length) + public void setCharacterStream(final String parameterName, final Reader value, final long length) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setCharacterStream(parameterName, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setCharacterStream, getDelegateCallableStatement(), parameterName, value, length); } @Override - public void setClob(final String parameterName, final Clob clob) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setClob(parameterName, clob); - } catch (final SQLException e) { - handleException(e); - } + public void setClob(final String parameterName, final Clob value) throws SQLException { + accept(CallableStatement::setClob, getDelegateCallableStatement(), parameterName, value); } @Override - public void setClob(final String parameterName, final Reader reader) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setClob(parameterName, reader); - } catch (final SQLException e) { - handleException(e); - } + public void setClob(final String parameterName, final Reader value) throws SQLException { + accept(CallableStatement::setClob, getDelegateCallableStatement(), parameterName, value); } @Override - public void setClob(final String parameterName, final Reader reader, final long length) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setClob(parameterName, reader, length); - } catch (final SQLException e) { - handleException(e); - } + public void setClob(final String parameterName, final Reader value, final long length) throws SQLException { + accept(CallableStatement::setClob, getDelegateCallableStatement(), parameterName, value, length); } @Override - public void setDate(final String parameterName, final Date x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setDate(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setDate(final String parameterName, final Date value) throws SQLException { + accept(CallableStatement::setDate, getDelegateCallableStatement(), parameterName, value); } @Override - public void setDate(final String parameterName, final Date x, final Calendar cal) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setDate(parameterName, x, cal); - } catch (final SQLException e) { - handleException(e); - } + public void setDate(final String parameterName, final Date value, final Calendar cal) throws SQLException { + accept(CallableStatement::setDate, getDelegateCallableStatement(), parameterName, value, cal); } @Override - public void setDouble(final String parameterName, final double x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setDouble(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setDouble(final String parameterName, final double value) throws SQLException { + accept(CallableStatement::setDouble, getDelegateCallableStatement(), parameterName, value); } @Override - public void setFloat(final String parameterName, final float x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setFloat(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setFloat(final String parameterName, final float value) throws SQLException { + accept(CallableStatement::setFloat, getDelegateCallableStatement(), parameterName, value); } @Override - public void setInt(final String parameterName, final int x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setInt(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setInt(final String parameterName, final int value) throws SQLException { + accept(CallableStatement::setInt, getDelegateCallableStatement(), parameterName, value); } @Override - public void setLong(final String parameterName, final long x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setLong(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setLong(final String parameterName, final long value) throws SQLException { + accept(CallableStatement::setLong, getDelegateCallableStatement(), parameterName, value); } @Override - public void setNCharacterStream(final String parameterName, final Reader reader) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setNCharacterStream(parameterName, reader); - } catch (final SQLException e) { - handleException(e); - } + public void setNCharacterStream(final String parameterName, final Reader value) throws SQLException { + accept(CallableStatement::setNCharacterStream, getDelegateCallableStatement(), parameterName, value); } @Override - public void setNCharacterStream(final String parameterName, final Reader reader, final long length) + public void setNCharacterStream(final String parameterName, final Reader value, final long length) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setNCharacterStream(parameterName, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setNCharacterStream, getDelegateCallableStatement(), parameterName, value, length); } @Override public void setNClob(final String parameterName, final NClob value) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setNClob(parameterName, value); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setNClob, getDelegateCallableStatement(), parameterName, value); } @Override - public void setNClob(final String parameterName, final Reader reader) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setNClob(parameterName, reader); - } catch (final SQLException e) { - handleException(e); - } + public void setNClob(final String parameterName, final Reader value) throws SQLException { + accept(CallableStatement::setNClob, getDelegateCallableStatement(), parameterName, value); } @Override - public void setNClob(final String parameterName, final Reader reader, final long length) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setNClob(parameterName, reader, length); - } catch (final SQLException e) { - handleException(e); - } + public void setNClob(final String parameterName, final Reader value, final long length) throws SQLException { + accept(CallableStatement::setNClob, getDelegateCallableStatement(), parameterName, value, length); } @Override public void setNString(final String parameterName, final String value) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setNString(parameterName, value); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setNString, getDelegateCallableStatement(), parameterName, value); } @Override public void setNull(final String parameterName, final int sqlType) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setNull(parameterName, sqlType); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setNull, getDelegateCallableStatement(), parameterName, sqlType); } @Override public void setNull(final String parameterName, final int sqlType, final String typeName) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setNull(parameterName, sqlType, typeName); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setNull, getDelegateCallableStatement(), parameterName, sqlType, typeName); } @Override - public void setObject(final String parameterName, final Object x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setObject(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setObject(final String parameterName, final Object value) throws SQLException { + accept(CallableStatement::setObject, getDelegateCallableStatement(), parameterName, value); } @Override - public void setObject(final String parameterName, final Object x, final int targetSqlType) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setObject(parameterName, x, targetSqlType); - } catch (final SQLException e) { - handleException(e); - } + public void setObject(final String parameterName, final Object value, final int targetSqlType) throws SQLException { + accept(CallableStatement::setObject, getDelegateCallableStatement(), parameterName, value, targetSqlType); } @Override - public void setObject(final String parameterName, final Object x, final int targetSqlType, final int scale) + public void setObject(final String parameterName, final Object value, final int targetSqlType, final int scale) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setObject(parameterName, x, targetSqlType, scale); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setObject, getDelegateCallableStatement(), parameterName, value, targetSqlType, + scale); } /** * @since 2.5.0 */ @Override - public void setObject(final String parameterName, final Object x, final SQLType targetSqlType) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setObject(parameterName, x, targetSqlType); - } catch (final SQLException e) { - handleException(e); - } + public void setObject(final String parameterName, final Object value, final SQLType targetSqlType) + throws SQLException { + accept(CallableStatement::setObject, getDelegateCallableStatement(), parameterName, value, targetSqlType); } /** * @since 2.5.0 */ @Override - public void setObject(final String parameterName, final Object x, final SQLType targetSqlType, + public void setObject(final String parameterName, final Object value, final SQLType targetSqlType, final int scaleOrLength) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setObject(parameterName, x, targetSqlType, scaleOrLength); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setObject, getDelegateCallableStatement(), parameterName, value, targetSqlType, + scaleOrLength); } @Override public void setRowId(final String parameterName, final RowId value) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setRowId(parameterName, value); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setRowId, getDelegateCallableStatement(), parameterName, value); } @Override - public void setShort(final String parameterName, final short x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setShort(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setShort(final String parameterName, final short value) throws SQLException { + accept(CallableStatement::setShort, getDelegateCallableStatement(), parameterName, value); } @Override public void setSQLXML(final String parameterName, final SQLXML value) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setSQLXML(parameterName, value); - } catch (final SQLException e) { - handleException(e); - } + accept(CallableStatement::setSQLXML, getDelegateCallableStatement(), parameterName, value); } @Override - public void setString(final String parameterName, final String x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setString(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setString(final String parameterName, final String value) throws SQLException { + accept(CallableStatement::setString, getDelegateCallableStatement(), parameterName, value); } @Override - public void setTime(final String parameterName, final Time x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setTime(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setTime(final String parameterName, final Time value) throws SQLException { + accept(CallableStatement::setTime, getDelegateCallableStatement(), parameterName, value); } @Override - public void setTime(final String parameterName, final Time x, final Calendar cal) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setTime(parameterName, x, cal); - } catch (final SQLException e) { - handleException(e); - } + public void setTime(final String parameterName, final Time value, final Calendar cal) throws SQLException { + accept(CallableStatement::setTime, getDelegateCallableStatement(), parameterName, value, cal); } @Override - public void setTimestamp(final String parameterName, final Timestamp x) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setTimestamp(parameterName, x); - } catch (final SQLException e) { - handleException(e); - } + public void setTimestamp(final String parameterName, final Timestamp value) throws SQLException { + accept(CallableStatement::setTimestamp, getDelegateCallableStatement(), parameterName, value); } @Override - public void setTimestamp(final String parameterName, final Timestamp x, final Calendar cal) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setTimestamp(parameterName, x, cal); - } catch (final SQLException e) { - handleException(e); - } + public void setTimestamp(final String parameterName, final Timestamp value, final Calendar cal) + throws SQLException { + accept(CallableStatement::setTimestamp, getDelegateCallableStatement(), parameterName, value, cal); } @Override - public void setURL(final String parameterName, final URL val) throws SQLException { - checkOpen(); - try { - getDelegateCallableStatement().setURL(parameterName, val); - } catch (final SQLException e) { - handleException(e); - } + public void setURL(final String parameterName, final URL value) throws SQLException { + accept(CallableStatement::setURL, getDelegateCallableStatement(), parameterName, value); } @Override public boolean wasNull() throws SQLException { - checkOpen(); - try { - return getDelegateCallableStatement().wasNull(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return apply(CallableStatement::wasNull, getDelegateCallableStatement()); } } diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java b/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java index f005b21aa..457cce76e 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java @@ -54,21 +54,18 @@ * connections is nearing exhaustion and this connection's last usage is older than the removeAbandonedTimeout. *

* - * @param - * the Connection type + * @param the Connection type * * @since 2.0 */ public class DelegatingConnection extends AbandonedTrace implements Connection { private static final Map EMPTY_FAILED_PROPERTIES = Collections - .emptyMap(); + .emptyMap(); /** My delegate {@link Connection}. */ private volatile C connection; - private volatile boolean closed; - private boolean cacheState = true; private Boolean autoCommitCached; private Boolean readOnlyCached; @@ -77,121 +74,57 @@ public class DelegatingConnection 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 c the {@link Connection} to delegate all calls to. */ public DelegatingConnection(final C c) { super(); connection = c; } - /** - * Returns a string representation of the metadata associated with the innermost delegate connection. - */ - @SuppressWarnings("resource") @Override - public synchronized String toString() { - String str = null; - - final Connection conn = this.getInnermostDelegateInternal(); - if (conn != null) { - try { - if (conn.isClosed()) { - str = "connection is closed"; - } else { - final StringBuffer sb = new StringBuffer(); - sb.append(hashCode()); - final DatabaseMetaData meta = conn.getMetaData(); - if (meta != null) { - sb.append(", URL="); - sb.append(meta.getURL()); - sb.append(", UserName="); - sb.append(meta.getUserName()); - sb.append(", "); - sb.append(meta.getDriverName()); - str = sb.toString(); - } - } - } catch (final SQLException ex) { - // Ignore - } - } - return str != null ? str : super.toString(); - } - - /** - * Returns my underlying {@link Connection}. - * - * @return my underlying {@link Connection}. - */ - public C getDelegate() { - return getDelegateInternal(); - } - - protected final C getDelegateInternal() { - return connection; + public void abort(final Executor executor) throws SQLException { + accept(Jdbc41Bridge::abort, connection, executor); } - /** - * Compares innermost delegate to the given connection. - * - * @param c - * connection to compare innermost delegate with - * @return true if innermost delegate equals c - */ - @SuppressWarnings("resource") - public boolean innermostDelegateEquals(final Connection c) { - final Connection innerCon = getInnermostDelegateInternal(); - if (innerCon == null) { - return c == null; + protected void activate() { + closed = false; + setLastUsed(); + if (connection instanceof DelegatingConnection) { + ((DelegatingConnection) connection).activate(); } - return innerCon.equals(c); } - /** - * If my underlying {@link Connection} is not a {@code DelegatingConnection}, returns it, otherwise recursively - * invokes this method on my delegate. - *

- * Hence this method will return the first delegate that is not a {@code DelegatingConnection}, or {@code null} when - * no non-{@code DelegatingConnection} delegate can be found by traversing this chain. - *

- *

- * This method is useful when you may have nested {@code DelegatingConnection}s, and you want to make sure to obtain - * a "genuine" {@link Connection}. - *

- * - * @return innermost delegate. - */ - public Connection getInnermostDelegate() { - return getInnermostDelegateInternal(); + @Override + protected void checkOpen() throws SQLException { + if (closed) { + if (null != connection) { + String label = ""; + try { + label = connection.toString(); + } catch (final Exception ex) { + // ignore, leave label empty + } + throw new SQLException("Connection " + label + " is closed."); + } + throw new SQLException("Connection is null."); + } } /** - * Although this method is public, it is part of the internal API and should not be used by clients. The signature - * of this method may change at any time including in ways that break backwards compatibility. - * - * @return innermost delegate. + * Can be used to clear cached state when it is known that the underlying connection may have been accessed + * directly. */ - @SuppressWarnings("resource") - public final Connection getInnermostDelegateInternal() { - Connection conn = connection; - while (conn != null && conn instanceof DelegatingConnection) { - conn = ((DelegatingConnection) conn).getDelegateInternal(); - if (this == conn) { - return null; - } + public void clearCachedState() { + autoCommitCached = null; + readOnlyCached = null; + if (connection instanceof DelegatingConnection) { + ((DelegatingConnection) connection).clearCachedState(); } - return conn; } - /** - * Sets my delegate. - * - * @param connection - * my delegate. - */ - public void setDelegate(final C connection) { - this.connection = connection; + @Override + public void clearWarnings() throws SQLException { + accept(connection::clearWarnings); } /** @@ -210,14 +143,6 @@ public void close() throws SQLException { } } - protected boolean isClosedInternal() { - return closed; - } - - protected void setClosedInternal(final boolean closed) { - this.closed = closed; - } - protected final void closeInternal() throws SQLException { try { passivate(); @@ -246,129 +171,71 @@ protected final void closeInternal() throws SQLException { } } - protected void handleException(final SQLException e) throws SQLException { - throw e; + @Override + public void commit() throws SQLException { + accept(connection::commit); } - /** - * Handles the given {@code SQLException}. - * - * @param The throwable type. - * @param e The SQLException - * @return the given {@code SQLException} - * @since 2.7.0 - */ - protected T handleExceptionNoThrow(final T e) { - return e; + @Override + public Array createArrayOf(final String typeName, final Object[] elements) throws SQLException { + return apply(connection::createArrayOf, typeName, elements); } - private void initializeStatement(final DelegatingStatement ds) throws SQLException { - if (defaultQueryTimeoutSeconds != null && defaultQueryTimeoutSeconds.intValue() != ds.getQueryTimeout()) { - ds.setQueryTimeout(defaultQueryTimeoutSeconds.intValue()); - } + @Override + public Blob createBlob() throws SQLException { + return apply(connection::createBlob); } @Override - public Statement createStatement() throws SQLException { - checkOpen(); - try { - final DelegatingStatement ds = new DelegatingStatement(this, connection.createStatement()); - initializeStatement(ds); - return ds; - } catch (final SQLException e) { - handleException(e); - return null; - } + public Clob createClob() throws SQLException { + return apply(connection::createClob); } @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; - } catch (final SQLException e) { - handleException(e); - return null; - } + public NClob createNClob() throws SQLException { + return apply(connection::createNClob); } @Override - public PreparedStatement prepareStatement(final String sql) throws SQLException { - checkOpen(); - try { - final DelegatingPreparedStatement dps = new DelegatingPreparedStatement(this, - connection.prepareStatement(sql)); - initializeStatement(dps); - return dps; - } catch (final SQLException e) { - handleException(e); - return null; - } + public SQLXML createSQLXML() throws SQLException { + return apply(connection::createSQLXML); } @Override - public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) - throws SQLException { - checkOpen(); - try { - final DelegatingPreparedStatement dps = new DelegatingPreparedStatement(this, - connection.prepareStatement(sql, resultSetType, resultSetConcurrency)); - initializeStatement(dps); - return dps; - } catch (final SQLException e) { - handleException(e); - return null; - } + public Statement createStatement() throws SQLException { + return apply(() -> init(new DelegatingStatement(this, connection.createStatement()))); } @Override - public CallableStatement prepareCall(final String sql) throws SQLException { - checkOpen(); - try { - final DelegatingCallableStatement dcs = new DelegatingCallableStatement(this, connection.prepareCall(sql)); - initializeStatement(dcs); - return dcs; - } catch (final SQLException e) { - handleException(e); - return null; - } + public Statement createStatement(final int resultSetType, final int resultSetConcurrency) throws SQLException { + return apply( + () -> init(new DelegatingStatement(this, connection.createStatement(resultSetType, resultSetConcurrency)))); } @Override - public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) - throws SQLException { - checkOpen(); - try { - final DelegatingCallableStatement dcs = new DelegatingCallableStatement(this, - connection.prepareCall(sql, resultSetType, resultSetConcurrency)); - initializeStatement(dcs); - return dcs; - } catch (final SQLException e) { - handleException(e); - return null; - } + public Statement createStatement(final int resultSetType, final int resultSetConcurrency, + final int resultSetHoldability) throws SQLException { + return apply(() -> init(new DelegatingStatement(this, + connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability)))); } @Override - public void clearWarnings() throws SQLException { - checkOpen(); - try { - connection.clearWarnings(); - } catch (final SQLException e) { - handleException(e); - } + public Struct createStruct(final String typeName, final Object[] attributes) throws SQLException { + return apply(connection::createStruct, typeName, attributes); } @Override - public void commit() throws SQLException { + public boolean getAutoCommit() throws SQLException { checkOpen(); + if (cacheState && autoCommitCached != null) { + return autoCommitCached.booleanValue(); + } try { - connection.commit(); + autoCommitCached = Boolean.valueOf(connection.getAutoCommit()); + return autoCommitCached.booleanValue(); } catch (final SQLException e) { handleException(e); + return false; } } @@ -381,239 +248,205 @@ public boolean getCacheState() { return cacheState; } - @Override - public boolean getAutoCommit() throws SQLException { - checkOpen(); - if (cacheState && autoCommitCached != null) { - return autoCommitCached.booleanValue(); - } - try { - autoCommitCached = Boolean.valueOf(connection.getAutoCommit()); - return autoCommitCached.booleanValue(); - } catch (final SQLException e) { - handleException(e); - return false; - } - } - @Override public String getCatalog() throws SQLException { - checkOpen(); - try { - return connection.getCatalog(); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(connection::getCatalog); } @Override - public DatabaseMetaData getMetaData() throws SQLException { - checkOpen(); - try { - return new DelegatingDatabaseMetaData(this, connection.getMetaData()); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Properties getClientInfo() throws SQLException { + return apply(connection::getClientInfo); } @Override - public int getTransactionIsolation() throws SQLException { - checkOpen(); - try { - return connection.getTransactionIsolation(); - } catch (final SQLException e) { - handleException(e); - return -1; - } + public String getClientInfo(final String name) throws SQLException { + return apply(connection::getClientInfo, name); } - @Override - public Map> getTypeMap() throws SQLException { - checkOpen(); - try { - return connection.getTypeMap(); - } catch (final SQLException e) { - handleException(e); - return null; - } + /** + * Gets the default query timeout that will be used for {@link Statement}s created from this connection. + * null means that the driver default will be used. + * + * @return query timeout limit in seconds; zero means there is no limit. + */ + public Integer getDefaultQueryTimeout() { + return defaultQueryTimeoutSeconds; } - @Override - public SQLWarning getWarnings() throws SQLException { - checkOpen(); - try { - return connection.getWarnings(); - } catch (final SQLException e) { - handleException(e); - return null; - } - } - - @Override - public boolean isReadOnly() throws SQLException { - checkOpen(); - if (cacheState && readOnlyCached != null) { - return readOnlyCached.booleanValue(); - } - try { - readOnlyCached = Boolean.valueOf(connection.isReadOnly()); - return readOnlyCached.booleanValue(); - } catch (final SQLException e) { - handleException(e); - return false; - } + /** + * Returns my underlying {@link Connection}. + * + * @return my underlying {@link Connection}. + */ + public C getDelegate() { + return getDelegateInternal(); } - @Override - public String nativeSQL(final String sql) throws SQLException { - checkOpen(); - try { - return connection.nativeSQL(sql); - } catch (final SQLException e) { - handleException(e); - return null; - } + protected final C getDelegateInternal() { + return connection; } @Override - public void rollback() throws SQLException { - checkOpen(); - try { - connection.rollback(); - } catch (final SQLException e) { - handleException(e); - } + public int getHoldability() throws SQLException { + return applyTo0(connection::getHoldability); } /** - * Gets the default query timeout that will be used for {@link Statement}s created from this connection. - * null means that the driver default will be used. + * If my underlying {@link Connection} is not a {@code DelegatingConnection}, returns it, otherwise recursively + * invokes this method on my delegate. + *

+ * Hence this method will return the first delegate that is not a {@code DelegatingConnection}, or {@code null} when + * no non-{@code DelegatingConnection} delegate can be found by traversing this chain. + *

+ *

+ * This method is useful when you may have nested {@code DelegatingConnection}s, and you want to make sure to obtain + * a "genuine" {@link Connection}. + *

* - * @return query timeout limit in seconds; zero means there is no limit. + * @return innermost delegate. */ - public Integer getDefaultQueryTimeout() { - return defaultQueryTimeoutSeconds; + public Connection getInnermostDelegate() { + return getInnermostDelegateInternal(); } /** - * Sets the default query timeout that will be used for {@link Statement}s created from this connection. - * null means that the driver default will be used. + * Although this method is public, it is part of the internal API and should not be used by clients. The signature + * of this method may change at any time including in ways that break backwards compatibility. * - * @param defaultQueryTimeoutSeconds - * the new query timeout limit in seconds; zero means there is no limit + * @return innermost delegate. */ - public void setDefaultQueryTimeout(final Integer defaultQueryTimeoutSeconds) { - this.defaultQueryTimeoutSeconds = defaultQueryTimeoutSeconds; + @SuppressWarnings("resource") + public final Connection getInnermostDelegateInternal() { + Connection conn = connection; + while (conn != null && conn instanceof DelegatingConnection) { + conn = ((DelegatingConnection) conn).getDelegateInternal(); + if (this == conn) { + return null; + } + } + return conn; + } + + @Override + public DatabaseMetaData getMetaData() throws SQLException { + return apply(() -> new DelegatingDatabaseMetaData(this, connection.getMetaData())); + } + + @Override + public int getNetworkTimeout() throws SQLException { + return apply(Jdbc41Bridge::getNetworkTimeout, connection); + } + + @Override + public String getSchema() throws SQLException { + return apply(Jdbc41Bridge::getSchema, connection); + } + + @Override + public int getTransactionIsolation() throws SQLException { + return applyTo0(connection::getTransactionIsolation); + } + + @Override + public Map> getTypeMap() throws SQLException { + return apply(connection::getTypeMap); + } + + @Override + public SQLWarning getWarnings() throws SQLException { + return apply(connection::getWarnings); + } + + @Override + protected void handleException(final SQLException e) throws SQLException { + throw e; } /** - * Sets the state caching flag. + * Handles the given {@code SQLException}. * - * @param cacheState - * The new value for the state caching flag + * @param The throwable type. + * @param e The SQLException + * @return the given {@code SQLException} + * @since 2.7.0 */ - public void setCacheState(final boolean cacheState) { - this.cacheState = cacheState; + protected T handleExceptionNoThrow(final T e) { + return e; } - /** - * Can be used to clear cached state when it is known that the underlying connection may have been accessed - * directly. - */ - public void clearCachedState() { - autoCommitCached = null; - readOnlyCached = null; - if (connection instanceof DelegatingConnection) { - ((DelegatingConnection) connection).clearCachedState(); + private T init(final T ds) throws SQLException { + if (defaultQueryTimeoutSeconds != null && defaultQueryTimeoutSeconds.intValue() != ds.getQueryTimeout()) { + ds.setQueryTimeout(defaultQueryTimeoutSeconds.intValue()); } + return ds; } - @Override - public void setAutoCommit(final boolean autoCommit) throws SQLException { - checkOpen(); - try { - connection.setAutoCommit(autoCommit); - if (cacheState) { - autoCommitCached = Boolean.valueOf(autoCommit); - } - } catch (final SQLException e) { - autoCommitCached = null; - handleException(e); + /** + * Compares innermost delegate to the given connection. + * + * @param c connection to compare innermost delegate with + * @return true if innermost delegate equals c + */ + @SuppressWarnings("resource") + public boolean innermostDelegateEquals(final Connection c) { + final Connection innerCon = getInnermostDelegateInternal(); + if (innerCon == null) { + return c == null; } + return innerCon.equals(c); } @Override - public void setCatalog(final String catalog) throws SQLException { - checkOpen(); - try { - connection.setCatalog(catalog); - } catch (final SQLException e) { - handleException(e); - } + public boolean isClosed() throws SQLException { + return closed || connection == null || connection.isClosed(); } - @Override - public void setReadOnly(final boolean readOnly) throws SQLException { - checkOpen(); - try { - connection.setReadOnly(readOnly); - if (cacheState) { - readOnlyCached = Boolean.valueOf(readOnly); - } - } catch (final SQLException e) { - readOnlyCached = null; - handleException(e); - } + protected boolean isClosedInternal() { + return closed; } @Override - public void setTransactionIsolation(final int level) throws SQLException { + public boolean isReadOnly() throws SQLException { checkOpen(); + if (cacheState && readOnlyCached != null) { + return readOnlyCached.booleanValue(); + } try { - connection.setTransactionIsolation(level); + readOnlyCached = Boolean.valueOf(connection.isReadOnly()); + return readOnlyCached.booleanValue(); } catch (final SQLException e) { handleException(e); + return false; } } @Override - public void setTypeMap(final Map> map) throws SQLException { - checkOpen(); + public boolean isValid(final int timeoutSeconds) throws SQLException { + if (isClosed()) { + return false; + } try { - connection.setTypeMap(map); + return connection.isValid(timeoutSeconds); } catch (final SQLException e) { handleException(e); + return false; } } @Override - public boolean isClosed() throws SQLException { - return closed || connection == null || connection.isClosed(); - } - - protected void checkOpen() throws SQLException { - if (closed) { - if (null != connection) { - String label = ""; - try { - label = connection.toString(); - } catch (final Exception ex) { - // ignore, leave label empty - } - throw new SQLException("Connection " + label + " is closed."); - } - throw new SQLException("Connection is null."); + public boolean isWrapperFor(final Class iface) throws SQLException { + if (iface.isAssignableFrom(getClass())) { + return true; + } else if (iface.isAssignableFrom(connection.getClass())) { + return true; + } else { + return connection.isWrapperFor(iface); } } - protected void activate() { - closed = false; - setLastUsed(); - if (connection instanceof DelegatingConnection) { - ((DelegatingConnection) connection).activate(); - } + @Override + public String nativeSQL(final String sql) throws SQLException { + return apply(connection::nativeSQL, sql); } protected void passivate() throws SQLException { @@ -629,7 +462,7 @@ protected void passivate() throws SQLException { if (trace instanceof Statement) { try { ((Statement) trace).close(); - } catch (Exception e) { + } catch (final Exception e) { thrownList.add(e); } } else if (trace instanceof ResultSet) { @@ -637,7 +470,7 @@ protected void passivate() throws SQLException { // generated via DatabaseMetaData try { ((ResultSet) trace).close(); - } catch (Exception e) { + } catch (final Exception e) { thrownList.add(e); } } @@ -651,253 +484,112 @@ protected void passivate() throws SQLException { } @Override - public int getHoldability() throws SQLException { - checkOpen(); - try { - return connection.getHoldability(); - } catch (final SQLException e) { - handleException(e); - return 0; - } - } - - @Override - public void setHoldability(final int holdability) throws SQLException { - checkOpen(); - try { - connection.setHoldability(holdability); - } catch (final SQLException e) { - handleException(e); - } - } - - @Override - public Savepoint setSavepoint() throws SQLException { - checkOpen(); - try { - return connection.setSavepoint(); - } catch (final SQLException e) { - handleException(e); - return null; - } - } - - @Override - public Savepoint setSavepoint(final String name) throws SQLException { - checkOpen(); - try { - return connection.setSavepoint(name); - } catch (final SQLException e) { - handleException(e); - return null; - } - } - - @Override - public void rollback(final Savepoint savepoint) throws SQLException { - checkOpen(); - try { - connection.rollback(savepoint); - } catch (final SQLException e) { - handleException(e); - } - } - - @Override - public void releaseSavepoint(final Savepoint savepoint) throws SQLException { - checkOpen(); - try { - connection.releaseSavepoint(savepoint); - } catch (final SQLException e) { - handleException(e); - } + public CallableStatement prepareCall(final String sql) throws SQLException { + return apply(() -> init(new DelegatingCallableStatement(this, connection.prepareCall(sql)))); } @Override - public Statement createStatement(final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { - checkOpen(); - try { - final DelegatingStatement ds = new DelegatingStatement(this, - connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability)); - initializeStatement(ds); - return ds; - } catch (final SQLException e) { - handleException(e); - return null; - } + public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) + throws SQLException { + return apply(() -> init( + new DelegatingCallableStatement(this, connection.prepareCall(sql, resultSetType, resultSetConcurrency)))); } @Override - public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { - checkOpen(); - try { - final DelegatingPreparedStatement dps = new DelegatingPreparedStatement(this, - connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); - initializeStatement(dps); - return dps; - } catch (final SQLException e) { - handleException(e); - return null; - } + public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, + final int resultSetHoldability) throws SQLException { + return apply(() -> init(new DelegatingCallableStatement(this, + connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability)))); } @Override - public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) throws SQLException { - checkOpen(); - try { - final DelegatingCallableStatement dcs = new DelegatingCallableStatement(this, - connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); - initializeStatement(dcs); - return dcs; - } catch (final SQLException e) { - handleException(e); - return null; - } + public PreparedStatement prepareStatement(final String sql) throws SQLException { + return apply(() -> init(new DelegatingPreparedStatement(this, connection.prepareStatement(sql)))); } @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; - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply( + () -> init(new DelegatingPreparedStatement(this, connection.prepareStatement(sql, autoGeneratedKeys)))); } @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; - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply( + () -> init(new DelegatingPreparedStatement(this, connection.prepareStatement(sql, columnIndexes)))); } @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; - } catch (final SQLException e) { - handleException(e); - return null; - } + public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) + throws SQLException { + return apply(() -> init(new DelegatingPreparedStatement(this, + connection.prepareStatement(sql, resultSetType, resultSetConcurrency)))); } @Override - public boolean isWrapperFor(final Class iface) throws SQLException { - if (iface.isAssignableFrom(getClass())) { - return true; - } else if (iface.isAssignableFrom(connection.getClass())) { - return true; - } else { - return connection.isWrapperFor(iface); - } + public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, + final int resultSetHoldability) throws SQLException { + return apply(() -> init(new DelegatingPreparedStatement(this, + connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability)))); } @Override - public T unwrap(final Class iface) throws SQLException { - if (iface.isAssignableFrom(getClass())) { - return iface.cast(this); - } else if (iface.isAssignableFrom(connection.getClass())) { - return iface.cast(connection); - } else { - return connection.unwrap(iface); - } + public PreparedStatement prepareStatement(final String sql, final String columnNames[]) throws SQLException { + return apply(() -> init(new DelegatingPreparedStatement(this, connection.prepareStatement(sql, columnNames)))); } @Override - public Array createArrayOf(final String typeName, final Object[] elements) throws SQLException { - checkOpen(); - try { - return connection.createArrayOf(typeName, elements); - } catch (final SQLException e) { - handleException(e); - return null; - } + public void releaseSavepoint(final Savepoint savepoint) throws SQLException { + accept(connection::releaseSavepoint, savepoint); } @Override - public Blob createBlob() throws SQLException { - checkOpen(); - try { - return connection.createBlob(); - } catch (final SQLException e) { - handleException(e); - return null; - } + public void rollback() throws SQLException { + accept(connection::rollback); } @Override - public Clob createClob() throws SQLException { - checkOpen(); - try { - return connection.createClob(); - } catch (final SQLException e) { - handleException(e); - return null; - } + public void rollback(final Savepoint savepoint) throws SQLException { + accept(connection::rollback, savepoint); } @Override - public NClob createNClob() throws SQLException { + public void setAutoCommit(final boolean autoCommit) throws SQLException { checkOpen(); try { - return connection.createNClob(); + connection.setAutoCommit(autoCommit); + if (cacheState) { + autoCommitCached = Boolean.valueOf(autoCommit); + } } catch (final SQLException e) { + autoCommitCached = null; handleException(e); - return null; } } - @Override - public SQLXML createSQLXML() throws SQLException { - checkOpen(); - try { - return connection.createSQLXML(); - } catch (final SQLException e) { - handleException(e); - return null; - } + /** + * Sets the state caching flag. + * + * @param cacheState The new value for the state caching flag + */ + public void setCacheState(final boolean cacheState) { + this.cacheState = cacheState; } @Override - public Struct createStruct(final String typeName, final Object[] attributes) throws SQLException { - checkOpen(); - try { - return connection.createStruct(typeName, attributes); - } catch (final SQLException e) { - handleException(e); - return null; - } + public void setCatalog(final String catalog) throws SQLException { + accept(connection::setCatalog, catalog); } @Override - public boolean isValid(final int timeoutSeconds) throws SQLException { - if (isClosed()) { - return false; - } + public void setClientInfo(final Properties properties) throws SQLClientInfoException { try { - return connection.isValid(timeoutSeconds); + checkOpen(); + connection.setClientInfo(properties); + } catch (final SQLClientInfoException e) { + throw e; } catch (final SQLException e) { - handleException(e); - return false; + throw new SQLClientInfoException("Connection is closed.", EMPTY_FAILED_PROPERTIES, e); } } @@ -913,89 +605,121 @@ public void setClientInfo(final String name, final String value) throws SQLClien } } + protected void setClosedInternal(final boolean closed) { + this.closed = closed; + } + + /** + * Sets the default query timeout that will be used for {@link Statement}s created from this connection. + * null means that the driver default will be used. + * + * @param defaultQueryTimeoutSeconds the new query timeout limit in seconds; zero means there is no limit + */ + public void setDefaultQueryTimeout(final Integer defaultQueryTimeoutSeconds) { + this.defaultQueryTimeoutSeconds = defaultQueryTimeoutSeconds; + } + + /** + * Sets my delegate. + * + * @param connection my delegate. + */ + public void setDelegate(final C connection) { + this.connection = connection; + } + @Override - public void setClientInfo(final Properties properties) throws SQLClientInfoException { - try { - checkOpen(); - connection.setClientInfo(properties); - } catch (final SQLClientInfoException e) { - throw e; - } catch (final SQLException e) { - throw new SQLClientInfoException("Connection is closed.", EMPTY_FAILED_PROPERTIES, e); - } + public void setHoldability(final int holdability) throws SQLException { + acceptInt(connection::setHoldability, holdability); } @Override - public Properties getClientInfo() throws SQLException { - checkOpen(); - try { - return connection.getClientInfo(); - } catch (final SQLException e) { - handleException(e); - return null; - } + public void setNetworkTimeout(final Executor executor, final int milliseconds) throws SQLException { + accept(Jdbc41Bridge::setNetworkTimeout, connection, executor, milliseconds); } @Override - public String getClientInfo(final String name) throws SQLException { + public void setReadOnly(final boolean readOnly) throws SQLException { checkOpen(); try { - return connection.getClientInfo(name); + connection.setReadOnly(readOnly); + if (cacheState) { + readOnlyCached = Boolean.valueOf(readOnly); + } } catch (final SQLException e) { + readOnlyCached = null; handleException(e); - return null; } } + @Override + public Savepoint setSavepoint() throws SQLException { + return apply(connection::setSavepoint); + } + + @Override + public Savepoint setSavepoint(final String name) throws SQLException { + return apply(connection::setSavepoint, name); + } + @Override public void setSchema(final String schema) throws SQLException { - checkOpen(); - try { - Jdbc41Bridge.setSchema(connection, schema); - } catch (final SQLException e) { - handleException(e); - } + accept(connection::setSchema, schema); } @Override - public String getSchema() throws SQLException { - checkOpen(); - try { - return Jdbc41Bridge.getSchema(connection); - } catch (final SQLException e) { - handleException(e); - return null; - } + public void setTransactionIsolation(final int level) throws SQLException { + acceptInt(connection::setTransactionIsolation, level); + } @Override - public void abort(final Executor executor) throws SQLException { - checkOpen(); - try { - Jdbc41Bridge.abort(connection, executor); - } catch (final SQLException e) { - handleException(e); - } + public void setTypeMap(final Map> map) throws SQLException { + accept(connection::setTypeMap, map); } + /** + * Returns a string representation of the metadata associated with the innermost delegate connection. + */ + @SuppressWarnings("resource") @Override - public void setNetworkTimeout(final Executor executor, final int milliseconds) throws SQLException { - checkOpen(); - try { - Jdbc41Bridge.setNetworkTimeout(connection, executor, milliseconds); - } catch (final SQLException e) { - handleException(e); + public synchronized String toString() { + String str = null; + + final Connection conn = this.getInnermostDelegateInternal(); + if (conn != null) { + try { + if (conn.isClosed()) { + str = "connection is closed"; + } else { + final StringBuffer sb = new StringBuffer(); + sb.append(hashCode()); + final DatabaseMetaData meta = conn.getMetaData(); + if (meta != null) { + sb.append(", URL="); + sb.append(meta.getURL()); + sb.append(", UserName="); + sb.append(meta.getUserName()); + sb.append(", "); + sb.append(meta.getDriverName()); + str = sb.toString(); + } + } + } catch (final SQLException ex) { + // Ignore + } } + return str != null ? str : super.toString(); } @Override - public int getNetworkTimeout() throws SQLException { - checkOpen(); - try { - return Jdbc41Bridge.getNetworkTimeout(connection); - } catch (final SQLException e) { - handleException(e); - return 0; + public T unwrap(final Class iface) throws SQLException { + if (iface.isAssignableFrom(getClass())) { + return iface.cast(this); + } else if (iface.isAssignableFrom(connection.getClass())) { + return iface.cast(connection); + } else { + return connection.unwrap(iface); } } } diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingDatabaseMetaData.java b/src/main/java/org/apache/commons/dbcp2/DelegatingDatabaseMetaData.java index 9aea2d30c..558568c4c 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingDatabaseMetaData.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingDatabaseMetaData.java @@ -22,6 +22,13 @@ import java.sql.RowIdLifetime; import java.sql.SQLException; +import org.apache.commons.dbcp2.function.SQLFunction0; +import org.apache.commons.dbcp2.function.SQLFunction2; +import org.apache.commons.dbcp2.function.SQLFunction3; +import org.apache.commons.dbcp2.function.SQLFunction4; +import org.apache.commons.dbcp2.function.SQLFunction5; +import org.apache.commons.dbcp2.function.SQLFunction6; + /** *

* A base delegating implementation of {@link DatabaseMetaData}. @@ -33,7 +40,7 @@ * * @since 2.0 */ -public class DelegatingDatabaseMetaData implements DatabaseMetaData { +public class DelegatingDatabaseMetaData extends ResourceFunctions implements DatabaseMetaData { /** My delegate {@link DatabaseMetaData} */ private final DatabaseMetaData databaseMetaData; @@ -44,13 +51,11 @@ public class DelegatingDatabaseMetaData implements DatabaseMetaData { /** * Constructs a new instance for the given delegating connection and database meta data. * - * @param connection - * the delegating connection - * @param databaseMetaData - * the database meta data + * @param connection the delegating connection + * @param databaseMetaData the database meta data */ public DelegatingDatabaseMetaData(final DelegatingConnection connection, - final DatabaseMetaData databaseMetaData) { + final DatabaseMetaData databaseMetaData) { super(); this.connection = connection; this.databaseMetaData = databaseMetaData; @@ -58,177 +63,87 @@ public DelegatingDatabaseMetaData(final DelegatingConnection connection, @Override public boolean allProceduresAreCallable() throws SQLException { - try { - return databaseMetaData.allProceduresAreCallable(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::allProceduresAreCallable); } @Override public boolean allTablesAreSelectable() throws SQLException { - try { - return databaseMetaData.allTablesAreSelectable(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::allTablesAreSelectable); } @Override public boolean autoCommitFailureClosesAllResultSets() throws SQLException { - try { - return databaseMetaData.autoCommitFailureClosesAllResultSets(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::autoCommitFailureClosesAllResultSets); } @Override public boolean dataDefinitionCausesTransactionCommit() throws SQLException { - try { - return databaseMetaData.dataDefinitionCausesTransactionCommit(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::dataDefinitionCausesTransactionCommit); } @Override public boolean dataDefinitionIgnoredInTransactions() throws SQLException { - try { - return databaseMetaData.dataDefinitionIgnoredInTransactions(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::dataDefinitionIgnoredInTransactions); } @Override public boolean deletesAreDetected(final int type) throws SQLException { - try { - return databaseMetaData.deletesAreDetected(type); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyIntTo(databaseMetaData::deletesAreDetected, type, false); } @Override public boolean doesMaxRowSizeIncludeBlobs() throws SQLException { - try { - return databaseMetaData.doesMaxRowSizeIncludeBlobs(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::doesMaxRowSizeIncludeBlobs); } @Override public boolean generatedKeyAlwaysReturned() throws SQLException { - connection.checkOpen(); - try { - return Jdbc41Bridge.generatedKeyAlwaysReturned(databaseMetaData); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::generatedKeyAlwaysReturned); } @Override public ResultSet getAttributes(final String catalog, final String schemaPattern, final String typeNamePattern, - final String attributeNamePattern) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getAttributes(catalog, schemaPattern, typeNamePattern, attributeNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final String attributeNamePattern) throws SQLException { + return toResultSet(databaseMetaData::getAttributes, catalog, schemaPattern, typeNamePattern, + attributeNamePattern); } @Override public ResultSet getBestRowIdentifier(final String catalog, final String schema, final String table, - final int scope, final boolean nullable) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getBestRowIdentifier(catalog, schema, table, scope, nullable)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final int scope, final boolean nullable) throws SQLException { + return toResultSet(databaseMetaData::getBestRowIdentifier, catalog, schema, table, scope, nullable); } @Override public ResultSet getCatalogs() throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, databaseMetaData.getCatalogs()); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return toResultSet(databaseMetaData::getCatalogs); } @Override public String getCatalogSeparator() throws SQLException { - try { - return databaseMetaData.getCatalogSeparator(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getCatalogSeparator); } @Override public String getCatalogTerm() throws SQLException { - try { - return databaseMetaData.getCatalogTerm(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getCatalogTerm); } @Override public ResultSet getClientInfoProperties() throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, databaseMetaData.getClientInfoProperties()); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return toResultSet(databaseMetaData::getClientInfoProperties); } @Override public ResultSet getColumnPrivileges(final String catalog, final String schema, final String table, - final String columnNamePattern) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getColumnPrivileges(catalog, schema, table, columnNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final String columnNamePattern) throws SQLException { + return toResultSet(databaseMetaData::getColumnPrivileges, catalog, schema, table, columnNamePattern); } @Override public ResultSet getColumns(final String catalog, final String schemaPattern, final String tableNamePattern, - final String columnNamePattern) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final String columnNamePattern) throws SQLException { + return toResultSet(databaseMetaData::getColumns, catalog, schemaPattern, tableNamePattern, columnNamePattern); } @Override @@ -238,65 +153,34 @@ public Connection getConnection() throws SQLException { @Override public ResultSet getCrossReference(final String parentCatalog, final String parentSchema, final String parentTable, - final String foreignCatalog, final String foreignSchema, final String foreignTable) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, databaseMetaData.getCrossReference(parentCatalog, - parentSchema, parentTable, foreignCatalog, foreignSchema, foreignTable)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final String foreignCatalog, final String foreignSchema, final String foreignTable) throws SQLException { + return toResultSet(databaseMetaData::getCrossReference, parentCatalog, parentSchema, parentTable, + foreignCatalog, foreignSchema, foreignTable); } @Override public int getDatabaseMajorVersion() throws SQLException { - try { - return databaseMetaData.getDatabaseMajorVersion(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getDatabaseMajorVersion); } @Override public int getDatabaseMinorVersion() throws SQLException { - try { - return databaseMetaData.getDatabaseMinorVersion(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getDatabaseMinorVersion); } @Override public String getDatabaseProductName() throws SQLException { - try { - return databaseMetaData.getDatabaseProductName(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getDatabaseProductName); } @Override public String getDatabaseProductVersion() throws SQLException { - try { - return databaseMetaData.getDatabaseProductVersion(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getDatabaseProductVersion); } @Override public int getDefaultTransactionIsolation() throws SQLException { - try { - return databaseMetaData.getDefaultTransactionIsolation(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getDefaultTransactionIsolation); } /** @@ -320,107 +204,53 @@ public int getDriverMinorVersion() { @Override public String getDriverName() throws SQLException { - try { - return databaseMetaData.getDriverName(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getDriverName); } @Override public String getDriverVersion() throws SQLException { - try { - return databaseMetaData.getDriverVersion(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getDriverVersion); } @Override public ResultSet getExportedKeys(final String catalog, final String schema, final String table) - throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getExportedKeys(catalog, schema, table)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + throws SQLException { + return toResultSet(databaseMetaData::getExportedKeys, catalog, schema, table); } @Override public String getExtraNameCharacters() throws SQLException { - try { - return databaseMetaData.getExtraNameCharacters(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getExtraNameCharacters); } @Override public ResultSet getFunctionColumns(final String catalog, final String schemaPattern, - final String functionNamePattern, final String columnNamePattern) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, databaseMetaData.getFunctionColumns(catalog, - schemaPattern, functionNamePattern, columnNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final String functionNamePattern, final String columnNamePattern) throws SQLException { + return toResultSet(databaseMetaData::getFunctionColumns, catalog, schemaPattern, functionNamePattern, + columnNamePattern); } @Override public ResultSet getFunctions(final String catalog, final String schemaPattern, final String functionNamePattern) - throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getFunctions(catalog, schemaPattern, functionNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + throws SQLException { + return toResultSet(databaseMetaData::getFunctions, catalog, schemaPattern, functionNamePattern); } @Override public String getIdentifierQuoteString() throws SQLException { - try { - return databaseMetaData.getIdentifierQuoteString(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getIdentifierQuoteString); } @Override public ResultSet getImportedKeys(final String catalog, final String schema, final String table) - throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getImportedKeys(catalog, schema, table)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + throws SQLException { + return toResultSet(databaseMetaData::getImportedKeys, catalog, schema, table); } @Override public ResultSet getIndexInfo(final String catalog, final String schema, final String table, final boolean unique, - final boolean approximate) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getIndexInfo(catalog, schema, table, unique, approximate)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final boolean approximate) throws SQLException { + return toResultSet(databaseMetaData::getIndexInfo, catalog, schema, table, unique, approximate); } /** @@ -450,142 +280,72 @@ public DatabaseMetaData getInnermostDelegate() { @Override public int getJDBCMajorVersion() throws SQLException { - try { - return databaseMetaData.getJDBCMajorVersion(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getJDBCMajorVersion); } @Override public int getJDBCMinorVersion() throws SQLException { - try { - return databaseMetaData.getJDBCMinorVersion(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getJDBCMinorVersion); } @Override public int getMaxBinaryLiteralLength() throws SQLException { - try { - return databaseMetaData.getMaxBinaryLiteralLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxBinaryLiteralLength); } @Override public int getMaxCatalogNameLength() throws SQLException { - try { - return databaseMetaData.getMaxCatalogNameLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxCatalogNameLength); } @Override public int getMaxCharLiteralLength() throws SQLException { - try { - return databaseMetaData.getMaxCharLiteralLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxCharLiteralLength); } @Override public int getMaxColumnNameLength() throws SQLException { - try { - return databaseMetaData.getMaxColumnNameLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxColumnNameLength); } @Override public int getMaxColumnsInGroupBy() throws SQLException { - try { - return databaseMetaData.getMaxColumnsInGroupBy(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxColumnsInGroupBy); } @Override public int getMaxColumnsInIndex() throws SQLException { - try { - return databaseMetaData.getMaxColumnsInIndex(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxColumnsInIndex); } @Override public int getMaxColumnsInOrderBy() throws SQLException { - try { - return databaseMetaData.getMaxColumnsInOrderBy(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxColumnsInOrderBy); } @Override public int getMaxColumnsInSelect() throws SQLException { - try { - return databaseMetaData.getMaxColumnsInSelect(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxColumnsInSelect); } @Override public int getMaxColumnsInTable() throws SQLException { - try { - return databaseMetaData.getMaxColumnsInTable(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxColumnsInTable); } @Override public int getMaxConnections() throws SQLException { - try { - return databaseMetaData.getMaxConnections(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxConnections); } @Override public int getMaxCursorNameLength() throws SQLException { - try { - return databaseMetaData.getMaxCursorNameLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxCursorNameLength); } @Override public int getMaxIndexLength() throws SQLException { - try { - return databaseMetaData.getMaxIndexLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxIndexLength); } /** @@ -593,397 +353,196 @@ public int getMaxIndexLength() throws SQLException { */ @Override public long getMaxLogicalLobSize() throws SQLException { - try { - return databaseMetaData.getMaxLogicalLobSize(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0L(databaseMetaData::getMaxLogicalLobSize); } @Override public int getMaxProcedureNameLength() throws SQLException { - try { - return databaseMetaData.getMaxProcedureNameLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxProcedureNameLength); } @Override public int getMaxRowSize() throws SQLException { - try { - return databaseMetaData.getMaxRowSize(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxRowSize); } @Override public int getMaxSchemaNameLength() throws SQLException { - try { - return databaseMetaData.getMaxSchemaNameLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxSchemaNameLength); } @Override public int getMaxStatementLength() throws SQLException { - try { - return databaseMetaData.getMaxStatementLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxStatementLength); } @Override public int getMaxStatements() throws SQLException { - try { - return databaseMetaData.getMaxStatements(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxStatements); } @Override public int getMaxTableNameLength() throws SQLException { - try { - return databaseMetaData.getMaxTableNameLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxTableNameLength); } @Override public int getMaxTablesInSelect() throws SQLException { - try { - return databaseMetaData.getMaxTablesInSelect(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxTablesInSelect); } @Override public int getMaxUserNameLength() throws SQLException { - try { - return databaseMetaData.getMaxUserNameLength(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getMaxUserNameLength); } @Override public String getNumericFunctions() throws SQLException { - try { - return databaseMetaData.getNumericFunctions(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getNumericFunctions); } @Override public ResultSet getPrimaryKeys(final String catalog, final String schema, final String table) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getPrimaryKeys(catalog, schema, table)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return toResultSet(databaseMetaData::getPrimaryKeys, catalog, schema, table); } @Override public ResultSet getProcedureColumns(final String catalog, final String schemaPattern, - final String procedureNamePattern, final String columnNamePattern) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, databaseMetaData.getProcedureColumns(catalog, - schemaPattern, procedureNamePattern, columnNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final String procedureNamePattern, final String columnNamePattern) throws SQLException { + return toResultSet(databaseMetaData::getProcedureColumns, catalog, schemaPattern, procedureNamePattern, + columnNamePattern); } @Override public ResultSet getProcedures(final String catalog, final String schemaPattern, final String procedureNamePattern) - throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getProcedures(catalog, schemaPattern, procedureNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + throws SQLException { + return toResultSet(databaseMetaData::getProcedures, catalog, schemaPattern, procedureNamePattern); } @Override public String getProcedureTerm() throws SQLException { - try { - return databaseMetaData.getProcedureTerm(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getProcedureTerm); } @Override public ResultSet getPseudoColumns(final String catalog, final String schemaPattern, final String tableNamePattern, - final String columnNamePattern) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, Jdbc41Bridge.getPseudoColumns(databaseMetaData, - catalog, schemaPattern, tableNamePattern, columnNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final String columnNamePattern) throws SQLException { + return toResultSet(databaseMetaData::getPseudoColumns, catalog, schemaPattern, tableNamePattern, + columnNamePattern); } @Override public int getResultSetHoldability() throws SQLException { - try { - return databaseMetaData.getResultSetHoldability(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(databaseMetaData::getResultSetHoldability); } @Override public RowIdLifetime getRowIdLifetime() throws SQLException { - try { - return databaseMetaData.getRowIdLifetime(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getRowIdLifetime); } @Override public ResultSet getSchemas() throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, databaseMetaData.getSchemas()); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return toResultSet(databaseMetaData::getSchemas); } @Override public ResultSet getSchemas(final String catalog, final String schemaPattern) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, databaseMetaData.getSchemas(catalog, schemaPattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return toResultSet(databaseMetaData::getSchemas, catalog, schemaPattern); } @Override public String getSchemaTerm() throws SQLException { - try { - return databaseMetaData.getSchemaTerm(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getSchemaTerm); } @Override public String getSearchStringEscape() throws SQLException { - try { - return databaseMetaData.getSearchStringEscape(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getSearchStringEscape); } @Override public String getSQLKeywords() throws SQLException { - try { - return databaseMetaData.getSQLKeywords(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getSQLKeywords); } @Override public int getSQLStateType() throws SQLException { - try { - return databaseMetaData.getSQLStateType(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return apply(databaseMetaData::getSQLStateType); } @Override public String getStringFunctions() throws SQLException { - try { - return databaseMetaData.getStringFunctions(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getStringFunctions); } @Override public ResultSet getSuperTables(final String catalog, final String schemaPattern, final String tableNamePattern) - throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getSuperTables(catalog, schemaPattern, tableNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + throws SQLException { + return toResultSet(databaseMetaData::getSuperTables, catalog, schemaPattern, tableNamePattern); } @Override public ResultSet getSuperTypes(final String catalog, final String schemaPattern, final String typeNamePattern) - throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getSuperTypes(catalog, schemaPattern, typeNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + throws SQLException { + return toResultSet(databaseMetaData::getSuperTypes, catalog, schemaPattern, typeNamePattern); } @Override public String getSystemFunctions() throws SQLException { - try { - return databaseMetaData.getSystemFunctions(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getSystemFunctions); } @Override public ResultSet getTablePrivileges(final String catalog, final String schemaPattern, final String tableNamePattern) - throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getTablePrivileges(catalog, schemaPattern, tableNamePattern)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + throws SQLException { + return toResultSet(databaseMetaData::getTablePrivileges, catalog, schemaPattern, tableNamePattern); } @Override public ResultSet getTables(final String catalog, final String schemaPattern, final String tableNamePattern, - final String[] types) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getTables(catalog, schemaPattern, tableNamePattern, types)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final String[] types) throws SQLException { + return toResultSet(databaseMetaData::getTables, catalog, schemaPattern, tableNamePattern, types); } @Override public ResultSet getTableTypes() throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, databaseMetaData.getTableTypes()); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return toResultSet(databaseMetaData::getTableTypes); } @Override public String getTimeDateFunctions() throws SQLException { - try { - return databaseMetaData.getTimeDateFunctions(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getTimeDateFunctions); } @Override public ResultSet getTypeInfo() throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, databaseMetaData.getTypeInfo()); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return toResultSet(databaseMetaData::getTypeInfo); } @Override public ResultSet getUDTs(final String catalog, final String schemaPattern, final String typeNamePattern, - final int[] types) throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getUDTs(catalog, schemaPattern, typeNamePattern, types)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + final int[] types) throws SQLException { + return toResultSet(databaseMetaData::getUDTs, catalog, schemaPattern, typeNamePattern, types); } @Override public String getURL() throws SQLException { - try { - return databaseMetaData.getURL(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getURL); } @Override public String getUserName() throws SQLException { - try { - return databaseMetaData.getUserName(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(databaseMetaData::getUserName); } @Override public ResultSet getVersionColumns(final String catalog, final String schema, final String table) - throws SQLException { - connection.checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(connection, - databaseMetaData.getVersionColumns(catalog, schema, table)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + throws SQLException { + return toResultSet(databaseMetaData::getVersionColumns, catalog, schema, table); } + @Override protected void handleException(final SQLException e) throws SQLException { if (connection != null) { connection.handleException(e); @@ -994,32 +553,17 @@ protected void handleException(final SQLException e) throws SQLException { @Override public boolean insertsAreDetected(final int type) throws SQLException { - try { - return databaseMetaData.insertsAreDetected(type); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyIntTo(databaseMetaData::insertsAreDetected, type, false); } @Override public boolean isCatalogAtStart() throws SQLException { - try { - return databaseMetaData.isCatalogAtStart(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::isCatalogAtStart); } @Override public boolean isReadOnly() throws SQLException { - try { - return databaseMetaData.isReadOnly(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::isReadOnly); } @Override @@ -1035,632 +579,317 @@ public boolean isWrapperFor(final Class iface) throws SQLException { @Override public boolean locatorsUpdateCopy() throws SQLException { - try { - return databaseMetaData.locatorsUpdateCopy(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::locatorsUpdateCopy); } @Override public boolean nullPlusNonNullIsNull() throws SQLException { - try { - return databaseMetaData.nullPlusNonNullIsNull(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::nullPlusNonNullIsNull); } @Override public boolean nullsAreSortedAtEnd() throws SQLException { - try { - return databaseMetaData.nullsAreSortedAtEnd(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::nullsAreSortedAtEnd); } @Override public boolean nullsAreSortedAtStart() throws SQLException { - try { - return databaseMetaData.nullsAreSortedAtStart(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::nullsAreSortedAtStart); } @Override public boolean nullsAreSortedHigh() throws SQLException { - try { - return databaseMetaData.nullsAreSortedHigh(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::nullsAreSortedHigh); } @Override public boolean nullsAreSortedLow() throws SQLException { - try { - return databaseMetaData.nullsAreSortedLow(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::nullsAreSortedLow); } @Override public boolean othersDeletesAreVisible(final int type) throws SQLException { - try { - return databaseMetaData.othersDeletesAreVisible(type); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyIntTo(databaseMetaData::othersDeletesAreVisible, type, false); } @Override public boolean othersInsertsAreVisible(final int type) throws SQLException { - try { - return databaseMetaData.othersInsertsAreVisible(type); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyIntTo(databaseMetaData::othersInsertsAreVisible, type, false); } @Override public boolean othersUpdatesAreVisible(final int type) throws SQLException { - try { - return databaseMetaData.othersUpdatesAreVisible(type); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyIntTo(databaseMetaData::othersUpdatesAreVisible, type, false); } @Override public boolean ownDeletesAreVisible(final int type) throws SQLException { - try { - return databaseMetaData.ownDeletesAreVisible(type); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyIntTo(databaseMetaData::ownDeletesAreVisible, type, false); } @Override public boolean ownInsertsAreVisible(final int type) throws SQLException { - try { - return databaseMetaData.ownInsertsAreVisible(type); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyIntTo(databaseMetaData::ownInsertsAreVisible, type, false); } @Override public boolean ownUpdatesAreVisible(final int type) throws SQLException { - try { - return databaseMetaData.ownUpdatesAreVisible(type); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyIntTo(databaseMetaData::ownUpdatesAreVisible, type, false); } @Override public boolean storesLowerCaseIdentifiers() throws SQLException { - try { - return databaseMetaData.storesLowerCaseIdentifiers(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::storesLowerCaseIdentifiers); } @Override public boolean storesLowerCaseQuotedIdentifiers() throws SQLException { - try { - return databaseMetaData.storesLowerCaseQuotedIdentifiers(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::storesLowerCaseQuotedIdentifiers); } @Override public boolean storesMixedCaseIdentifiers() throws SQLException { - try { - return databaseMetaData.storesMixedCaseIdentifiers(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::storesMixedCaseIdentifiers); } @Override public boolean storesMixedCaseQuotedIdentifiers() throws SQLException { - try { - return databaseMetaData.storesMixedCaseQuotedIdentifiers(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::storesMixedCaseQuotedIdentifiers); } @Override public boolean storesUpperCaseIdentifiers() throws SQLException { - try { - return databaseMetaData.storesUpperCaseIdentifiers(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::storesUpperCaseIdentifiers); } @Override public boolean storesUpperCaseQuotedIdentifiers() throws SQLException { - try { - return databaseMetaData.storesUpperCaseQuotedIdentifiers(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::storesUpperCaseQuotedIdentifiers); } @Override public boolean supportsAlterTableWithAddColumn() throws SQLException { - try { - return databaseMetaData.supportsAlterTableWithAddColumn(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsAlterTableWithAddColumn); } @Override public boolean supportsAlterTableWithDropColumn() throws SQLException { - try { - return databaseMetaData.supportsAlterTableWithDropColumn(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsAlterTableWithDropColumn); } @Override public boolean supportsANSI92EntryLevelSQL() throws SQLException { - try { - return databaseMetaData.supportsANSI92EntryLevelSQL(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsANSI92EntryLevelSQL); } @Override public boolean supportsANSI92FullSQL() throws SQLException { - try { - return databaseMetaData.supportsANSI92FullSQL(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsANSI92FullSQL); } @Override public boolean supportsANSI92IntermediateSQL() throws SQLException { - try { - return databaseMetaData.supportsANSI92IntermediateSQL(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsANSI92IntermediateSQL); } @Override public boolean supportsBatchUpdates() throws SQLException { - try { - return databaseMetaData.supportsBatchUpdates(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsBatchUpdates); } @Override public boolean supportsCatalogsInDataManipulation() throws SQLException { - try { - return databaseMetaData.supportsCatalogsInDataManipulation(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsCatalogsInDataManipulation); } @Override public boolean supportsCatalogsInIndexDefinitions() throws SQLException { - try { - return databaseMetaData.supportsCatalogsInIndexDefinitions(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsCatalogsInIndexDefinitions); } @Override public boolean supportsCatalogsInPrivilegeDefinitions() throws SQLException { - try { - return databaseMetaData.supportsCatalogsInPrivilegeDefinitions(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsCatalogsInPrivilegeDefinitions); } @Override public boolean supportsCatalogsInProcedureCalls() throws SQLException { - try { - return databaseMetaData.supportsCatalogsInProcedureCalls(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsCatalogsInProcedureCalls); } @Override public boolean supportsCatalogsInTableDefinitions() throws SQLException { - try { - return databaseMetaData.supportsCatalogsInTableDefinitions(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsCatalogsInTableDefinitions); } @Override public boolean supportsColumnAliasing() throws SQLException { - try { - return databaseMetaData.supportsColumnAliasing(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsColumnAliasing); } @Override public boolean supportsConvert() throws SQLException { - try { - return databaseMetaData.supportsConvert(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsConvert); } @Override public boolean supportsConvert(final int fromType, final int toType) throws SQLException { - try { - return databaseMetaData.supportsConvert(fromType, toType); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(databaseMetaData::supportsConvert, fromType, toType, false); } @Override public boolean supportsCoreSQLGrammar() throws SQLException { - try { - return databaseMetaData.supportsCoreSQLGrammar(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsCoreSQLGrammar); } @Override public boolean supportsCorrelatedSubqueries() throws SQLException { - try { - return databaseMetaData.supportsCorrelatedSubqueries(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsCorrelatedSubqueries); } @Override public boolean supportsDataDefinitionAndDataManipulationTransactions() throws SQLException { - try { - return databaseMetaData.supportsDataDefinitionAndDataManipulationTransactions(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsDataDefinitionAndDataManipulationTransactions); } @Override public boolean supportsDataManipulationTransactionsOnly() throws SQLException { - try { - return databaseMetaData.supportsDataManipulationTransactionsOnly(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsDataManipulationTransactionsOnly); } @Override public boolean supportsDifferentTableCorrelationNames() throws SQLException { - try { - return databaseMetaData.supportsDifferentTableCorrelationNames(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsDifferentTableCorrelationNames); } @Override public boolean supportsExpressionsInOrderBy() throws SQLException { - try { - return databaseMetaData.supportsExpressionsInOrderBy(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsExpressionsInOrderBy); } @Override public boolean supportsExtendedSQLGrammar() throws SQLException { - try { - return databaseMetaData.supportsExtendedSQLGrammar(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsExtendedSQLGrammar); } @Override public boolean supportsFullOuterJoins() throws SQLException { - try { - return databaseMetaData.supportsFullOuterJoins(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsFullOuterJoins); } @Override public boolean supportsGetGeneratedKeys() throws SQLException { - try { - return databaseMetaData.supportsGetGeneratedKeys(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsGetGeneratedKeys); } @Override public boolean supportsGroupBy() throws SQLException { - try { - return databaseMetaData.supportsGroupBy(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsGroupBy); } @Override public boolean supportsGroupByBeyondSelect() throws SQLException { - try { - return databaseMetaData.supportsGroupByBeyondSelect(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsGroupByBeyondSelect); } @Override public boolean supportsGroupByUnrelated() throws SQLException { - try { - return databaseMetaData.supportsGroupByUnrelated(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsGroupByUnrelated); } @Override public boolean supportsIntegrityEnhancementFacility() throws SQLException { - try { - return databaseMetaData.supportsIntegrityEnhancementFacility(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsIntegrityEnhancementFacility); } @Override public boolean supportsLikeEscapeClause() throws SQLException { - try { - return databaseMetaData.supportsLikeEscapeClause(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsLikeEscapeClause); } @Override public boolean supportsLimitedOuterJoins() throws SQLException { - try { - return databaseMetaData.supportsLimitedOuterJoins(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsLimitedOuterJoins); } @Override public boolean supportsMinimumSQLGrammar() throws SQLException { - try { - return databaseMetaData.supportsMinimumSQLGrammar(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsMinimumSQLGrammar); } @Override public boolean supportsMixedCaseIdentifiers() throws SQLException { - try { - return databaseMetaData.supportsMixedCaseIdentifiers(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsMixedCaseIdentifiers); } @Override public boolean supportsMixedCaseQuotedIdentifiers() throws SQLException { - try { - return databaseMetaData.supportsMixedCaseQuotedIdentifiers(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsMixedCaseQuotedIdentifiers); } @Override public boolean supportsMultipleOpenResults() throws SQLException { - try { - return databaseMetaData.supportsMultipleOpenResults(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsMultipleOpenResults); } @Override public boolean supportsMultipleResultSets() throws SQLException { - try { - return databaseMetaData.supportsMultipleResultSets(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsMultipleResultSets); } @Override public boolean supportsMultipleTransactions() throws SQLException { - try { - return databaseMetaData.supportsMultipleTransactions(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsMultipleTransactions); } @Override public boolean supportsNamedParameters() throws SQLException { - try { - return databaseMetaData.supportsNamedParameters(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsNamedParameters); } @Override public boolean supportsNonNullableColumns() throws SQLException { - try { - return databaseMetaData.supportsNonNullableColumns(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsNonNullableColumns); } @Override public boolean supportsOpenCursorsAcrossCommit() throws SQLException { - try { - return databaseMetaData.supportsOpenCursorsAcrossCommit(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsOpenCursorsAcrossCommit); } @Override public boolean supportsOpenCursorsAcrossRollback() throws SQLException { - try { - return databaseMetaData.supportsOpenCursorsAcrossRollback(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsOpenCursorsAcrossRollback); } @Override public boolean supportsOpenStatementsAcrossCommit() throws SQLException { - try { - return databaseMetaData.supportsOpenStatementsAcrossCommit(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsOpenStatementsAcrossCommit); } @Override public boolean supportsOpenStatementsAcrossRollback() throws SQLException { - try { - return databaseMetaData.supportsOpenStatementsAcrossRollback(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsOpenStatementsAcrossRollback); } @Override public boolean supportsOrderByUnrelated() throws SQLException { - try { - return databaseMetaData.supportsOrderByUnrelated(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsOrderByUnrelated); } @Override public boolean supportsOuterJoins() throws SQLException { - try { - return databaseMetaData.supportsOuterJoins(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsOuterJoins); } @Override public boolean supportsPositionedDelete() throws SQLException { - try { - return databaseMetaData.supportsPositionedDelete(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsPositionedDelete); } @Override public boolean supportsPositionedUpdate() throws SQLException { - try { - return databaseMetaData.supportsPositionedUpdate(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsPositionedUpdate); } /** @@ -1668,234 +897,154 @@ public boolean supportsPositionedUpdate() throws SQLException { */ @Override public boolean supportsRefCursors() throws SQLException { - try { - return databaseMetaData.supportsRefCursors(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsRefCursors); } @Override public boolean supportsResultSetConcurrency(final int type, final int concurrency) throws SQLException { - try { - return databaseMetaData.supportsResultSetConcurrency(type, concurrency); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(databaseMetaData::supportsResultSetConcurrency, type, concurrency, false); } @Override public boolean supportsResultSetHoldability(final int holdability) throws SQLException { - try { - return databaseMetaData.supportsResultSetHoldability(holdability); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(databaseMetaData::supportsResultSetHoldability, holdability, false); } @Override public boolean supportsResultSetType(final int type) throws SQLException { - try { - return databaseMetaData.supportsResultSetType(type); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(databaseMetaData::supportsResultSetType, type, false); } @Override public boolean supportsSavepoints() throws SQLException { - try { - return databaseMetaData.supportsSavepoints(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSavepoints); } @Override public boolean supportsSchemasInDataManipulation() throws SQLException { - try { - return databaseMetaData.supportsSchemasInDataManipulation(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSchemasInDataManipulation); } @Override public boolean supportsSchemasInIndexDefinitions() throws SQLException { - try { - return databaseMetaData.supportsSchemasInIndexDefinitions(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSchemasInIndexDefinitions); } @Override public boolean supportsSchemasInPrivilegeDefinitions() throws SQLException { - try { - return databaseMetaData.supportsSchemasInPrivilegeDefinitions(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSchemasInPrivilegeDefinitions); } @Override public boolean supportsSchemasInProcedureCalls() throws SQLException { - try { - return databaseMetaData.supportsSchemasInProcedureCalls(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSchemasInProcedureCalls); } @Override public boolean supportsSchemasInTableDefinitions() throws SQLException { - try { - return databaseMetaData.supportsSchemasInTableDefinitions(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSchemasInTableDefinitions); } @Override public boolean supportsSelectForUpdate() throws SQLException { - try { - return databaseMetaData.supportsSelectForUpdate(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSelectForUpdate); } @Override public boolean supportsStatementPooling() throws SQLException { - try { - return databaseMetaData.supportsStatementPooling(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsStatementPooling); } @Override public boolean supportsStoredFunctionsUsingCallSyntax() throws SQLException { - try { - return databaseMetaData.supportsStoredFunctionsUsingCallSyntax(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsStoredFunctionsUsingCallSyntax); } @Override public boolean supportsStoredProcedures() throws SQLException { - try { - return databaseMetaData.supportsStoredProcedures(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsStoredProcedures); } - /* JDBC_4_ANT_KEY_BEGIN */ - @Override public boolean supportsSubqueriesInComparisons() throws SQLException { - try { - return databaseMetaData.supportsSubqueriesInComparisons(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSubqueriesInComparisons); } @Override public boolean supportsSubqueriesInExists() throws SQLException { - try { - return databaseMetaData.supportsSubqueriesInExists(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSubqueriesInExists); } @Override public boolean supportsSubqueriesInIns() throws SQLException { - try { - return databaseMetaData.supportsSubqueriesInIns(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSubqueriesInIns); } @Override public boolean supportsSubqueriesInQuantifieds() throws SQLException { - try { - return databaseMetaData.supportsSubqueriesInQuantifieds(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsSubqueriesInQuantifieds); } @Override public boolean supportsTableCorrelationNames() throws SQLException { - try { - return databaseMetaData.supportsTableCorrelationNames(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsTableCorrelationNames); } @Override public boolean supportsTransactionIsolationLevel(final int level) throws SQLException { - try { - return databaseMetaData.supportsTransactionIsolationLevel(level); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(databaseMetaData::supportsTransactionIsolationLevel, level, false); } @Override public boolean supportsTransactions() throws SQLException { - try { - return databaseMetaData.supportsTransactions(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsTransactions); } + /* JDBC_4_ANT_KEY_BEGIN */ + @Override public boolean supportsUnion() throws SQLException { - try { - return databaseMetaData.supportsUnion(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsUnion); } @Override public boolean supportsUnionAll() throws SQLException { - try { - return databaseMetaData.supportsUnionAll(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::supportsUnionAll); + } + + private ResultSet toResultSet(final SQLFunction0 callableResultSet) throws SQLException { + connection.checkOpen(); + return apply(() -> DelegatingResultSet.wrapResultSet(connection, callableResultSet.apply())); + } + + private ResultSet toResultSet(final SQLFunction2 callableResultSet, final T t, final U u) + throws SQLException { + connection.checkOpen(); + return apply(() -> DelegatingResultSet.wrapResultSet(connection, callableResultSet.apply(t, u))); + } + + private ResultSet toResultSet(final SQLFunction3 callableResultSet, final T t, + final U u, final V v) throws SQLException { + connection.checkOpen(); + return apply(() -> DelegatingResultSet.wrapResultSet(connection, callableResultSet.apply(t, u, v))); + } + + private ResultSet toResultSet(final SQLFunction4 callableResultSet, final T t, + final U u, final V v, final X x) throws SQLException { + connection.checkOpen(); + return apply(() -> DelegatingResultSet.wrapResultSet(connection, callableResultSet.apply(t, u, v, x))); + } + + private ResultSet toResultSet(final SQLFunction5 callableResultSet, + final T t, final U u, final V v, final X x, final Y y) throws SQLException { + connection.checkOpen(); + return apply(() -> DelegatingResultSet.wrapResultSet(connection, callableResultSet.apply(t, u, v, x, y))); + } + + private ResultSet toResultSet(final SQLFunction6 callableResultSet, + final T t, final U u, final V v, final X x, final Y y, final Z z) throws SQLException { + connection.checkOpen(); + return apply(() -> DelegatingResultSet.wrapResultSet(connection, callableResultSet.apply(t, u, v, x, y, z))); } /* JDBC_4_ANT_KEY_END */ @@ -1913,31 +1062,16 @@ public T unwrap(final Class iface) throws SQLException { @Override public boolean updatesAreDetected(final int type) throws SQLException { - try { - return databaseMetaData.updatesAreDetected(type); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(databaseMetaData::updatesAreDetected, type, false); } @Override public boolean usesLocalFilePerTable() throws SQLException { - try { - return databaseMetaData.usesLocalFilePerTable(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::usesLocalFilePerTable); } @Override public boolean usesLocalFiles() throws SQLException { - try { - return databaseMetaData.usesLocalFiles(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(databaseMetaData::usesLocalFiles); } } diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingPreparedStatement.java b/src/main/java/org/apache/commons/dbcp2/DelegatingPreparedStatement.java index 048687c05..d32baa84a 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingPreparedStatement.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingPreparedStatement.java @@ -43,9 +43,11 @@ *

* All of the methods from the {@link PreparedStatement} interface simply check to see that the * {@link PreparedStatement} is active, and call the corresponding method on the "delegate" provided in my constructor. + *

*

* Extends AbandonedTrace to implement Statement tracking and logging of code which created the Statement. Tracking the * Statement ensures that the Connection which created it can close any open Statement's on Connection close. + *

* * @since 2.0 */ @@ -55,10 +57,8 @@ public class DelegatingPreparedStatement extends DelegatingStatement implements * Create a wrapper for the Statement which traces this Statement to the Connection which created it and the code * which created it. * - * @param statement - * the {@link PreparedStatement} to delegate all calls to. - * @param connection - * the {@link DelegatingConnection} that created this statement. + * @param statement the {@link PreparedStatement} to delegate all calls to. + * @param connection the {@link DelegatingConnection} that created this statement. */ public DelegatingPreparedStatement(final DelegatingConnection connection, final PreparedStatement statement) { super(connection, statement); @@ -66,36 +66,17 @@ public DelegatingPreparedStatement(final DelegatingConnection connection, fin @Override public void addBatch() throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().addBatch(); - } catch (final SQLException e) { - handleException(e); - } + accept(PreparedStatement::addBatch, getDelegatePreparedStatement()); } @Override public void clearParameters() throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().clearParameters(); - } catch (final SQLException e) { - handleException(e); - } + accept(PreparedStatement::clearParameters, getDelegatePreparedStatement()); } @Override public boolean execute() throws SQLException { - checkOpen(); - if (getConnectionInternal() != null) { - getConnectionInternal().setLastUsed(); - } - try { - return getDelegatePreparedStatement().execute(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(PreparedStatement::execute, getDelegatePreparedStatement(), false); } /** @@ -103,41 +84,17 @@ public boolean execute() throws SQLException { */ @Override public long executeLargeUpdate() throws SQLException { - checkOpen(); - try { - return getDelegatePreparedStatement().executeLargeUpdate(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(PreparedStatement::executeLargeUpdate, getDelegatePreparedStatement(), 0L); } @Override public ResultSet executeQuery() throws SQLException { - checkOpen(); - if (getConnectionInternal() != null) { - getConnectionInternal().setLastUsed(); - } - try { - return DelegatingResultSet.wrapResultSet(this, getDelegatePreparedStatement().executeQuery()); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(() -> DelegatingResultSet.wrapResultSet(this, getDelegatePreparedStatement().executeQuery())); } @Override public int executeUpdate() throws SQLException { - checkOpen(); - if (getConnectionInternal() != null) { - getConnectionInternal().setLastUsed(); - } - try { - return getDelegatePreparedStatement().executeUpdate(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(PreparedStatement::executeUpdate, getDelegatePreparedStatement(), 0); } private PreparedStatement getDelegatePreparedStatement() { @@ -146,539 +103,283 @@ private PreparedStatement getDelegatePreparedStatement() { @Override public ResultSetMetaData getMetaData() throws SQLException { - checkOpen(); - try { - return getDelegatePreparedStatement().getMetaData(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(PreparedStatement::getMetaData, getDelegatePreparedStatement()); } @Override public java.sql.ParameterMetaData getParameterMetaData() throws SQLException { - checkOpen(); - try { - return getDelegatePreparedStatement().getParameterMetaData(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(PreparedStatement::getParameterMetaData, getDelegatePreparedStatement()); } @Override - public void setArray(final int i, final Array x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setArray(i, x); - } catch (final SQLException e) { - handleException(e); - } + public void setArray(final int inputStream, final Array value) throws SQLException { + accept(PreparedStatement::setArray, getDelegatePreparedStatement(), inputStream, value); } @Override - public void setAsciiStream(final int parameterIndex, final InputStream inputStream) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setAsciiStream(parameterIndex, inputStream); - } catch (final SQLException e) { - handleException(e); - } + public void setAsciiStream(final int parameterIndex, final InputStream value) throws SQLException { + accept(PreparedStatement::setAsciiStream, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setAsciiStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setAsciiStream(parameterIndex, x, length); - } catch (final SQLException e) { - handleException(e); - } + public void setAsciiStream(final int parameterIndex, final InputStream value, final int length) + throws SQLException { + accept(PreparedStatement::setAsciiStream, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override - public void setAsciiStream(final int parameterIndex, final InputStream inputStream, final long length) - throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setAsciiStream(parameterIndex, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + public void setAsciiStream(final int parameterIndex, final InputStream value, final long length) + throws SQLException { + accept(PreparedStatement::setAsciiStream, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override - public void setBigDecimal(final int parameterIndex, final BigDecimal x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setBigDecimal(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setBigDecimal(final int parameterIndex, final BigDecimal value) throws SQLException { + accept(PreparedStatement::setBigDecimal, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setBinaryStream(final int parameterIndex, final InputStream inputStream) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setBinaryStream(parameterIndex, inputStream); - } catch (final SQLException e) { - handleException(e); - } + public void setBinaryStream(final int parameterIndex, final InputStream value) throws SQLException { + accept(PreparedStatement::setBinaryStream, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setBinaryStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setBinaryStream(parameterIndex, x, length); - } catch (final SQLException e) { - handleException(e); - } + public void setBinaryStream(final int parameterIndex, final InputStream value, final int length) + throws SQLException { + accept(PreparedStatement::setBinaryStream, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override - public void setBinaryStream(final int parameterIndex, final InputStream inputStream, final long length) - throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setBinaryStream(parameterIndex, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + public void setBinaryStream(final int parameterIndex, final InputStream value, final long length) + throws SQLException { + accept(PreparedStatement::setBinaryStream, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override - public void setBlob(final int i, final Blob x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setBlob(i, x); - } catch (final SQLException e) { - handleException(e); - } + public void setBlob(final int parameterIndex, final Blob value) throws SQLException { + accept(PreparedStatement::setBlob, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setBlob(final int parameterIndex, final InputStream inputStream) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setBlob(parameterIndex, inputStream); - } catch (final SQLException e) { - handleException(e); - } + public void setBlob(final int parameterIndex, final InputStream value) throws SQLException { + accept(PreparedStatement::setBlob, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setBlob(final int parameterIndex, final InputStream inputStream, final long length) - throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setBlob(parameterIndex, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + public void setBlob(final int parameterIndex, final InputStream value, final long length) throws SQLException { + accept(PreparedStatement::setBlob, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override - public void setBoolean(final int parameterIndex, final boolean x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setBoolean(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setBoolean(final int parameterIndex, final boolean value) throws SQLException { + accept(PreparedStatement::setBoolean, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setByte(final int parameterIndex, final byte x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setByte(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setByte(final int parameterIndex, final byte value) throws SQLException { + accept(PreparedStatement::setByte, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setBytes(final int parameterIndex, final byte[] x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setBytes(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setBytes(final int parameterIndex, final byte[] value) throws SQLException { + accept(PreparedStatement::setBytes, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setCharacterStream(final int parameterIndex, final Reader reader) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader); - } catch (final SQLException e) { - handleException(e); - } + public void setCharacterStream(final int parameterIndex, final Reader value) throws SQLException { + accept(PreparedStatement::setCharacterStream, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setCharacterStream(final int parameterIndex, final Reader reader, final int length) - throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader, length); - } catch (final SQLException e) { - handleException(e); - } + public void setCharacterStream(final int parameterIndex, final Reader value, final int length) throws SQLException { + accept(PreparedStatement::setCharacterStream, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override - public void setCharacterStream(final int parameterIndex, final Reader reader, final long length) - throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setCharacterStream(parameterIndex, reader, length); - } catch (final SQLException e) { - handleException(e); - } + public void setCharacterStream(final int parameterIndex, final Reader value, final long length) + throws SQLException { + accept(PreparedStatement::setCharacterStream, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override - public void setClob(final int i, final Clob x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setClob(i, x); - } catch (final SQLException e) { - handleException(e); - } + public void setClob(final int parameterIndex, final Clob value) throws SQLException { + accept(PreparedStatement::setClob, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setClob(final int parameterIndex, final Reader reader) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setClob(parameterIndex, reader); - } catch (final SQLException e) { - handleException(e); - } + public void setClob(final int parameterIndex, final Reader value) throws SQLException { + accept(PreparedStatement::setClob, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setClob(final int parameterIndex, final Reader reader, final long length) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setClob(parameterIndex, reader, length); - } catch (final SQLException e) { - handleException(e); - } + public void setClob(final int parameterIndex, final Reader value, final long length) throws SQLException { + accept(PreparedStatement::setClob, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override - public void setDate(final int parameterIndex, final Date x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setDate(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setDate(final int parameterIndex, final Date value) throws SQLException { + accept(PreparedStatement::setDate, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setDate(final int parameterIndex, final Date x, final Calendar cal) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setDate(parameterIndex, x, cal); - } catch (final SQLException e) { - handleException(e); - } + public void setDate(final int parameterIndex, final Date value, final Calendar calendar) throws SQLException { + accept(PreparedStatement::setDate, getDelegatePreparedStatement(), parameterIndex, value, calendar); } @Override - public void setDouble(final int parameterIndex, final double x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setDouble(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setDouble(final int parameterIndex, final double value) throws SQLException { + accept(PreparedStatement::setDouble, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setFloat(final int parameterIndex, final float x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setFloat(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setFloat(final int parameterIndex, final float value) throws SQLException { + accept(PreparedStatement::setFloat, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setInt(final int parameterIndex, final int x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setInt(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setInt(final int parameterIndex, final int value) throws SQLException { + accept(PreparedStatement::setInt, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setLong(final int parameterIndex, final long x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setLong(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setLong(final int parameterIndex, final long value) throws SQLException { + accept(PreparedStatement::setLong, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setNCharacterStream(final int parameterIndex, final Reader reader) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setNCharacterStream(parameterIndex, reader); - } catch (final SQLException e) { - handleException(e); - } + public void setNCharacterStream(final int parameterIndex, final Reader value) throws SQLException { + accept(PreparedStatement::setNCharacterStream, getDelegatePreparedStatement(), parameterIndex, value); } @Override public void setNCharacterStream(final int parameterIndex, final Reader value, final long length) - throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setNCharacterStream(parameterIndex, value, length); - } catch (final SQLException e) { - handleException(e); - } + throws SQLException { + accept(PreparedStatement::setNCharacterStream, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override public void setNClob(final int parameterIndex, final NClob value) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setNClob(parameterIndex, value); - } catch (final SQLException e) { - handleException(e); - } + accept(PreparedStatement::setNClob, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setNClob(final int parameterIndex, final Reader reader) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setNClob(parameterIndex, reader); - } catch (final SQLException e) { - handleException(e); - } + public void setNClob(final int parameterIndex, final Reader value) throws SQLException { + accept(PreparedStatement::setNClob, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setNClob(final int parameterIndex, final Reader reader, final long length) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setNClob(parameterIndex, reader, length); - } catch (final SQLException e) { - handleException(e); - } + public void setNClob(final int parameterIndex, final Reader value, final long length) throws SQLException { + accept(PreparedStatement::setNClob, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override public void setNString(final int parameterIndex, final String value) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setNString(parameterIndex, value); - } catch (final SQLException e) { - handleException(e); - } + accept(PreparedStatement::setNString, getDelegatePreparedStatement(), parameterIndex, value); } @Override public void setNull(final int parameterIndex, final int sqlType) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setNull(parameterIndex, sqlType); - } catch (final SQLException e) { - handleException(e); - } + accept(PreparedStatement::setNull, getDelegatePreparedStatement(), parameterIndex, sqlType); } @Override - public void setNull(final int paramIndex, final int sqlType, final String typeName) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setNull(paramIndex, sqlType, typeName); - } catch (final SQLException e) { - handleException(e); - } + public void setNull(final int parameterIndex, final int sqlType, final String typeName) throws SQLException { + accept(PreparedStatement::setNull, getDelegatePreparedStatement(), parameterIndex, sqlType, typeName); } @Override - public void setObject(final int parameterIndex, final Object x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setObject(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setObject(final int parameterIndex, final Object value) throws SQLException { + accept(PreparedStatement::setObject, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setObject(final int parameterIndex, final Object x, final int targetSqlType) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType); - } catch (final SQLException e) { - handleException(e); - } + public void setObject(final int parameterIndex, final Object value, final int targetSqlType) throws SQLException { + accept(PreparedStatement::setObject, getDelegatePreparedStatement(), parameterIndex, value, targetSqlType); } @Override - public void setObject(final int parameterIndex, final Object x, final int targetSqlType, final int scale) - throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType, scale); - } catch (final SQLException e) { - handleException(e); - } + public void setObject(final int parameterIndex, final Object value, final int targetSqlType, final int scale) + throws SQLException { + accept(PreparedStatement::setObject, getDelegatePreparedStatement(), parameterIndex, value, targetSqlType, + scale); } /** * @since 2.5.0 */ @Override - public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType); - } catch (final SQLException e) { - handleException(e); - } + public void setObject(final int parameterIndex, final Object value, final SQLType targetSqlType) + throws SQLException { + accept(PreparedStatement::setObject, getDelegatePreparedStatement(), parameterIndex, value, targetSqlType); } /** * @since 2.5.0 */ @Override - public void setObject(final int parameterIndex, final Object x, final SQLType targetSqlType, final int scaleOrLength) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setObject(parameterIndex, x, targetSqlType, scaleOrLength); - } catch (final SQLException e) { - handleException(e); - } + public void setObject(final int parameterIndex, final Object value, final SQLType targetSqlType, + final int scaleOrLength) throws SQLException { + accept(PreparedStatement::setObject, getDelegatePreparedStatement(), parameterIndex, value, targetSqlType, + scaleOrLength); } @Override - public void setRef(final int i, final Ref x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setRef(i, x); - } catch (final SQLException e) { - handleException(e); - } + public void setRef(final int parameterIndex, final Ref value) throws SQLException { + accept(PreparedStatement::setRef, getDelegatePreparedStatement(), parameterIndex, value); } @Override public void setRowId(final int parameterIndex, final RowId value) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setRowId(parameterIndex, value); - } catch (final SQLException e) { - handleException(e); - } + accept(PreparedStatement::setRowId, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setShort(final int parameterIndex, final short x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setShort(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setShort(final int parameterIndex, final short value) throws SQLException { + accept(PreparedStatement::setShort, getDelegatePreparedStatement(), parameterIndex, value); } @Override public void setSQLXML(final int parameterIndex, final SQLXML value) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setSQLXML(parameterIndex, value); - } catch (final SQLException e) { - handleException(e); - } + accept(PreparedStatement::setSQLXML, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setString(final int parameterIndex, final String x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setString(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setString(final int parameterIndex, final String value) throws SQLException { + accept(PreparedStatement::setString, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setTime(final int parameterIndex, final Time x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setTime(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setTime(final int parameterIndex, final Time value) throws SQLException { + accept(PreparedStatement::setTime, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setTime(final int parameterIndex, final Time x, final Calendar cal) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setTime(parameterIndex, x, cal); - } catch (final SQLException e) { - handleException(e); - } + public void setTime(final int parameterIndex, final Time value, final Calendar calendar) throws SQLException { + accept(PreparedStatement::setTime, getDelegatePreparedStatement(), parameterIndex, value, calendar); } @Override - public void setTimestamp(final int parameterIndex, final Timestamp x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setTimestamp(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setTimestamp(final int parameterIndex, final Timestamp value) throws SQLException { + accept(PreparedStatement::setTimestamp, getDelegatePreparedStatement(), parameterIndex, value); } @Override - public void setTimestamp(final int parameterIndex, final Timestamp x, final Calendar cal) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setTimestamp(parameterIndex, x, cal); - } catch (final SQLException e) { - handleException(e); - } + public void setTimestamp(final int parameterIndex, final Timestamp value, final Calendar calendar) + throws SQLException { + accept(PreparedStatement::setTimestamp, getDelegatePreparedStatement(), parameterIndex, value, calendar); } /** @deprecated Use setAsciiStream(), setCharacterStream() or setNCharacterStream() */ @Deprecated @Override - public void setUnicodeStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setUnicodeStream(parameterIndex, x, length); - } catch (final SQLException e) { - handleException(e); - } + public void setUnicodeStream(final int parameterIndex, final InputStream value, final int length) + throws SQLException { + accept(PreparedStatement::setUnicodeStream, getDelegatePreparedStatement(), parameterIndex, value, length); } @Override - public void setURL(final int parameterIndex, final java.net.URL x) throws SQLException { - checkOpen(); - try { - getDelegatePreparedStatement().setURL(parameterIndex, x); - } catch (final SQLException e) { - handleException(e); - } + public void setURL(final int parameterIndex, final java.net.URL value) throws SQLException { + accept(PreparedStatement::setURL, getDelegatePreparedStatement(), parameterIndex, value); } /** diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingResultSet.java b/src/main/java/org/apache/commons/dbcp2/DelegatingResultSet.java index 86e4d43ee..875db1fc0 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingResultSet.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingResultSet.java @@ -60,8 +60,7 @@ public final class DelegatingResultSet extends AbandonedTrace implements ResultS * * @param connection * The Connection which created the ResultSet. - * @param resultSet - * The ResultSet to wrap. + * @param resultSet The ResultSet to wrap. * @return a new delegate. */ public static ResultSet wrapResultSet(final Connection connection, final ResultSet resultSet) { @@ -74,10 +73,8 @@ public static ResultSet wrapResultSet(final Connection connection, final ResultS /** * Wraps the given result set in a delegate. * - * @param statement - * The Statement which created the ResultSet. - * @param resultSet - * The ResultSet to wrap. + * @param statement The Statement which created the ResultSet. + * @param resultSet The ResultSet to wrap. * @return a new delegate. */ public static ResultSet wrapResultSet(final Statement statement, final ResultSet resultSet) { @@ -103,10 +100,8 @@ public static ResultSet wrapResultSet(final Statement statement, final ResultSet * Private to ensure all construction is {@link #wrapResultSet(Connection, ResultSet)} *

* - * @param conn - * Connection which created this ResultSet - * @param res - * ResultSet to wrap + * @param conn Connection which created this ResultSet + * @param res ResultSet to wrap */ private DelegatingResultSet(final Connection conn, final ResultSet res) { super((AbandonedTrace) conn); @@ -121,10 +116,8 @@ private DelegatingResultSet(final Connection conn, final ResultSet res) { * Private to ensure all construction is {@link #wrapResultSet(Statement, ResultSet)} *

* - * @param statement - * The Statement which created the ResultSet. - * @param resultSet - * The ResultSet to wrap. + * @param statement The Statement which created the ResultSet. + * @param resultSet The ResultSet to wrap. */ private DelegatingResultSet(final Statement statement, final ResultSet resultSet) { super((AbandonedTrace) statement); @@ -134,48 +127,27 @@ private DelegatingResultSet(final Statement statement, final ResultSet resultSet @Override public boolean absolute(final int row) throws SQLException { - try { - return resultSet.absolute(row); - } catch (final SQLException e) { - handleException(e); - return false; - } + return apply(resultSet::absolute, row); } @Override public void afterLast() throws SQLException { - try { - resultSet.afterLast(); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::afterLast); } @Override public void beforeFirst() throws SQLException { - try { - resultSet.beforeFirst(); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::beforeFirst); } @Override public void cancelRowUpdates() throws SQLException { - try { - resultSet.cancelRowUpdates(); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::cancelRowUpdates); } @Override public void clearWarnings() throws SQLException { - try { - resultSet.clearWarnings(); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::clearWarnings); } /** @@ -201,315 +173,161 @@ public void close() throws SQLException { @Override public void deleteRow() throws SQLException { - try { - resultSet.deleteRow(); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::deleteRow); } @Override - public int findColumn(final String columnName) throws SQLException { - try { - return resultSet.findColumn(columnName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + public int findColumn(final String columnLabel) throws SQLException { + return apply(resultSet::findColumn, columnLabel); } @Override public boolean first() throws SQLException { - try { - return resultSet.first(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::first); } @Override - public Array getArray(final int i) throws SQLException { - try { - return resultSet.getArray(i); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Array getArray(final int columnIndex) throws SQLException { + return apply(resultSet::getArray, columnIndex); } @Override - public Array getArray(final String colName) throws SQLException { - try { - return resultSet.getArray(colName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Array getArray(final String columnLabel) throws SQLException { + return apply(resultSet::getArray, columnLabel); } @Override public InputStream getAsciiStream(final int columnIndex) throws SQLException { - try { - return resultSet.getAsciiStream(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getAsciiStream, columnIndex); } @Override - public InputStream getAsciiStream(final String columnName) throws SQLException { - try { - return resultSet.getAsciiStream(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public InputStream getAsciiStream(final String columnLabel) throws SQLException { + return apply(resultSet::getAsciiStream, columnLabel); } @Override public BigDecimal getBigDecimal(final int columnIndex) throws SQLException { - try { - return resultSet.getBigDecimal(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getBigDecimal, columnIndex); } /** @deprecated Use {@link #getBigDecimal(int)} */ @Deprecated @Override public BigDecimal getBigDecimal(final int columnIndex, final int scale) throws SQLException { - try { - return resultSet.getBigDecimal(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getBigDecimal, columnIndex, scale); } @Override - public BigDecimal getBigDecimal(final String columnName) throws SQLException { - try { - return resultSet.getBigDecimal(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public BigDecimal getBigDecimal(final String columnLabel) throws SQLException { + return apply(resultSet::getBigDecimal, columnLabel); } /** @deprecated Use {@link #getBigDecimal(String)} */ @Deprecated @Override - public BigDecimal getBigDecimal(final String columnName, final int scale) throws SQLException { - try { - return resultSet.getBigDecimal(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public BigDecimal getBigDecimal(final String columnLabel, final int scale) throws SQLException { + return apply(resultSet::getBigDecimal, columnLabel, scale); } @Override public InputStream getBinaryStream(final int columnIndex) throws SQLException { - try { - return resultSet.getBinaryStream(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getBinaryStream, columnIndex); } @Override - public InputStream getBinaryStream(final String columnName) throws SQLException { - try { - return resultSet.getBinaryStream(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public InputStream getBinaryStream(final String columnLabel) throws SQLException { + return apply(resultSet::getBinaryStream, columnLabel); } @Override - public Blob getBlob(final int i) throws SQLException { - try { - return resultSet.getBlob(i); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Blob getBlob(final int columnIndex) throws SQLException { + return apply(resultSet::getBlob, columnIndex); } @Override - public Blob getBlob(final String colName) throws SQLException { - try { - return resultSet.getBlob(colName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Blob getBlob(final String columnLabel) throws SQLException { + return apply(resultSet::getBlob, columnLabel); } @Override public boolean getBoolean(final int columnIndex) throws SQLException { - try { - return resultSet.getBoolean(columnIndex); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(resultSet::getBoolean, columnIndex, false); } @Override - public boolean getBoolean(final String columnName) throws SQLException { - try { - return resultSet.getBoolean(columnName); - } catch (final SQLException e) { - handleException(e); - return false; - } + public boolean getBoolean(final String columnLabel) throws SQLException { + return applyTo(resultSet::getBoolean, columnLabel, false); } @Override public byte getByte(final int columnIndex) throws SQLException { - try { - return resultSet.getByte(columnIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(resultSet::getByte, columnIndex, (byte) 0); } @Override - public byte getByte(final String columnName) throws SQLException { - try { - return resultSet.getByte(columnName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + public byte getByte(final String columnLabel) throws SQLException { + return applyTo(resultSet::getByte, columnLabel, (byte) 0); } @Override public byte[] getBytes(final int columnIndex) throws SQLException { - try { - return resultSet.getBytes(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getBytes, columnIndex); } @Override - public byte[] getBytes(final String columnName) throws SQLException { - try { - return resultSet.getBytes(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public byte[] getBytes(final String columnLabel) throws SQLException { + return apply(resultSet::getBytes, columnLabel); } @Override public Reader getCharacterStream(final int columnIndex) throws SQLException { - try { - return resultSet.getCharacterStream(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getCharacterStream, columnIndex); } @Override - public Reader getCharacterStream(final String columnName) throws SQLException { - try { - return resultSet.getCharacterStream(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Reader getCharacterStream(final String columnLabel) throws SQLException { + return apply(resultSet::getCharacterStream, columnLabel); } @Override - public Clob getClob(final int i) throws SQLException { - try { - return resultSet.getClob(i); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Clob getClob(final int columnIndex) throws SQLException { + return apply(resultSet::getClob, columnIndex); } @Override - public Clob getClob(final String colName) throws SQLException { - try { - return resultSet.getClob(colName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Clob getClob(final String columnLabel) throws SQLException { + return apply(resultSet::getClob, columnLabel); } @Override public int getConcurrency() throws SQLException { - try { - return resultSet.getConcurrency(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo0(resultSet::getConcurrency); } @Override public String getCursorName() throws SQLException { - try { - return resultSet.getCursorName(); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getCursorName); } @Override public Date getDate(final int columnIndex) throws SQLException { - try { - return resultSet.getDate(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getDate, columnIndex); } @Override public Date getDate(final int columnIndex, final Calendar cal) throws SQLException { - try { - return resultSet.getDate(columnIndex, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getDate, columnIndex, cal); } @Override - public Date getDate(final String columnName) throws SQLException { - try { - return resultSet.getDate(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Date getDate(final String columnLabel) throws SQLException { + return apply(resultSet::getDate, columnLabel); } @Override - public Date getDate(final String columnName, final Calendar cal) throws SQLException { - try { - return resultSet.getDate(columnName, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Date getDate(final String columnLabel, final Calendar cal) throws SQLException { + return apply(resultSet::getDate, columnLabel, cal); } /** @@ -523,72 +341,38 @@ public ResultSet getDelegate() { @Override public double getDouble(final int columnIndex) throws SQLException { - try { - return resultSet.getDouble(columnIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(resultSet::getDouble, columnIndex, 0d); } @Override - public double getDouble(final String columnName) throws SQLException { - try { - return resultSet.getDouble(columnName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + public double getDouble(final String columnLabel) throws SQLException { + return applyTo(resultSet::getDouble, columnLabel, 0d); } @Override public int getFetchDirection() throws SQLException { - try { - return resultSet.getFetchDirection(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return getAsInt(resultSet::getFetchDirection); + } @Override public int getFetchSize() throws SQLException { - try { - return resultSet.getFetchSize(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return getAsInt(resultSet::getFetchSize); } @Override public float getFloat(final int columnIndex) throws SQLException { - try { - return resultSet.getFloat(columnIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(resultSet::getFloat, columnIndex, 0f); } @Override - public float getFloat(final String columnName) throws SQLException { - try { - return resultSet.getFloat(columnName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + public float getFloat(final String columnLabel) throws SQLException { + return applyTo(resultSet::getFloat, columnLabel, 0f); } @Override public int getHoldability() throws SQLException { - try { - return resultSet.getHoldability(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return getAsInt(resultSet::getHoldability); } /** @@ -619,262 +403,132 @@ public ResultSet getInnermostDelegate() { @Override public int getInt(final int columnIndex) throws SQLException { - try { - return resultSet.getInt(columnIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyIntTo(resultSet::getInt, columnIndex, 0); } @Override - public int getInt(final String columnName) throws SQLException { - try { - return resultSet.getInt(columnName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + public int getInt(final String columnLabel) throws SQLException { + return applyTo(resultSet::getInt, columnLabel, 0); } @Override public long getLong(final int columnIndex) throws SQLException { - try { - return resultSet.getLong(columnIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyIntTo(resultSet::getLong, columnIndex, 0L); } @Override - public long getLong(final String columnName) throws SQLException { - try { - return resultSet.getLong(columnName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + public long getLong(final String columnLabel) throws SQLException { + return applyTo(resultSet::getLong, columnLabel, 0L); } @Override public ResultSetMetaData getMetaData() throws SQLException { - try { - return resultSet.getMetaData(); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getMetaData); } @Override public Reader getNCharacterStream(final int columnIndex) throws SQLException { - try { - return resultSet.getNCharacterStream(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getNCharacterStream, columnIndex); } @Override public Reader getNCharacterStream(final String columnLabel) throws SQLException { - try { - return resultSet.getNCharacterStream(columnLabel); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getNCharacterStream, columnLabel); } @Override public NClob getNClob(final int columnIndex) throws SQLException { - try { - return resultSet.getNClob(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getNClob, columnIndex); } @Override public NClob getNClob(final String columnLabel) throws SQLException { - try { - return resultSet.getNClob(columnLabel); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getNClob, columnLabel); } @Override public String getNString(final int columnIndex) throws SQLException { - try { - return resultSet.getNString(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getNString, columnIndex); } @Override public String getNString(final String columnLabel) throws SQLException { - try { - return resultSet.getNString(columnLabel); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getNString, columnLabel); } @Override public Object getObject(final int columnIndex) throws SQLException { - try { - return resultSet.getObject(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getObject, columnIndex); } @Override public T getObject(final int columnIndex, final Class type) throws SQLException { - try { - return Jdbc41Bridge.getObject(resultSet, columnIndex, type); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(Jdbc41Bridge::getObject, resultSet, columnIndex, type); } @Override public Object getObject(final int i, final Map> map) throws SQLException { - try { - return resultSet.getObject(i, map); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getObject, i, map); } @Override - public Object getObject(final String columnName) throws SQLException { - try { - return resultSet.getObject(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Object getObject(final String columnLabel) throws SQLException { + return apply(resultSet::getObject, columnLabel); } @Override public T getObject(final String columnLabel, final Class type) throws SQLException { - try { - return Jdbc41Bridge.getObject(resultSet, columnLabel, type); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(Jdbc41Bridge::getObject, resultSet, columnLabel, type); } @Override - public Object getObject(final String colName, final Map> map) throws SQLException { - try { - return resultSet.getObject(colName, map); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Object getObject(final String columnLabel, final Map> map) throws SQLException { + return apply(resultSet::getObject, columnLabel, map); } @Override public Ref getRef(final int i) throws SQLException { - try { - return resultSet.getRef(i); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getRef, i); } @Override - public Ref getRef(final String colName) throws SQLException { - try { - return resultSet.getRef(colName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Ref getRef(final String columnLabel) throws SQLException { + return apply(resultSet::getRef, columnLabel); } @Override public int getRow() throws SQLException { - try { - return resultSet.getRow(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return getAsInt(resultSet::getRow); } @Override public RowId getRowId(final int columnIndex) throws SQLException { - try { - return resultSet.getRowId(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getRowId, columnIndex); } @Override public RowId getRowId(final String columnLabel) throws SQLException { - try { - return resultSet.getRowId(columnLabel); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getRowId, columnLabel); } @Override public short getShort(final int columnIndex) throws SQLException { - try { - return resultSet.getShort(columnIndex); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(resultSet::getShort, columnIndex, (short) 0); } @Override - public short getShort(final String columnName) throws SQLException { - try { - return resultSet.getShort(columnName); - } catch (final SQLException e) { - handleException(e); - return 0; - } + public short getShort(final String columnLabel) throws SQLException { + return applyTo(resultSet::getShort, columnLabel, (short) 0); } @Override public SQLXML getSQLXML(final int columnIndex) throws SQLException { - try { - return resultSet.getSQLXML(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getSQLXML, columnIndex); } - @Override - public SQLXML getSQLXML(final String columnLabel) throws SQLException { - try { - return resultSet.getSQLXML(columnLabel); - } catch (final SQLException e) { - handleException(e); - return null; - } + @Override + public SQLXML getSQLXML(final String columnLabel) throws SQLException { + return apply(resultSet::getSQLXML, columnLabel); } @Override @@ -884,168 +538,89 @@ public Statement getStatement() throws SQLException { @Override public String getString(final int columnIndex) throws SQLException { - try { - return resultSet.getString(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getString, columnIndex); } @Override - public String getString(final String columnName) throws SQLException { - try { - return resultSet.getString(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public String getString(final String columnLabel) throws SQLException { + return apply(resultSet::getString, columnLabel); } @Override public Time getTime(final int columnIndex) throws SQLException { - try { - return resultSet.getTime(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getTime, columnIndex); } @Override public Time getTime(final int columnIndex, final Calendar cal) throws SQLException { - try { - return resultSet.getTime(columnIndex, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getTime, columnIndex, cal); } @Override - public Time getTime(final String columnName) throws SQLException { - try { - return resultSet.getTime(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Time getTime(final String columnLabel) throws SQLException { + return apply(resultSet::getTime, columnLabel); } @Override - public Time getTime(final String columnName, final Calendar cal) throws SQLException { - try { - return resultSet.getTime(columnName, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Time getTime(final String columnLabel, final Calendar cal) throws SQLException { + return apply(resultSet::getTime, columnLabel, cal); } @Override public Timestamp getTimestamp(final int columnIndex) throws SQLException { - try { - return resultSet.getTimestamp(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getTimestamp, columnIndex); } @Override public Timestamp getTimestamp(final int columnIndex, final Calendar cal) throws SQLException { - try { - return resultSet.getTimestamp(columnIndex, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getTimestamp, columnIndex, cal); } @Override - public Timestamp getTimestamp(final String columnName) throws SQLException { - try { - return resultSet.getTimestamp(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Timestamp getTimestamp(final String columnLabel) throws SQLException { + return apply(resultSet::getTimestamp, columnLabel); } @Override - public Timestamp getTimestamp(final String columnName, final Calendar cal) throws SQLException { - try { - return resultSet.getTimestamp(columnName, cal); - } catch (final SQLException e) { - handleException(e); - return null; - } + public Timestamp getTimestamp(final String columnLabel, final Calendar cal) throws SQLException { + return apply(resultSet::getTimestamp, columnLabel, cal); } @Override public int getType() throws SQLException { - try { - return resultSet.getType(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return getAsInt(resultSet::getType); } /** @deprecated Use {@link #getCharacterStream(int)} */ @Deprecated @Override public InputStream getUnicodeStream(final int columnIndex) throws SQLException { - try { - return resultSet.getUnicodeStream(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getUnicodeStream, columnIndex); } /** @deprecated Use {@link #getCharacterStream(String)} */ @Deprecated @Override - public InputStream getUnicodeStream(final String columnName) throws SQLException { - try { - return resultSet.getUnicodeStream(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public InputStream getUnicodeStream(final String columnLabel) throws SQLException { + return apply(resultSet::getUnicodeStream, columnLabel); } @Override public java.net.URL getURL(final int columnIndex) throws SQLException { - try { - return resultSet.getURL(columnIndex); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getURL, columnIndex); } @Override - public java.net.URL getURL(final String columnName) throws SQLException { - try { - return resultSet.getURL(columnName); - } catch (final SQLException e) { - handleException(e); - return null; - } + public java.net.URL getURL(final String columnLabel) throws SQLException { + return apply(resultSet::getURL, columnLabel); } @Override public SQLWarning getWarnings() throws SQLException { - try { - return resultSet.getWarnings(); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(resultSet::getWarnings); } + @Override protected void handleException(final SQLException e) throws SQLException { if (statement != null && statement instanceof DelegatingStatement) { ((DelegatingStatement) statement).handleException(e); @@ -1058,61 +633,32 @@ protected void handleException(final SQLException e) throws SQLException { @Override public void insertRow() throws SQLException { - try { - resultSet.insertRow(); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::insertRow); } @Override public boolean isAfterLast() throws SQLException { - try { - return resultSet.isAfterLast(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::isAfterLast); } @Override public boolean isBeforeFirst() throws SQLException { - try { - return resultSet.isBeforeFirst(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::isBeforeFirst); } @Override public boolean isClosed() throws SQLException { - try { - return resultSet.isClosed(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::isClosed); } @Override public boolean isFirst() throws SQLException { - try { - return resultSet.isFirst(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::isFirst); } @Override public boolean isLast() throws SQLException { - try { - return resultSet.isLast(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::isLast); } @Override @@ -1128,122 +674,68 @@ public boolean isWrapperFor(final Class iface) throws SQLException { @Override public boolean last() throws SQLException { - try { - return resultSet.last(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::last); } @Override public void moveToCurrentRow() throws SQLException { - try { - resultSet.moveToCurrentRow(); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::moveToCurrentRow); } @Override public void moveToInsertRow() throws SQLException { - try { - resultSet.moveToInsertRow(); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::moveToInsertRow); } @Override public boolean next() throws SQLException { - try { - return resultSet.next(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::next); } @Override public boolean previous() throws SQLException { - try { - return resultSet.previous(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::previous); } @Override public void refreshRow() throws SQLException { - try { - resultSet.refreshRow(); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::refreshRow); } @Override public boolean relative(final int rows) throws SQLException { - try { - return resultSet.relative(rows); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyIntTo(resultSet::relative, rows, false); } @Override public boolean rowDeleted() throws SQLException { - try { - return resultSet.rowDeleted(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::rowDeleted); } @Override public boolean rowInserted() throws SQLException { - try { - return resultSet.rowInserted(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::rowInserted); } @Override public boolean rowUpdated() throws SQLException { - try { - return resultSet.rowUpdated(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return getAsBoolean(resultSet::rowUpdated); } @Override public void setFetchDirection(final int direction) throws SQLException { - try { - resultSet.setFetchDirection(direction); - } catch (final SQLException e) { - handleException(e); - } + acceptInt(resultSet::setFetchDirection, direction); } @Override public void setFetchSize(final int rows) throws SQLException { - try { - resultSet.setFetchSize(rows); - } catch (final SQLException e) { - handleException(e); - } + acceptInt(resultSet::setFetchSize, rows); } @Override public synchronized String toString() { - return super.toString() + "[resultSet=" + resultSet + ", statement=" + statement + ", connection=" + connection + "]"; + return super.toString() + "[resultSet=" + resultSet + ", statement=" + statement + ", connection=" + connection + + "]"; } @Override @@ -1259,607 +751,343 @@ public T unwrap(final Class iface) throws SQLException { @Override public void updateArray(final int columnIndex, final Array x) throws SQLException { - try { - resultSet.updateArray(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateArray, columnIndex, x); } @Override public void updateArray(final String columnName, final Array x) throws SQLException { - try { - resultSet.updateArray(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateArray, columnName, x); } @Override public void updateAsciiStream(final int columnIndex, final InputStream inputStream) throws SQLException { - try { - resultSet.updateAsciiStream(columnIndex, inputStream); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateAsciiStream, columnIndex, inputStream); } @Override public void updateAsciiStream(final int columnIndex, final InputStream x, final int length) throws SQLException { - try { - resultSet.updateAsciiStream(columnIndex, x, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateAsciiStream, columnIndex, x, length); } @Override public void updateAsciiStream(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { - try { - resultSet.updateAsciiStream(columnIndex, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateAsciiStream, columnIndex, inputStream, length); } @Override public void updateAsciiStream(final String columnLabel, final InputStream inputStream) throws SQLException { - try { - resultSet.updateAsciiStream(columnLabel, inputStream); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateAsciiStream, columnLabel, inputStream); } @Override public void updateAsciiStream(final String columnName, final InputStream x, final int length) throws SQLException { - try { - resultSet.updateAsciiStream(columnName, x, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateAsciiStream, columnName, x, length); } @Override public void updateAsciiStream(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { - try { - resultSet.updateAsciiStream(columnLabel, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateAsciiStream, columnLabel, inputStream, length); } @Override public void updateBigDecimal(final int columnIndex, final BigDecimal x) throws SQLException { - try { - resultSet.updateBigDecimal(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBigDecimal, columnIndex, x); } @Override public void updateBigDecimal(final String columnName, final BigDecimal x) throws SQLException { - try { - resultSet.updateBigDecimal(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBigDecimal, columnName, x); } @Override public void updateBinaryStream(final int columnIndex, final InputStream inputStream) throws SQLException { - try { - resultSet.updateBinaryStream(columnIndex, inputStream); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBinaryStream, columnIndex, inputStream); } @Override public void updateBinaryStream(final int columnIndex, final InputStream x, final int length) throws SQLException { - try { - resultSet.updateBinaryStream(columnIndex, x, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBinaryStream, columnIndex, x, length); } @Override public void updateBinaryStream(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { - try { - resultSet.updateBinaryStream(columnIndex, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBinaryStream, columnIndex, inputStream, length); } @Override public void updateBinaryStream(final String columnLabel, final InputStream inputStream) throws SQLException { - try { - resultSet.updateBinaryStream(columnLabel, inputStream); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBinaryStream, columnLabel, inputStream); } @Override public void updateBinaryStream(final String columnName, final InputStream x, final int length) throws SQLException { - try { - resultSet.updateBinaryStream(columnName, x, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBinaryStream, columnName, x, length); } @Override public void updateBinaryStream(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { - try { - resultSet.updateBinaryStream(columnLabel, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBinaryStream, columnLabel, inputStream, length); } @Override public void updateBlob(final int columnIndex, final Blob x) throws SQLException { - try { - resultSet.updateBlob(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBlob, columnIndex, x); } @Override public void updateBlob(final int columnIndex, final InputStream inputStream) throws SQLException { - try { - resultSet.updateBlob(columnIndex, inputStream); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBlob, columnIndex, inputStream); } @Override public void updateBlob(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { - try { - resultSet.updateBlob(columnIndex, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBlob, columnIndex, inputStream, length); } @Override public void updateBlob(final String columnName, final Blob x) throws SQLException { - try { - resultSet.updateBlob(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBlob, columnName, x); } @Override public void updateBlob(final String columnLabel, final InputStream inputStream) throws SQLException { - try { - resultSet.updateBlob(columnLabel, inputStream); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBlob, columnLabel, inputStream); } @Override public void updateBlob(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { - try { - resultSet.updateBlob(columnLabel, inputStream, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBlob, columnLabel, inputStream, length); } @Override public void updateBoolean(final int columnIndex, final boolean x) throws SQLException { - try { - resultSet.updateBoolean(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBoolean, columnIndex, x); } @Override public void updateBoolean(final String columnName, final boolean x) throws SQLException { - try { - resultSet.updateBoolean(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBoolean, columnName, x); } - @Override - public void updateByte(final int columnIndex, final byte x) throws SQLException { - try { - resultSet.updateByte(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + @Override + public void updateByte(final int columnIndex, final byte x) throws SQLException { + accept(resultSet::updateByte, columnIndex, x); } @Override public void updateByte(final String columnName, final byte x) throws SQLException { - try { - resultSet.updateByte(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateByte, columnName, x); } @Override public void updateBytes(final int columnIndex, final byte[] x) throws SQLException { - try { - resultSet.updateBytes(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBytes, columnIndex, x); } @Override public void updateBytes(final String columnName, final byte[] x) throws SQLException { - try { - resultSet.updateBytes(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateBytes, columnName, x); } @Override public void updateCharacterStream(final int columnIndex, final Reader reader) throws SQLException { - try { - resultSet.updateCharacterStream(columnIndex, reader); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateCharacterStream, columnIndex, reader); } @Override public void updateCharacterStream(final int columnIndex, final Reader x, final int length) throws SQLException { - try { - resultSet.updateCharacterStream(columnIndex, x, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateCharacterStream, columnIndex, x, length); } @Override public void updateCharacterStream(final int columnIndex, final Reader reader, final long length) throws SQLException { - try { - resultSet.updateCharacterStream(columnIndex, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateCharacterStream, columnIndex, reader, length); } @Override public void updateCharacterStream(final String columnLabel, final Reader reader) throws SQLException { - try { - resultSet.updateCharacterStream(columnLabel, reader); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateCharacterStream, columnLabel, reader); } @Override public void updateCharacterStream(final String columnName, final Reader reader, final int length) throws SQLException { - try { - resultSet.updateCharacterStream(columnName, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateCharacterStream, columnName, reader, length); } @Override public void updateCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { - try { - resultSet.updateCharacterStream(columnLabel, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateCharacterStream, columnLabel, reader, length); } @Override public void updateClob(final int columnIndex, final Clob x) throws SQLException { - try { - resultSet.updateClob(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateClob, columnIndex, x); } @Override public void updateClob(final int columnIndex, final Reader reader) throws SQLException { - try { - resultSet.updateClob(columnIndex, reader); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateClob, columnIndex, reader); } @Override public void updateClob(final int columnIndex, final Reader reader, final long length) throws SQLException { - try { - resultSet.updateClob(columnIndex, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateClob, columnIndex, reader, length); } @Override public void updateClob(final String columnName, final Clob x) throws SQLException { - try { - resultSet.updateClob(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateClob, columnName, x); } @Override public void updateClob(final String columnLabel, final Reader reader) throws SQLException { - try { - resultSet.updateClob(columnLabel, reader); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateClob, columnLabel, reader); } @Override public void updateClob(final String columnLabel, final Reader reader, final long length) throws SQLException { - try { - resultSet.updateClob(columnLabel, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateClob, columnLabel, reader, length); } @Override public void updateDate(final int columnIndex, final Date x) throws SQLException { - try { - resultSet.updateDate(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateDate, columnIndex, x); } @Override public void updateDate(final String columnName, final Date x) throws SQLException { - try { - resultSet.updateDate(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateDate, columnName, x); } @Override public void updateDouble(final int columnIndex, final double x) throws SQLException { - try { - resultSet.updateDouble(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateDouble, columnIndex, x); } @Override public void updateDouble(final String columnName, final double x) throws SQLException { - try { - resultSet.updateDouble(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateDouble, columnName, x); } @Override public void updateFloat(final int columnIndex, final float x) throws SQLException { - try { - resultSet.updateFloat(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateFloat, columnIndex, x); } @Override public void updateFloat(final String columnName, final float x) throws SQLException { - try { - resultSet.updateFloat(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateFloat, columnName, x); } @Override public void updateInt(final int columnIndex, final int x) throws SQLException { - try { - resultSet.updateInt(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateInt, columnIndex, x); } @Override public void updateInt(final String columnName, final int x) throws SQLException { - try { - resultSet.updateInt(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateInt, columnName, x); } @Override public void updateLong(final int columnIndex, final long x) throws SQLException { - try { - resultSet.updateLong(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateLong, columnIndex, x); } @Override public void updateLong(final String columnName, final long x) throws SQLException { - try { - resultSet.updateLong(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateLong, columnName, x); } @Override public void updateNCharacterStream(final int columnIndex, final Reader reader) throws SQLException { - try { - resultSet.updateNCharacterStream(columnIndex, reader); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNCharacterStream, columnIndex, reader); } @Override public void updateNCharacterStream(final int columnIndex, final Reader reader, final long length) throws SQLException { - try { - resultSet.updateNCharacterStream(columnIndex, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNCharacterStream, columnIndex, reader, length); } @Override public void updateNCharacterStream(final String columnLabel, final Reader reader) throws SQLException { - try { - resultSet.updateNCharacterStream(columnLabel, reader); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNCharacterStream, columnLabel, reader); } @Override public void updateNCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { - try { - resultSet.updateNCharacterStream(columnLabel, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNCharacterStream, columnLabel, reader, length); } @Override public void updateNClob(final int columnIndex, final NClob value) throws SQLException { - try { - resultSet.updateNClob(columnIndex, value); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNClob, columnIndex, value); } @Override public void updateNClob(final int columnIndex, final Reader reader) throws SQLException { - try { - resultSet.updateNClob(columnIndex, reader); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNClob, columnIndex, reader); } @Override public void updateNClob(final int columnIndex, final Reader reader, final long length) throws SQLException { - try { - resultSet.updateNClob(columnIndex, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNClob, columnIndex, reader, length); } @Override public void updateNClob(final String columnLabel, final NClob value) throws SQLException { - try { - resultSet.updateNClob(columnLabel, value); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNClob, columnLabel, value); } @Override public void updateNClob(final String columnLabel, final Reader reader) throws SQLException { - try { - resultSet.updateNClob(columnLabel, reader); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNClob, columnLabel, reader); } @Override public void updateNClob(final String columnLabel, final Reader reader, final long length) throws SQLException { - try { - resultSet.updateNClob(columnLabel, reader, length); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNClob, columnLabel, reader, length); } @Override public void updateNString(final int columnIndex, final String value) throws SQLException { - try { - resultSet.updateNString(columnIndex, value); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNString, columnIndex, value); } @Override public void updateNString(final String columnLabel, final String value) throws SQLException { - try { - resultSet.updateNString(columnLabel, value); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNString, columnLabel, value); } @Override public void updateNull(final int columnIndex) throws SQLException { - try { - resultSet.updateNull(columnIndex); - } catch (final SQLException e) { - handleException(e); - } + acceptInt(resultSet::updateNull, columnIndex); } @Override public void updateNull(final String columnName) throws SQLException { - try { - resultSet.updateNull(columnName); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateNull, columnName); } @Override public void updateObject(final int columnIndex, final Object x) throws SQLException { - try { - resultSet.updateObject(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateObject, columnIndex, x); } @Override public void updateObject(final int columnIndex, final Object x, final int scale) throws SQLException { - try { - resultSet.updateObject(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateObject, columnIndex, x); } /** @@ -1867,210 +1095,123 @@ public void updateObject(final int columnIndex, final Object x, final int scale) */ @Override public void updateObject(final int columnIndex, final Object x, final SQLType targetSqlType) throws SQLException { - try { - resultSet.updateObject(columnIndex, x, targetSqlType); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateObject, columnIndex, x, targetSqlType); } /** * @since 2.5.0 */ @Override - public void updateObject(final int columnIndex, final Object x, final SQLType targetSqlType, final int scaleOrLength) throws SQLException { - try { - resultSet.updateObject(columnIndex, x, targetSqlType, scaleOrLength); - } catch (final SQLException e) { - handleException(e); - } + public void updateObject(final int columnIndex, final Object x, final SQLType targetSqlType, + final int scaleOrLength) throws SQLException { + accept(resultSet::updateObject, columnIndex, x, targetSqlType, scaleOrLength); } @Override public void updateObject(final String columnName, final Object x) throws SQLException { - try { - resultSet.updateObject(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateObject, columnName, x); } @Override public void updateObject(final String columnName, final Object x, final int scale) throws SQLException { - try { - resultSet.updateObject(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateObject, columnName, x); } /** * @since 2.5.0 */ @Override - public void updateObject(final String columnLabel, final Object x, final SQLType targetSqlType) throws SQLException { - try { - resultSet.updateObject(columnLabel, x, targetSqlType); - } catch (final SQLException e) { - handleException(e); - } + public void updateObject(final String columnLabel, final Object x, final SQLType targetSqlType) + throws SQLException { + accept(resultSet::updateObject, columnLabel, x, targetSqlType); } /** * @since 2.5.0 */ @Override - public void updateObject(final String columnLabel, final Object x, final SQLType targetSqlType, final int scaleOrLength) - throws SQLException { - try { - resultSet.updateObject(columnLabel, x, targetSqlType, scaleOrLength); - } catch (final SQLException e) { - handleException(e); - } + public void updateObject(final String columnLabel, final Object x, final SQLType targetSqlType, + final int scaleOrLength) throws SQLException { + accept(resultSet::updateObject, columnLabel, x, targetSqlType, scaleOrLength); } @Override public void updateRef(final int columnIndex, final Ref x) throws SQLException { - try { - resultSet.updateRef(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateRef, columnIndex, x); } @Override public void updateRef(final String columnName, final Ref x) throws SQLException { - try { - resultSet.updateRef(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateRef, columnName, x); } @Override public void updateRow() throws SQLException { - try { - resultSet.updateRow(); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateRow); } @Override public void updateRowId(final int columnIndex, final RowId value) throws SQLException { - try { - resultSet.updateRowId(columnIndex, value); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateRowId, columnIndex, value); } @Override public void updateRowId(final String columnLabel, final RowId value) throws SQLException { - try { - resultSet.updateRowId(columnLabel, value); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateRowId, columnLabel, value); } @Override public void updateShort(final int columnIndex, final short x) throws SQLException { - try { - resultSet.updateShort(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateShort, columnIndex, x); } @Override public void updateShort(final String columnName, final short x) throws SQLException { - try { - resultSet.updateShort(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateShort, columnName, x); } @Override public void updateSQLXML(final int columnIndex, final SQLXML value) throws SQLException { - try { - resultSet.updateSQLXML(columnIndex, value); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateSQLXML, columnIndex, value); } @Override public void updateSQLXML(final String columnLabel, final SQLXML value) throws SQLException { - try { - resultSet.updateSQLXML(columnLabel, value); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateSQLXML, columnLabel, value); } @Override public void updateString(final int columnIndex, final String x) throws SQLException { - try { - resultSet.updateString(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateString, columnIndex, x); } @Override public void updateString(final String columnName, final String x) throws SQLException { - try { - resultSet.updateString(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateString, columnName, x); } @Override public void updateTime(final int columnIndex, final Time x) throws SQLException { - try { - resultSet.updateTime(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateTime, columnIndex, x); } @Override public void updateTime(final String columnName, final Time x) throws SQLException { - try { - resultSet.updateTime(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateTime, columnName, x); } @Override public void updateTimestamp(final int columnIndex, final Timestamp x) throws SQLException { - try { - resultSet.updateTimestamp(columnIndex, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateTimestamp, columnIndex, x); } @Override public void updateTimestamp(final String columnName, final Timestamp x) throws SQLException { - try { - resultSet.updateTimestamp(columnName, x); - } catch (final SQLException e) { - handleException(e); - } + accept(resultSet::updateTimestamp, columnName, x); } @Override public boolean wasNull() throws SQLException { - try { - return resultSet.wasNull(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyToFalse(resultSet::wasNull); } } diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java index 95253e500..8059b9719 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java @@ -49,10 +49,8 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { * Create a wrapper for the Statement which traces this Statement to the Connection which created it and the code * which created it. * - * @param statement - * the {@link Statement} to delegate all calls to. - * @param connection - * the {@link DelegatingConnection} that created this statement. + * @param statement the {@link Statement} to delegate all calls to. + * @param connection the {@link DelegatingConnection} that created this statement. */ public DelegatingStatement(final DelegatingConnection connection, final Statement statement) { super(connection); @@ -62,8 +60,7 @@ public DelegatingStatement(final DelegatingConnection connection, final State /** * - * @throws SQLException - * thrown by the delegating statement. + * @throws SQLException thrown by the delegating statement. * @since 2.4.0 made public, was protected in 2.3.0. */ public void activate() throws SQLException { @@ -74,24 +71,15 @@ public void activate() throws SQLException { @Override public void addBatch(final String sql) throws SQLException { - checkOpen(); - try { - statement.addBatch(sql); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::addBatch, statement, sql); } @Override public void cancel() throws SQLException { - checkOpen(); - try { - statement.cancel(); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::cancel, statement); } + @Override protected void checkOpen() throws SQLException { if (isClosed()) { throw new SQLException(this.getClass().getName() + " with address: \"" + this.toString() + "\" is closed."); @@ -100,22 +88,12 @@ protected void checkOpen() throws SQLException { @Override public void clearBatch() throws SQLException { - checkOpen(); - try { - statement.clearBatch(); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::clearBatch, statement); } @Override public void clearWarnings() throws SQLException { - checkOpen(); - try { - statement.clearWarnings(); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::clearWarnings, statement); } /** @@ -145,7 +123,7 @@ public void close() throws SQLException { if (resultSet != null) { try { resultSet.close(); - } catch (Exception e) { + } catch (final Exception e) { if (connection != null) { // Does not rethrow e. connection.handleExceptionNoThrow(e); @@ -159,7 +137,7 @@ public void close() throws SQLException { if (statement != null) { try { statement.close(); - } catch (Exception e) { + } catch (final Exception e) { if (connection != null) { // Does not rethrow e. connection.handleExceptionNoThrow(e); @@ -178,72 +156,32 @@ public void close() throws SQLException { @Override public void closeOnCompletion() throws SQLException { - checkOpen(); - try { - Jdbc41Bridge.closeOnCompletion(statement); - } catch (final SQLException e) { - handleException(e); - } + accept(Jdbc41Bridge::closeOnCompletion, statement); } @Override public boolean execute(final String sql) throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.execute(sql); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(Statement::execute, statement, sql, false); } @Override public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.execute(sql, autoGeneratedKeys); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(Statement::execute, statement, sql, autoGeneratedKeys, false); } @Override public boolean execute(final String sql, final int columnIndexes[]) throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.execute(sql, columnIndexes); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(Statement::execute, statement, sql, columnIndexes, false); } @Override public boolean execute(final String sql, final String columnNames[]) throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.execute(sql, columnNames); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(Statement::execute, statement, sql, columnNames, false); } @Override public int[] executeBatch() throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.executeBatch(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(Statement::executeBatch, statement); } /** @@ -251,14 +189,7 @@ public int[] executeBatch() throws SQLException { */ @Override public long[] executeLargeBatch() throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.executeLargeBatch(); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(Statement::executeLargeBatch, statement); } /** @@ -266,14 +197,7 @@ public long[] executeLargeBatch() throws SQLException { */ @Override public long executeLargeUpdate(final String sql) throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.executeLargeUpdate(sql); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::executeLargeUpdate, statement, sql, 0L); } /** @@ -281,14 +205,7 @@ public long executeLargeUpdate(final String sql) throws SQLException { */ @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; - } + return applyTo(Statement::executeLargeUpdate, statement, sql, autoGeneratedKeys, 0L); } /** @@ -296,14 +213,7 @@ public long executeLargeUpdate(final String sql, final int autoGeneratedKeys) th */ @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; - } + return applyTo(Statement::executeLargeUpdate, statement, sql, columnIndexes, 0L); } /** @@ -311,74 +221,32 @@ public long executeLargeUpdate(final String sql, final int[] columnIndexes) thro */ @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; - } + return applyTo(Statement::executeLargeUpdate, statement, sql, columnNames, 0L); } @Override public ResultSet executeQuery(final String sql) throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return DelegatingResultSet.wrapResultSet(this, statement.executeQuery(sql)); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(() -> DelegatingResultSet.wrapResultSet(this, statement.executeQuery(sql))); } @Override public int executeUpdate(final String sql) throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.executeUpdate(sql); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::executeUpdate, statement, sql, 0); } @Override public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.executeUpdate(sql, autoGeneratedKeys); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::executeUpdate, statement, sql, autoGeneratedKeys, 0); } @Override public int executeUpdate(final String sql, final int columnIndexes[]) throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.executeUpdate(sql, columnIndexes); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::executeUpdate, statement, sql, columnIndexes, 0); } @Override public int executeUpdate(final String sql, final String columnNames[]) throws SQLException { - checkOpen(); - setLastUsedInParent(); - try { - return statement.executeUpdate(sql, columnNames); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::executeUpdate, statement, sql, columnNames, 0); } @Override @@ -417,35 +285,17 @@ public Statement getDelegate() { @Override public int getFetchDirection() throws SQLException { - checkOpen(); - try { - return statement.getFetchDirection(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getFetchDirection, statement, 0); } @Override public int getFetchSize() throws SQLException { - checkOpen(); - try { - return statement.getFetchSize(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getFetchSize, statement, 0); } @Override public ResultSet getGeneratedKeys() throws SQLException { - checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(this, statement.getGeneratedKeys()); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(() -> DelegatingResultSet.wrapResultSet(this, statement.getGeneratedKeys())); } /** @@ -481,13 +331,7 @@ public Statement getInnermostDelegate() { */ @Override public long getLargeMaxRows() throws SQLException { - checkOpen(); - try { - return statement.getLargeMaxRows(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getLargeMaxRows, statement, 0L); } /** @@ -495,136 +339,65 @@ public long getLargeMaxRows() throws SQLException { */ @Override public long getLargeUpdateCount() throws SQLException { - checkOpen(); - try { - return statement.getLargeUpdateCount(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getLargeUpdateCount, statement, 0L); } @Override public int getMaxFieldSize() throws SQLException { - checkOpen(); - try { - return statement.getMaxFieldSize(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getMaxFieldSize, statement, 0); } @Override public int getMaxRows() throws SQLException { - checkOpen(); - try { - return statement.getMaxRows(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getMaxRows, statement, 0); } @Override public boolean getMoreResults() throws SQLException { - checkOpen(); - try { - return statement.getMoreResults(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(Statement::getMoreResults, statement, false); } @Override public boolean getMoreResults(final int current) throws SQLException { - checkOpen(); - try { - return statement.getMoreResults(current); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(Statement::getMoreResults, statement, current, false); } @Override public int getQueryTimeout() throws SQLException { - checkOpen(); - try { - return statement.getQueryTimeout(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getQueryTimeout, statement, 0); } @Override public ResultSet getResultSet() throws SQLException { - checkOpen(); - try { - return DelegatingResultSet.wrapResultSet(this, statement.getResultSet()); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(() -> DelegatingResultSet.wrapResultSet(this, statement.getResultSet())); } @Override public int getResultSetConcurrency() throws SQLException { - checkOpen(); - try { - return statement.getResultSetConcurrency(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getResultSetConcurrency, statement, 0); } @Override public int getResultSetHoldability() throws SQLException { - checkOpen(); - try { - return statement.getResultSetHoldability(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getResultSetHoldability, statement, 0); } @Override public int getResultSetType() throws SQLException { - checkOpen(); - try { - return statement.getResultSetType(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getResultSetType, statement, 0); } @Override public int getUpdateCount() throws SQLException { - checkOpen(); - try { - return statement.getUpdateCount(); - } catch (final SQLException e) { - handleException(e); - return 0; - } + return applyTo(Statement::getUpdateCount, statement, 0); } @Override public SQLWarning getWarnings() throws SQLException { - checkOpen(); - try { - return statement.getWarnings(); - } catch (final SQLException e) { - handleException(e); - throw new AssertionError(); - } + return apply(Statement::getWarnings, statement); } + @Override protected void handleException(final SQLException e) throws SQLException { if (connection != null) { connection.handleException(e); @@ -647,24 +420,12 @@ protected boolean isClosedInternal() { @Override public boolean isCloseOnCompletion() throws SQLException { - checkOpen(); - try { - return Jdbc41Bridge.isCloseOnCompletion(statement); - } catch (final SQLException e) { - handleException(e); - return false; - } + return apply(Jdbc41Bridge::isCloseOnCompletion, statement); } @Override public boolean isPoolable() throws SQLException { - checkOpen(); - try { - return statement.isPoolable(); - } catch (final SQLException e) { - handleException(e); - return false; - } + return applyTo(Statement::isPoolable, statement, false); } @Override @@ -680,8 +441,7 @@ public boolean isWrapperFor(final Class iface) throws SQLException { /** * - * @throws SQLException - * thrown by the delegating statement. + * @throws SQLException thrown by the delegating statement. * @since 2.4.0 made public, was protected in 2.3.0. */ public void passivate() throws SQLException { @@ -696,19 +456,13 @@ protected void setClosedInternal(final boolean closed) { @Override public void setCursorName(final String name) throws SQLException { - checkOpen(); - try { - statement.setCursorName(name); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::setCursorName, statement, name); } /** * Sets my delegate. * - * @param statement - * my delegate. + * @param statement my delegate. */ public void setDelegate(final Statement statement) { this.statement = statement; @@ -716,32 +470,17 @@ public void setDelegate(final Statement statement) { @Override public void setEscapeProcessing(final boolean enable) throws SQLException { - checkOpen(); - try { - statement.setEscapeProcessing(enable); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::setEscapeProcessing, statement, enable); } @Override public void setFetchDirection(final int direction) throws SQLException { - checkOpen(); - try { - statement.setFetchDirection(direction); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::setFetchDirection, statement, direction); } @Override public void setFetchSize(final int rows) throws SQLException { - checkOpen(); - try { - statement.setFetchSize(rows); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::setFetchSize, statement, rows); } /** @@ -749,15 +488,11 @@ public void setFetchSize(final int rows) throws SQLException { */ @Override public void setLargeMaxRows(final long max) throws SQLException { - checkOpen(); - try { - statement.setLargeMaxRows(max); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::setLargeMaxRows, statement, max); } - private void setLastUsedInParent() { + @Override + protected void markUse() { if (connection != null) { connection.setLastUsed(); } @@ -765,42 +500,22 @@ private void setLastUsedInParent() { @Override public void setMaxFieldSize(final int max) throws SQLException { - checkOpen(); - try { - statement.setMaxFieldSize(max); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::setMaxFieldSize, statement, max); } @Override public void setMaxRows(final int max) throws SQLException { - checkOpen(); - try { - statement.setMaxRows(max); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::setMaxRows, statement, max); } @Override public void setPoolable(final boolean poolable) throws SQLException { - checkOpen(); - try { - statement.setPoolable(poolable); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::setPoolable, statement, poolable); } @Override public void setQueryTimeout(final int seconds) throws SQLException { - checkOpen(); - try { - statement.setQueryTimeout(seconds); - } catch (final SQLException e) { - handleException(e); - } + accept(Statement::setQueryTimeout, statement, seconds); } /** diff --git a/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java b/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java index d99ed22b9..9ddbffe0f 100644 --- a/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java +++ b/src/main/java/org/apache/commons/dbcp2/PoolingConnection.java @@ -22,6 +22,7 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.NoSuchElementException; +import java.util.concurrent.Callable; import org.apache.commons.pool2.KeyedObjectPool; import org.apache.commons.pool2.KeyedPooledObjectFactory; @@ -63,7 +64,7 @@ public enum StatementType { } /** Pool of {@link PreparedStatement}s. and {@link CallableStatement}s */ - private KeyedObjectPool pstmtPool; + private KeyedObjectPool pStmtPool; /** * Constructor. @@ -96,9 +97,9 @@ public void activateObject(final PStmtKey key, final PooledObject oldpool = pstmtPool; - pstmtPool = null; + if (null != pStmtPool) { + final KeyedObjectPool oldpool = pStmtPool; + pStmtPool = null; try { oldpool.close(); } catch (final RuntimeException e) { @@ -304,11 +305,11 @@ public PooledObject makeObject(final PStmtKey key) if (key.getStmtType() == StatementType.PREPARED_STATEMENT) { final PreparedStatement statement = (PreparedStatement) key.createStatement(getDelegate()); @SuppressWarnings({"rawtypes", "unchecked" }) // Unable to find way to avoid this - final PoolablePreparedStatement pps = new PoolablePreparedStatement(statement, key, pstmtPool, this); + final PoolablePreparedStatement pps = new PoolablePreparedStatement(statement, key, pStmtPool, this); return new DefaultPooledObject<>(pps); } final CallableStatement statement = (CallableStatement) key.createStatement(getDelegate()); - final PoolableCallableStatement pcs = new PoolableCallableStatement(statement, key, pstmtPool, this); + final PoolableCallableStatement pcs = new PoolableCallableStatement(statement, key, pStmtPool, this); return new DefaultPooledObject<>(pcs); } @@ -341,6 +342,26 @@ public void passivateObject(final PStmtKey key, final PooledObject callableCreateKey) + throws SQLException { + if (null == pStmtPool) { + throw new SQLException("Statement pool is null - closed or invalid PoolingConnection."); + } + final PStmtKey pStmtKey; + StatementType stmtType = null; + try { + pStmtKey = callableCreateKey.call(); + stmtType = pStmtKey.getStmtType(); + return pStmtPool.borrowObject(pStmtKey); + } 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 " + stmtType + " from pool failed", e); + } + } + /** * Creates or obtains a {@link CallableStatement} from the pool. * @@ -352,15 +373,7 @@ public void passivateObject(final PStmtKey key, final PooledObject createKey(sql, StatementType.CALLABLE_STATEMENT)); } /** @@ -379,16 +392,8 @@ public CallableStatement prepareCall(final String sql) throws SQLException { @Override 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); - } + return (CallableStatement) borrow( + () -> createKey(sql, resultSetType, resultSetConcurrency, StatementType.CALLABLE_STATEMENT)); } /** @@ -409,16 +414,8 @@ public CallableStatement prepareCall(final String sql, final int resultSetType, @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); - } + return (CallableStatement) borrow(() -> createKey(sql, resultSetType, resultSetConcurrency, + resultSetHoldability, StatementType.CALLABLE_STATEMENT)); } /** @@ -430,34 +427,12 @@ public CallableStatement prepareCall(final String sql, final int resultSetType, */ @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); - } + return borrow(() -> createKey(sql)); } @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); - } + return borrow(() -> createKey(sql, autoGeneratedKeys)); } /** @@ -471,18 +446,7 @@ public PreparedStatement prepareStatement(final String sql, final int autoGenera */ @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); - } + return borrow(() -> createKey(sql, columnIndexes)); } /** @@ -499,18 +463,7 @@ public PreparedStatement prepareStatement(final String sql, final int columnInde @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); - } + return borrow(() -> createKey(sql, resultSetType, resultSetConcurrency)); } /** @@ -529,18 +482,7 @@ public PreparedStatement prepareStatement(final String sql, final int resultSetT @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); - } + return borrow(() -> createKey(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); } /** @@ -554,18 +496,7 @@ public PreparedStatement prepareStatement(final String sql, final int resultSetT */ @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); - } + return borrow(() -> createKey(sql, columnNames)); } /** @@ -575,13 +506,13 @@ public PreparedStatement prepareStatement(final String sql, final String columnN * the prepared statement pool. */ public void setStatementPool(final KeyedObjectPool pool) { - pstmtPool = pool; + pStmtPool = pool; } @Override public synchronized String toString() { - if (pstmtPool != null) { - return "PoolingConnection: " + pstmtPool.toString(); + if (pStmtPool != null) { + return "PoolingConnection: " + pStmtPool.toString(); } return "PoolingConnection: null"; } diff --git a/src/main/java/org/apache/commons/dbcp2/ResourceFunctions.java b/src/main/java/org/apache/commons/dbcp2/ResourceFunctions.java new file mode 100644 index 000000000..e32f5be67 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/ResourceFunctions.java @@ -0,0 +1,259 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbcp2; + +import java.sql.SQLException; + +import org.apache.commons.dbcp2.function.SQLBooleanSupplier; +import org.apache.commons.dbcp2.function.SQLConsumer0; +import org.apache.commons.dbcp2.function.SQLConsumer1; +import org.apache.commons.dbcp2.function.SQLConsumer2; +import org.apache.commons.dbcp2.function.SQLConsumer3; +import org.apache.commons.dbcp2.function.SQLConsumer4; +import org.apache.commons.dbcp2.function.SQLConsumer5; +import org.apache.commons.dbcp2.function.SQLFunction0; +import org.apache.commons.dbcp2.function.SQLFunction1; +import org.apache.commons.dbcp2.function.SQLFunction2; +import org.apache.commons.dbcp2.function.SQLFunction3; +import org.apache.commons.dbcp2.function.SQLIntConsumer; +import org.apache.commons.dbcp2.function.SQLIntFunction; +import org.apache.commons.dbcp2.function.SQLIntSupplier; + +/** + * Functional helpers. + * + * @since 2.8.0 + */ +public class ResourceFunctions { + + protected void accept(final SQLConsumer0 runnable) throws SQLException { + checkOpen(); + markUse(); + try { + runnable.accept(); + } catch (final SQLException e) { + handleException(e); + } + } + + protected void accept(final SQLConsumer1 consumer, final T value) throws SQLException { + checkOpen(); + markUse(); + try { + consumer.accept(value); + } catch (final SQLException e) { + handleException(e); + } + } + + protected void accept(final SQLConsumer2 consumer, final T t, final U u) throws SQLException { + checkOpen(); + markUse(); + try { + consumer.accept(t, u); + } catch (final SQLException e) { + handleException(e); + } + } + + protected void accept(final SQLConsumer3 consumer, final T t, final U u, final V v) + throws SQLException { + checkOpen(); + markUse(); + try { + consumer.accept(t, u, v); + } catch (final SQLException e) { + handleException(e); + } + } + + protected void accept(final SQLConsumer4 consumer, final T t, final U u, final V v, + final W w) throws SQLException { + checkOpen(); + markUse(); + try { + consumer.accept(t, u, v, w); + } catch (final SQLException e) { + handleException(e); + } + } + + protected void accept(final SQLConsumer5 consumer, final T t, final U u, + final V v, final W w, final X x) throws SQLException { + checkOpen(); + markUse(); + try { + consumer.accept(t, u, v, w, x); + } catch (final SQLException e) { + handleException(e); + } + } + + protected void acceptInt(final SQLIntConsumer consumer, final int value) throws SQLException { + checkOpen(); + markUse(); + try { + consumer.accept(value); + } catch (final SQLException e) { + handleException(e); + } + } + + protected T apply(final SQLFunction0 callable) throws SQLException { + return applyTo(callable, null); + } + + protected R apply(final SQLFunction1 function, final T value) throws SQLException { + return applyTo(function, value, null); + } + + protected R apply(final SQLFunction2 function, final T t, final U u) throws SQLException { + checkOpen(); + markUse(); + try { + return function.apply(t, u); + } catch (final SQLException e) { + handleException(e); + return null; + } + } + + protected R apply(final SQLFunction3 function, final T t, final U u, final V v) + throws SQLException { + checkOpen(); + markUse(); + try { + return function.apply(t, u, v); + } catch (final SQLException e) { + handleException(e); + return null; + } + } + + protected R apply(final SQLIntFunction function, final int value) throws SQLException { + return applyIntTo(function, value, null); + } + + protected R applyIntTo(final SQLIntFunction function, final int value, final R onException) + throws SQLException { + checkOpen(); + markUse(); + try { + return function.apply(value); + } catch (final SQLException e) { + handleException(e); + return onException; + } + } + + protected T applyTo(final SQLFunction0 callable, final T onException) throws SQLException { + checkOpen(); + markUse(); + try { + return callable.apply(); + } catch (final SQLException e) { + handleException(e); + return onException; + } + } + + protected R applyTo(final SQLFunction1 function, final T value, final R onException) + throws SQLException { + checkOpen(); + markUse(); + try { + return function.apply(value); + } catch (final SQLException e) { + handleException(e); + return onException; + } + } + + protected R applyTo(final SQLFunction2 function, final T t, final U u, + final R onException) throws SQLException { + checkOpen(); + markUse(); + try { + return function.apply(t, u); + } catch (final SQLException e) { + handleException(e); + return onException; + } + } + + protected R applyTo(final SQLFunction3 function, final T t, final U u, + final V v, final R onException) throws SQLException { + checkOpen(); + markUse(); + try { + return function.apply(t, u, v); + } catch (final SQLException e) { + handleException(e); + return onException; + } + } + + protected int applyTo0(final SQLFunction0 callable) throws SQLException { + return applyTo(callable, 0); + } + + protected long applyTo0L(final SQLFunction0 callable) throws SQLException { + return applyTo(callable, 0L); + } + + protected boolean applyToFalse(final SQLFunction0 callable) throws SQLException { + return applyTo(callable, false); + } + + @SuppressWarnings("unused") + protected void checkOpen() throws SQLException { + // empty + } + + protected boolean getAsBoolean(final SQLBooleanSupplier supplier) throws SQLException { + checkOpen(); + markUse(); + try { + return supplier.getAsBoolean(); + } catch (final SQLException e) { + handleException(e); + return false; + } + } + + protected int getAsInt(final SQLIntSupplier supplier) throws SQLException { + checkOpen(); + markUse(); + try { + return supplier.getAsInt(); + } catch (final SQLException e) { + handleException(e); + return 0; + } + } + + @SuppressWarnings("unused") + protected void handleException(final SQLException e) throws SQLException { + // empty + } + + protected void markUse() { + // empty + + } + +} diff --git a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/ConnectionImpl.java b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/ConnectionImpl.java index 2226e8e06..9ac586043 100644 --- a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/ConnectionImpl.java +++ b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/ConnectionImpl.java @@ -98,13 +98,7 @@ public void close() throws SQLException { */ @Override public CallableStatement prepareCall(final String sql) throws SQLException { - checkOpen(); - try { - return new DelegatingCallableStatement(this, pooledConnection.prepareCall(sql)); - } catch (final SQLException e) { - handleException(e); // Does not return - return null; - } + return apply(() -> new DelegatingCallableStatement(this, pooledConnection.prepareCall(sql)) ); } /** @@ -130,14 +124,8 @@ public CallableStatement prepareCall(final String sql) throws SQLException { @Override public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { - checkOpen(); - try { - return new DelegatingCallableStatement(this, - pooledConnection.prepareCall(sql, resultSetType, resultSetConcurrency)); - } catch (final SQLException e) { - handleException(e); // Does not return - return null; - } + return apply(() -> new DelegatingCallableStatement(this, + pooledConnection.prepareCall(sql, resultSetType, resultSetConcurrency))); } /** @@ -166,14 +154,8 @@ public CallableStatement prepareCall(final String sql, final int resultSetType, @Override public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException { - checkOpen(); - try { - return new DelegatingCallableStatement(this, - pooledConnection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); - } catch (final SQLException e) { - handleException(e); // Does not return - return null; - } + return apply(() -> new DelegatingCallableStatement(this, + pooledConnection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability))); } /** @@ -188,13 +170,7 @@ public CallableStatement prepareCall(final String sql, final int resultSetType, */ @Override public PreparedStatement prepareStatement(final String sql) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql)); - } catch (final SQLException e) { - handleException(e); // Does not return - return null; - } + return apply(() -> new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql))); } /** @@ -207,60 +183,31 @@ public PreparedStatement prepareStatement(final String sql) throws SQLException @Override public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, - pooledConnection.prepareStatement(sql, resultSetType, resultSetConcurrency)); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(() -> new DelegatingPreparedStatement(this, + pooledConnection.prepareStatement(sql, resultSetType, resultSetConcurrency))); } @Override public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, - pooledConnection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(() -> new DelegatingPreparedStatement(this, + pooledConnection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability))); } @Override public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, autoGeneratedKeys)); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply( + () -> new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, autoGeneratedKeys))); } @Override public PreparedStatement prepareStatement(final String sql, final int columnIndexes[]) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, columnIndexes)); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(() -> new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, columnIndexes))); } @Override public PreparedStatement prepareStatement(final String sql, final String columnNames[]) throws SQLException { - checkOpen(); - try { - return new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, columnNames)); - } catch (final SQLException e) { - handleException(e); - return null; - } + return apply(() -> new DelegatingPreparedStatement(this, pooledConnection.prepareStatement(sql, columnNames))); } // 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 1f74af4ad..705ce3b62 100644 --- a/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java +++ b/src/main/java/org/apache/commons/dbcp2/cpdsadapter/PooledConnectionImpl.java @@ -22,6 +22,7 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Vector; +import java.util.concurrent.Callable; import javax.sql.ConnectionEvent; import javax.sql.ConnectionEventListener; @@ -35,6 +36,7 @@ import org.apache.commons.dbcp2.PoolableCallableStatement; import org.apache.commons.dbcp2.PoolablePreparedStatement; import org.apache.commons.dbcp2.PoolingConnection.StatementType; +import org.apache.commons.dbcp2.function.SQLFunction0; import org.apache.commons.pool2.KeyedObjectPool; import org.apache.commons.pool2.KeyedPooledObjectFactory; import org.apache.commons.pool2.PooledObject; @@ -476,6 +478,29 @@ public void passivateObject(final PStmtKey key, final PooledObject nullPoolFunction, Callable callableCreateKey) + throws SQLException { + if (pStmtPool == null) { + return nullPoolFunction.apply(); + } + try { + return pStmtPool.borrowObject(callableCreateKey.call()); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new SQLException("Borrow prepareCall from pool failed", e); + } + } + /** * Creates or obtains a {@link CallableStatement} from my pool. * @@ -488,16 +513,8 @@ public void passivateObject(final PStmtKey key, final PooledObject connection.prepareCall(sql), + () -> createKey(sql, StatementType.CALLABLE_STATEMENT)); } /** @@ -521,17 +538,8 @@ CallableStatement prepareCall(final String sql) throws SQLException { */ CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { - if (pStmtPool == null) { - return connection.prepareCall(sql, resultSetType, resultSetConcurrency); - } - try { - return (CallableStatement) pStmtPool.borrowObject( - createKey(sql, resultSetType, resultSetConcurrency, StatementType.CALLABLE_STATEMENT)); - } catch (final RuntimeException e) { - throw e; - } catch (final Exception e) { - throw new SQLException("Borrow prepareCall from pool failed", e); - } + return (CallableStatement) borrow(() -> connection.prepareCall(sql, resultSetType, resultSetConcurrency), + () -> createKey(sql, resultSetType, resultSetConcurrency, StatementType.CALLABLE_STATEMENT)); } /** @@ -558,17 +566,10 @@ CallableStatement prepareCall(final String sql, final int resultSetType, final i */ CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException { - if (pStmtPool == null) { - return connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); - } - try { - return (CallableStatement) pStmtPool.borrowObject(createKey(sql, resultSetType, resultSetConcurrency, - resultSetHoldability, StatementType.CALLABLE_STATEMENT)); - } catch (final RuntimeException e) { - throw e; - } catch (final Exception e) { - throw new SQLException("Borrow prepareCall from pool failed", e); - } + return (CallableStatement) borrow( + () -> connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability), + () -> createKey(sql, resultSetType, resultSetConcurrency, resultSetHoldability, + StatementType.CALLABLE_STATEMENT)); } /** @@ -579,16 +580,7 @@ CallableStatement prepareCall(final String sql, final int resultSetType, final i * @return a {@link PoolablePreparedStatement} */ PreparedStatement prepareStatement(final String sql) throws SQLException { - if (pStmtPool == null) { - return connection.prepareStatement(sql); - } - try { - return pStmtPool.borrowObject(createKey(sql)); - } catch (final RuntimeException e) { - throw e; - } catch (final Exception e) { - throw new SQLException("Borrow prepareStatement from pool failed", e); - } + return borrow(() -> connection.prepareStatement(sql), () -> createKey(sql)); } /** @@ -603,29 +595,12 @@ PreparedStatement prepareStatement(final String sql) throws SQLException { * @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); - } + return borrow(() -> connection.prepareStatement(sql, autoGeneratedKeys), + () -> createKey(sql, autoGeneratedKeys)); } 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); - } + return borrow(() -> connection.prepareStatement(sql, columnIndexes),() -> createKey(sql, columnIndexes)); } /** @@ -646,43 +621,18 @@ PreparedStatement prepareStatement(final String sql, final int columnIndexes[]) */ PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { - if (pStmtPool == null) { - return connection.prepareStatement(sql, resultSetType, resultSetConcurrency); - } - try { - return pStmtPool.borrowObject(createKey(sql, resultSetType, resultSetConcurrency)); - } catch (final RuntimeException e) { - throw e; - } catch (final Exception e) { - throw new SQLException("Borrow prepareStatement from pool failed", e); - } + return borrow(() -> connection.prepareStatement(sql, resultSetType, resultSetConcurrency), + () -> createKey(sql, resultSetType, resultSetConcurrency)); } PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException { - if (pStmtPool == null) { - return connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); - } - try { - return pStmtPool.borrowObject(createKey(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); - } catch (final RuntimeException e) { - throw e; - } catch (final Exception e) { - throw new SQLException("Borrow prepareStatement from pool failed", e); - } + return borrow(() -> connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability), + () -> createKey(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); } PreparedStatement prepareStatement(final String sql, final String columnNames[]) throws SQLException { - if (pStmtPool == null) { - return connection.prepareStatement(sql, columnNames); - } - try { - return pStmtPool.borrowObject(createKey(sql, columnNames)); - } catch (final RuntimeException e) { - throw e; - } catch (final Exception e) { - throw new SQLException("Borrow prepareStatement from pool failed", e); - } + return borrow(() -> connection.prepareStatement(sql, columnNames), () -> createKey(sql, columnNames)); } /** diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLBooleanSupplier.java b/src/main/java/org/apache/commons/dbcp2/function/SQLBooleanSupplier.java new file mode 100644 index 000000000..9156c0bf8 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLBooleanSupplier.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.function.Supplier; + +/** + * Represents a supplier of SQL {@code boolean}-valued results. This is the {@code boolean}-producing primitive + * specialization of {@link Supplier}. + * + *

+ * There is no requirement that a distinct result be returned each time the supplier is invoked. + *

+ * + *

+ * This is a functional interface whose functional method is {@link #getAsBoolean()}. + *

+ * + * @see java.util.function.BooleanSupplier + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLBooleanSupplier { + + /** + * Gets a result. + * + * @return a result + * @throws SQLException + */ + boolean getAsBoolean() throws SQLException; +} diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer0.java b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer0.java new file mode 100644 index 000000000..b45bd0af2 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer0.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; + +/** + * A SQL task that may throw a SQLException. Implementors define a method without arguments called {@code call()}. + * + *

+ * The {@code Callable} interface is similar to {@link java.lang.Runnable} but is specialized for SQL Exceptions. + *

+ * + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLConsumer0 { + + /** + * Computes a result, or throws an exception if unable to do so. + * + * @throws SQLException if unable to compute a result + */ + void accept() throws SQLException; +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer1.java b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer1.java new file mode 100644 index 000000000..280075cc1 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer1.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.Objects; +import java.util.function.Consumer; + +/** + * Represents a SQL operation that accepts a single input argument and no result. Unlike most other functional + * interfaces, this is expected to operate via side-effects. + * + *

+ * This is a functional interface whose functional method is {@link #accept(Object)}. + *

+ * + * @param the type of the input to the operation + * + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLConsumer1 { + + /** + * Performs this operation on the given argument. + * + * @param t the input argument + * @throws SQLException + */ + void accept(T t) throws SQLException; + + /** + * As {@link Consumer#andThen(Consumer)} but with a SQL Exception. + * + * @param after the operation to perform after this operation + * @return a composed {@code Consumer} that performs in sequence this operation followed by the {@code after} + * operation + * @throws NullPointerException if {@code after} is null + */ + default SQLConsumer1 andThen(SQLConsumer1 after) { + Objects.requireNonNull(after); + return (T t) -> { + accept(t); + after.accept(t); + }; + } +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer2.java b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer2.java new file mode 100644 index 000000000..7955d5530 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer2.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.function.Consumer; + +/** + * Represents an operation that accepts two input arguments and returns no result. This is the two-arity specialization + * of {@link Consumer}. Unlike most other functional interfaces, this is expected to operate via side-effects. + * + *

+ * This is a functional interface whose functional method is + * {@link #accept(Object, Object)}. + *

+ * + * @param the type of the first argument to the operation + * @param the type of the second argument to the operation + * + * @see java.util.function.Consumer + * @see java.util.function.BiConsumer + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLConsumer2 { + + /** + * Performs this operation on the given arguments. + * + * @param t the first input argument + * @param u the second input argument + * @throws SQLException + */ + void accept(T t, U u) throws SQLException; + +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer3.java b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer3.java new file mode 100644 index 000000000..00f5ccfa6 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer3.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; + +/** + * Represents an operation that accepts three input arguments and returns no result. This is the three-arity + * specialization of {@link SQLConsumer1}. Unlike most other functional interfaces, this is expected to operate via + * side-effects. + * + *

+ * This is a functional interface whose functional method is + * {@link #accept(Object, Object, Object)}. + *

+ * + * @param the type of the first argument to the operation + * @param the type of the second argument to the operation + * @param the type of the third argument to the operation + * + * @see java.util.function.Consumer + * @see java.util.function.BiConsumer + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLConsumer3 { + + /** + * Performs this operation on the given arguments. + * + * @param t the first input argument + * @param u the second input argument + * @param v the third input argument + * @throws SQLException + */ + void accept(T t, U u, V v) throws SQLException; + +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer4.java b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer4.java new file mode 100644 index 000000000..391c7961f --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer4.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; + +/** + * Represents an operation that accepts four input arguments and returns no result. This is the four-arity + * specialization of {@link SQLConsumer1}. Unlike most other functional interfaces, this is expected to operate via + * side-effects. + * + *

+ * This is a functional interface whose functional method is + * {@link #accept(Object, Object, Object, Object)}. + *

+ * + * @param the type of the first argument to the operation + * @param the type of the second argument to the operation + * @param the type of the third argument to the operation + * @param the type of the fourth argument to the operation + * + * @see java.util.function.Consumer + * @see java.util.function.BiConsumer + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLConsumer4 { + + /** + * Performs this operation on the given arguments. + * + * @param t the first input argument + * @param u the second input argument + * @param v the third input argument + * @param w the fourth input argument + * @throws SQLException + */ + void accept(T t, U u, V v, W w) throws SQLException; + +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer5.java b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer5.java new file mode 100644 index 000000000..311a64dd4 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLConsumer5.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; + +/** + * Represents an operation that accepts four input arguments and returns no result. This is the four-arity + * specialization of {@link SQLConsumer1}. Unlike most other functional interfaces, this is expected to operate via + * side-effects. + * + *

+ * This is a functional interface whose functional method is + * {@link #accept(Object, Object, Object, Object, Object)}. + *

+ * + * @param the type of the first argument to the operation + * @param the type of the second argument to the operation + * @param the type of the third argument to the operation + * @param the type of the fourth argument to the operation + * @param the type of the fifth argument to the operation + * + * @see java.util.function.Consumer + * @see java.util.function.BiConsumer + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLConsumer5 { + + /** + * Performs this operation on the given arguments. + * + * @param t the first input argument + * @param u the second input argument + * @param v the third input argument + * @param w the fourth input argument + * @param x the fifth input argument + * @throws SQLException + */ + void accept(T t, U u, V v, W w, X x) throws SQLException; + +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLFunction0.java b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction0.java new file mode 100644 index 000000000..0dad9b154 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction0.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; + +/** + * A SQL task that returns a result and may throw a SQLException. Implementors define a method without arguments called + * {@code call()}. + * + *

+ * The {@code Callable} interface is similar to {@link java.util.concurrent.Callable} but is specialized for SQL + * Exceptions. + *

+ * + * @param the result type of method {@link #apply()} + * @see java.util.concurrent.Callable + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLFunction0 { + + /** + * Computes a result, or throws an exception if unable to do so. + * + * @return computed result + * @throws SQLException if unable to compute a result + */ + R apply() throws SQLException; +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLFunction1.java b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction1.java new file mode 100644 index 000000000..c69ff7095 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction1.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.Objects; +import java.util.function.Function; + +/** + * Represents a SQL function that accepts one argument and produces a result. + * + *

+ * This is a functional interface whose functional method is {@link #apply(Object)}. + *

+ * + * @param the type of the input to the function + * @param the type of the result of the function + * + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLFunction1 { + + /** + * Applies this function to the given argument. + * + * @param t the function argument + * @return the function result + * @throws SQLException + */ + R apply(T t) throws SQLException; + + /** + * See {@link Function#compose(Function)}. + * + * @param the type of input to the {@code before} function, and to the composed function + * @param before the function to apply before this function is applied + * @return a composed function that first applies the {@code before} function and then applies this function + * @throws NullPointerException if before is null + * + * @see #andThen(SQLFunction1) + */ + default SQLFunction1 compose(SQLFunction1 before) { + Objects.requireNonNull(before); + return (V v) -> apply(before.apply(v)); + } + + /** + * See {@link Function#andThen(Function)}. + * + * @param the type of output of the {@code after} function, and of the composed function + * @param after the function to apply after this function is applied + * @return a composed function that first applies this function and then applies the {@code after} function + * @throws NullPointerException if after is null + * + * @see #compose(SQLFunction1) + */ + default SQLFunction1 andThen(SQLFunction1 after) { + Objects.requireNonNull(after); + return (T t) -> after.apply(apply(t)); + } + + /** + * Returns a function that always returns its input argument. + * + * @param the type of the input and output objects to the function + * @return a function that always returns its input argument + */ + static SQLFunction1 identity() { + return t -> t; + } +} diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLFunction2.java b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction2.java new file mode 100644 index 000000000..5f1052794 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction2.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.function.Function; + +/** + * Represents a SQL function that accepts two arguments and produces a result. This is the two-arity specialization of + * {@link SQLFunction1}. + * + *

+ * This is a functional interface whose functional method is + * {@link #apply(Object, Object)}. + *

+ * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param the type of the result of the function + * + * @see Function + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLFunction2 { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @return the function result + * @throws SQLException + */ + R apply(T t, U u) throws SQLException; + +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLFunction3.java b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction3.java new file mode 100644 index 000000000..b996e682c --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction3.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.function.Function; + +/** + * Represents a SQL function that accepts three arguments and produces a result. This is the three-arity specialization of + * {@link SQLFunction1}. + * + *

+ * This is a functional interface whose functional method is + * {@link #apply(Object, Object, Object)}. + *

+ * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param the type of the third argument to the function + * @param the type of the result of the function + * + * @see Function + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLFunction3 { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @param v the third function argument + * @return the function result + * @throws SQLException + */ + R apply(T t, U u, V v) throws SQLException; + +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLFunction4.java b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction4.java new file mode 100644 index 000000000..966d162d7 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction4.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.function.Function; + +/** + * Represents a SQL function that accepts four arguments and produces a result. This is the four-arity specialization of + * {@link SQLFunction1}. + * + *

+ * This is a functional interface whose functional method is + * {@link #apply(Object, Object, Object, Object)}. + *

+ * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param the type of the third argument to the function + * @param the type of the fourth argument to the function + * @param the type of the result of the function + * + * @see Function + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLFunction4 { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @param v the third function argument + * @param x the fourth function argument + * @return the function result + * @throws SQLException + */ + R apply(T t, U u, V v, X x) throws SQLException; + +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLFunction5.java b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction5.java new file mode 100644 index 000000000..429717f83 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction5.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.function.Function; + +/** + * Represents a SQL function that accepts five arguments and produces a result. This is the five-arity specialization of + * {@link SQLFunction1}. + * + *

+ * This is a functional interface whose functional method is + * {@link #apply(Object, Object, Object, Object, Object)}. + *

+ * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param the type of the third argument to the function + * @param the type of the fourth argument to the function + * @param the type of the fourth argument to the function + * @param the type of the result of the function + * + * @see Function + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLFunction5 { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @param v the third function argument + * @param x the fourth function argument + * @param y the fourth function argument + * @return the function result + * @throws SQLException + */ + R apply(T t, U u, V v, X x, Y y) throws SQLException; + +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLFunction6.java b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction6.java new file mode 100644 index 000000000..389b275c1 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLFunction6.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.function.Function; + +/** + * Represents a SQL function that accepts six arguments and produces a result. This is the six-arity specialization of + * {@link SQLFunction1}. + * + *

+ * This is a functional interface whose functional method is + * {@link #apply(Object, Object, Object, Object, Object, Object)}. + *

+ * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param the type of the third argument to the function + * @param the type of the fourth argument to the function + * @param the type of the fifth argument to the function + * @param the type of the sixth argument to the function + * @param the type of the result of the function + * + * @see Function + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLFunction6 { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @param v the third function argument + * @param x the fourth function argument + * @param y the fifth function argument + * @param z the sixth function argument + * @return the function result + * @throws SQLException + */ + R apply(T t, U u, V v, X x, Y y, Z z) throws SQLException; + +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLIntConsumer.java b/src/main/java/org/apache/commons/dbcp2/function/SQLIntConsumer.java new file mode 100644 index 000000000..6a7a6fe25 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLIntConsumer.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.Objects; +import java.util.function.Consumer; + +/** + * Represents an operation that accepts a single {@code int}-valued argument and no result. This is the primitive type + * specialization of {@link Consumer} for {@code int}. Unlike most other functional interfaces, {@code IntConsumer} is + * expected to operate via side-effects. + * + *

+ * This is a functional interface whose functional method is {@link #accept(int)}. + *

+ * + * @see Consumer + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLIntConsumer { + + /** + * Performs this operation on the given argument. + * + * @param value the input argument + * @throws SQLException + */ + void accept(int value) throws SQLException; + + /** + * As {@link Consumer#andThen(Consumer)} but with a SQL Exception. + * + * @param after the operation to perform after this operation + * @return a composed {@code Consumer} that performs in sequence this operation followed by the {@code after} + * operation + * @throws NullPointerException if {@code after} is null + */ + default SQLIntConsumer andThen(SQLIntConsumer after) { + Objects.requireNonNull(after); + return (int t) -> { + accept(t); + after.accept(t); + }; + } +} diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLIntFunction.java b/src/main/java/org/apache/commons/dbcp2/function/SQLIntFunction.java new file mode 100644 index 000000000..cd250ac8b --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLIntFunction.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; +import java.util.function.Function; + +/** + * Represents a SQL function that accepts an int-valued argument and produces a result. This is the + * {@code int}-consuming primitive specialization for {@link Function}. + * + *

+ * This is a functional interface whose functional method is {@link #apply(int)}. + *

+ * + * @param the type of the result of the function + * + * @see Function + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLIntFunction { + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + * @throws SQLException + */ + R apply(int value) throws SQLException; +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/dbcp2/function/SQLIntSupplier.java b/src/main/java/org/apache/commons/dbcp2/function/SQLIntSupplier.java new file mode 100644 index 000000000..94528ff91 --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/function/SQLIntSupplier.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2.function; + +import java.sql.SQLException; + +/** + * Represents a supplier of SQL {@code int}-valued results. This is the SQL {@code int}-producing primitive + * specialization of {@link java.util.function.IntSupplier}. + * + *

+ * There is no requirement that a distinct result be returned each time the supplier is invoked. + *

+ * + *

+ * This is a functional interface whose functional method is {@link #getAsInt()}. + *

+ * + * @see java.util.function.IntSupplier + * @since 2.8.0 + */ +@FunctionalInterface +public interface SQLIntSupplier { + + /** + * Gets a result. + * + * @return a result + * @throws SQLException + */ + int getAsInt() throws SQLException; +} diff --git a/src/test/java/org/apache/commons/dbcp2/TestAbandonedBasicDataSource.java b/src/test/java/org/apache/commons/dbcp2/TestAbandonedBasicDataSource.java index da83df664..30466b932 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestAbandonedBasicDataSource.java +++ b/src/test/java/org/apache/commons/dbcp2/TestAbandonedBasicDataSource.java @@ -276,7 +276,7 @@ public void testGarbageCollectorCleanUp02() throws Exception { @SuppressWarnings("unchecked") final GenericKeyedObjectPool gkop = - (GenericKeyedObjectPool) TesterUtils.getField(poolingConn, "pstmtPool"); + (GenericKeyedObjectPool) TesterUtils.getField(poolingConn, "pStmtPool"); Assertions.assertEquals(0, conn.getTrace().size()); Assertions.assertEquals(0, gkop.getNumActive()); createStatement(conn); diff --git a/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java b/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java index 6b0ed7c25..6863b9c4c 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java +++ b/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java @@ -685,6 +685,15 @@ protected boolean isClosed(final Statement statement) { try { statement.getWarnings(); return false; + } catch (final SQLException e) { + // getWarnings throws an exception if the statement is + // closed, but could throw an exception for other reasons + // in this case it is good enough to assume the statement + // is closed + } + try { + statement.getFetchSize(); + return false; } catch (final SQLException e) { // getWarnings throws an exception if the statement is // closed, but could throw an exception for other reasons diff --git a/src/test/java/org/apache/commons/dbcp2/TestDelegatingResultSet.java b/src/test/java/org/apache/commons/dbcp2/TestDelegatingResultSet.java index f0ce5c0b2..a35ba3d5d 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestDelegatingResultSet.java +++ b/src/test/java/org/apache/commons/dbcp2/TestDelegatingResultSet.java @@ -45,7 +45,7 @@ public class TestDelegatingResultSet { private DelegatingResultSet delegate; @BeforeEach - public void setUp() { + public void setUp() throws SQLException { testConn = new TesterConnection("foo", "bar"); conn = new DelegatingConnection<>(testConn); rs = mock(ResultSet.class);