This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
commit 30670489489c76fec9cd44959f1f8c7df1678a43 Author: Gary Gregory <[email protected]> AuthorDate: Sun Nov 22 08:22:48 2026 -0500 Add a builder for PoolingDriver Introduce Builder implementing Supplier<PoolingDriver>, exposed through builder(), with construction delegated to a private Builder constructor. Allow configuring delegate access while preserving existing defaults and constructors. Document registration behavior and builder reuse. Add tests for configuration, independent instances, and delegate access. --- src/changes/changes.xml | 1 + .../org/apache/commons/dbcp2/PoolingDriver.java | 71 +++++++++++++++++++++- .../apache/commons/dbcp2/TestPoolingDriver.java | 40 ++++++++++++ .../dbcp2/TestUnderlyingConnectionAccess.java | 2 +- 4 files changed, 112 insertions(+), 2 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index f5676717..e84d2144 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -76,6 +76,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Extend Utils.cloneWithoutCredentials(Properties) to omit java.naming.security.principal and java.naming.security.credentials in addition to user and password. Use the shared filter for SharedPoolDataSource, PerUserPoolDataSource, DriverAdapterCPDS, and DriverConnectionFactory diagnostic strings without changing configured properties (f006).</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add DriverAdapterCPDS.exportPasswordAsRefAddr to optionally omit the password address from JNDI References. Password export remains enabled by default for compatibility; the setting survives JNDI lookup and Java serialization.</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add PoolingDriver.Builder and PoolingDriver.builder() to configure delegate access when creating drivers, preserving the existing permissive default.</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 93 to 107.</action> <action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-pool2 from 2.13.0 to 2.13.1.</action> diff --git a/src/main/java/org/apache/commons/dbcp2/PoolingDriver.java b/src/main/java/org/apache/commons/dbcp2/PoolingDriver.java index 2f745c37..5d649711 100644 --- a/src/main/java/org/apache/commons/dbcp2/PoolingDriver.java +++ b/src/main/java/org/apache/commons/dbcp2/PoolingDriver.java @@ -25,6 +25,7 @@ import java.sql.SQLFeatureNotSupportedException; import java.util.HashMap; import java.util.NoSuchElementException; import java.util.Properties; +import java.util.function.Supplier; import java.util.logging.Logger; import org.apache.commons.pool2.ObjectPool; @@ -36,6 +37,55 @@ import org.apache.commons.pool2.ObjectPool; */ public class PoolingDriver implements Driver { + /** + * Builds {@link PoolingDriver} instances. + * <p> + * Access to underlying connections is enabled by default, matching {@link PoolingDriver#PoolingDriver()}. + * </p> + * + * @since 2.15.0 + */ + public static class Builder implements Supplier<PoolingDriver> { + + private boolean accessToUnderlyingConnectionAllowed = true; + + /** + * Constructs a new builder. + */ + public Builder() { + // empty + } + + /** + * Gets a new driver with this builder's current configuration. + * <p> + * This method does not register the returned driver with {@link DriverManager} or replace the driver registered by + * {@link PoolingDriver}'s static initializer. Changes to this builder do not affect drivers already created by it. + * </p> + * + * @return a new driver. + */ + @Override + public PoolingDriver get() { + return new PoolingDriver(this); + } + + /** + * Sets whether connections and their statements, result sets, and database metadata permit access to delegates. + * <p> + * The default is {@code true}. When {@code false}, delegate getters return {@code null} and JDBC unwrapping is limited to + * interfaces implemented by the wrapper itself. + * </p> + * + * @param accessToUnderlyingConnectionAllowed whether to allow access to delegates. + * @return this builder. + */ + public Builder setAccessToUnderlyingConnectionAllowed(final boolean accessToUnderlyingConnectionAllowed) { + this.accessToUnderlyingConnectionAllowed = accessToUnderlyingConnectionAllowed; + return this; + } + } + /** * PoolGuardConnectionWrapper is a Connection wrapper that makes sure a closed connection cannot be used anymore. * @@ -112,6 +162,16 @@ public class PoolingDriver implements Driver { */ protected static final int MINOR_VERSION = 0; + /** + * Creates a new builder. + * + * @return a new builder. + * @since 2.15.0 + */ + public static Builder builder() { + return new Builder(); + } + /** Controls access to the underlying connection */ private final boolean accessToUnderlyingConnectionAllowed; @@ -122,7 +182,7 @@ public class PoolingDriver implements Driver { * </p> */ public PoolingDriver() { - this(true); + this(builder()); } /** @@ -136,6 +196,15 @@ public class PoolingDriver implements Driver { this.accessToUnderlyingConnectionAllowed = accessToUnderlyingConnectionAllowed; } + /** + * Constructs a new driver from a builder. + * + * @param builder the builder. + */ + private PoolingDriver(final Builder builder) { + this(builder.accessToUnderlyingConnectionAllowed); + } + @Override public boolean acceptsURL(final String url) throws SQLException { return url != null && url.startsWith(URL_PREFIX); diff --git a/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java b/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java index 884e8d60..46d5223c 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java +++ b/src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java @@ -22,6 +22,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -31,9 +33,11 @@ import java.io.PrintStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.sql.Connection; +import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.time.Duration; +import java.util.function.Supplier; import javax.sql.DataSource; @@ -116,6 +120,42 @@ public class TestPoolingDriver extends TestConnectionPool { driver2.registerPool("example", connectionPool); } + @Test + void testBuilderConfiguration() throws Exception { + final String url = PoolingDriver.URL_PREFIX + "test"; + final Driver registered = DriverManager.getDriver(url); + final PoolingDriver.Builder builder = PoolingDriver.builder(); + assertSame(builder, builder.setAccessToUnderlyingConnectionAllowed(false)); + final PoolingDriver restricted = builder.get(); + final PoolingDriver permissive = builder.setAccessToUnderlyingConnectionAllowed(true).get(); + assertSame(registered, DriverManager.getDriver(url)); + try (Connection restrictedConnection = restricted.connect(url, null); + Connection permissiveConnection = permissive.connect(url, null)) { + final DelegatingConnection<?> restrictedWrapper = (DelegatingConnection<?>) restrictedConnection; + final DelegatingConnection<?> permissiveWrapper = (DelegatingConnection<?>) permissiveConnection; + assertNull(restrictedWrapper.getDelegate()); + assertNull(restrictedWrapper.getInnermostDelegate()); + assertFalse(restrictedConnection.isWrapperFor(TesterConnection.class)); + assertThrows(SQLException.class, () -> restrictedConnection.unwrap(TesterConnection.class)); + assertNotNull(permissiveWrapper.getDelegate()); + assertSame(permissiveWrapper.getInnermostDelegate(), permissiveConnection.unwrap(TesterConnection.class)); + } + assertEquals(0, driver.getConnectionPool("test").getNumActive()); + } + + @Test + void testBuilderDefaults() { + final Supplier<PoolingDriver> supplier = PoolingDriver.builder(); + final PoolingDriver first = supplier.get(); + final PoolingDriver second = supplier.get(); + assertTrue(first.isAccessToUnderlyingConnectionAllowed()); + assertTrue(second.isAccessToUnderlyingConnectionAllowed()); + assertNotSame(first, second); + final PoolingDriver.Builder restrictedBuilder = PoolingDriver.builder().setAccessToUnderlyingConnectionAllowed(false); + assertTrue(PoolingDriver.builder().get().isAccessToUnderlyingConnectionAllowed()); + assertFalse(restrictedBuilder.get().isAccessToUnderlyingConnectionAllowed()); + } + @Test void testClosePool() throws Exception { final Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:test"); diff --git a/src/test/java/org/apache/commons/dbcp2/TestUnderlyingConnectionAccess.java b/src/test/java/org/apache/commons/dbcp2/TestUnderlyingConnectionAccess.java index ee729988..1af74dfc 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestUnderlyingConnectionAccess.java +++ b/src/test/java/org/apache/commons/dbcp2/TestUnderlyingConnectionAccess.java @@ -241,7 +241,7 @@ class TestUnderlyingConnectionAccess { dataSource.setAccessToUnderlyingConnectionAllowed(allowed); return new Fixture(dataSource.getConnection(), dataSource, nested); } - final PoolingDriver driver = new PoolingDriver(allowed); + final PoolingDriver driver = PoolingDriver.builder().setAccessToUnderlyingConnectionAllowed(allowed).get(); final String name = "underlying-access-" + IDS.incrementAndGet(); driver.registerPool(name, pool); return new Fixture(driver.connect(PoolingDriver.URL_PREFIX + name, new Properties()), () -> driver.closePool(name), nested);
