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 fc5af32858f90b37c7bed752b83927212a3f3ab2 Author: Gary Gregory <[email protected]> AuthorDate: Fri Nov 20 11:29:26 2026 -0500 Document JDBC driver discovery and trust in public Javadocs Expand BasicDataSource.createConnectionFactory() documentation and add package FAQ entries covering JDBC versions, driver selection, and the limits of driver type validation. Increase the Checkstyle file-length limit to accommodate the documentation. --- src/conf/checkstyle.xml | 2 +- .../org/apache/commons/dbcp2/BasicDataSource.java | 45 +++++++++++++++-- .../org/apache/commons/dbcp2/package-info.java | 57 ++++++++++++++++++++++ 3 files changed, 100 insertions(+), 4 deletions(-) diff --git a/src/conf/checkstyle.xml b/src/conf/checkstyle.xml index ab7ebd73..c9b1eb60 100644 --- a/src/conf/checkstyle.xml +++ b/src/conf/checkstyle.xml @@ -20,7 +20,7 @@ <!-- See http://checkstyle.sf.net/config_sizes.html --> <module name="FileLength"> <!-- Increase default --> - <property name="max" value="2700" /> + <property name="max" value="2800" /> </module> <!-- No tabs allowed! --> <module name="FileTabCharacter" /> diff --git a/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java b/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java index a56ff22b..fe2cd7b9 100644 --- a/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java +++ b/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java @@ -458,21 +458,60 @@ public class BasicDataSource implements DataSource, BasicDataSourceMXBean, MBean } /** - * Creates a JDBC connection factory for this data source. The JDBC driver is loaded using the following algorithm: + * Creates a JDBC connection factory for this data source. + * + * <p> + * The JDBC driver is selected using the following algorithm: + * </p> * <ol> - * <li>If a Driver instance has been specified via {@link #setDriver(Driver)}, use it.</li> + * <li>If a {@link Driver} instance has been specified via {@link #setDriver(Driver)}, use it.</li> * <li>If no Driver instance was specified and {@code driverClassName} is specified, load that class without initialization using the * {@link ClassLoader} that loaded DBCP or the {@code driverClassLoader}, if set.</li> * <li>If the class cannot be found with that loader, try the context class loader of the current thread.</li> * <li>Check that the loaded class implements {@link Driver} before initializing and instantiating it, then check that it accepts the configured URL.</li> * <li>If neither a Driver instance nor a class name was specified, use {@link DriverManager#getDriver(String)} with the configured URL.</li> * </ol> + * + * <p> + * Before JDBC 4.0, applications using {@link DriverManager} generally arranged for drivers to be loaded explicitly, commonly through + * {@link Class#forName(String)} or the {@code jdbc.drivers} system property. Drivers then registered themselves with {@link DriverManager}. This was the + * usual model for JDBC 1.x, 2.x, and 3.0. JDBC 3.0 was introduced in J2SE 1.4 and remained the JDBC version in Java 5. + * </p> + * + * <p> + * JDBC 4.0, introduced in Java SE 6, added automatic driver discovery through Java's service-provider mechanism. A driver JAR on the classpath declares + * its driver implementations in {@code META-INF/services/java.sql.Driver}. Provided the service metadata and driver classes are visible to the relevant + * class loaders, applications using {@link DriverManager} normally supply the JDBC URL and any required credentials or connection properties without + * explicitly naming or loading the driver class. Explicit loading remains supported. JDBC 4.1 (Java 7), 4.2 (Java 8), and 4.3 (Java 9) retained automatic + * discovery while adding other JDBC capabilities. + * </p> + * + * <p> + * URL-based selection predates JDBC 4.0 and uses drivers that are registered and accessible to the caller. A driver JAR can contain multiple + * implementations for compatibility or specialized use cases. If several drivers accept the same JDBC URL, that URL alone does not uniquely identify + * the intended implementation. Tools and deployment environments may still require an explicit driver class name, including with modern JDBC drivers. + * </p> + * + * <p> + * Whether supplied through a user interface, obtained as a Maven dependency, or made available through another build or deployment process, a driver + * JAR contains executable code chosen for the application to load. Automatic discovery neither downloads the driver nor establishes its trustworthiness. + * Developers and administrators are responsible for selecting trusted dependencies and controlling who can change driver configuration. + * </p> + * + * <p> + * When this method loads a class by name, the type check prevents unrelated classes from being initialized or instantiated through this driver-creation + * path. A malicious implementation of {@link Driver} can still pass the check and execute code with the privileges available to it. Type validation + * does not establish that a driver is safe, and DBCP does not provide a sandbox for driver implementations. The driver JAR must therefore be trusted. + * </p> + * * <p> * This method exists so subclasses can replace the implementation class. * </p> * * @return A new connection factory. - * @throws SQLException If the connection factory cannot be created + * @throws SQLException If the connection factory cannot be created. + * @see DriverManager + * @see <a href="https://docs.oracle.com/javase/9/docs/api/java/sql/package-summary.html">JDBC version history and features</a> */ protected ConnectionFactory createConnectionFactory() throws SQLException { // Load the JDBC driver class diff --git a/src/main/java/org/apache/commons/dbcp2/package-info.java b/src/main/java/org/apache/commons/dbcp2/package-info.java index b287b8af..5814c95a 100644 --- a/src/main/java/org/apache/commons/dbcp2/package-info.java +++ b/src/main/java/org/apache/commons/dbcp2/package-info.java @@ -124,5 +124,62 @@ * <pre> * Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example"); * </pre> + * <p> + * Q: Do I need to specify or explicitly load a JDBC driver class? + * </p> + * <p> + * A: Usually not with JDBC 4.0 or later when using {@link java.sql.DriverManager}. JDBC 4.0, introduced in Java SE 6, + * added automatic driver discovery through Java's service-provider mechanism. A driver JAR on the classpath declares + * its driver implementations in {@code META-INF/services/java.sql.Driver}. Provided the service metadata and driver + * classes are visible to the relevant class loaders, the application normally supplies the JDBC URL and any required + * credentials or connection properties without explicitly naming or loading the driver class. + * </p> + * <p> + * Before JDBC 4.0, applications using {@link java.sql.DriverManager} generally arranged for drivers to be loaded + * explicitly, commonly through {@link java.lang.Class#forName(String)} or the {@code jdbc.drivers} system property. + * Drivers then registered themselves with {@link java.sql.DriverManager}. This was the usual model for JDBC 1.x, + * 2.x, and 3.0. JDBC 3.0 was introduced in J2SE 1.4 and remained the JDBC version in Java 5. + * </p> + * <p> + * JDBC 4.1 (Java 7), 4.2 (Java 8), and 4.3 (Java 9) retained automatic discovery while adding other JDBC capabilities. + * Explicit loading remains supported for compatibility. Tools and deployment environments may still require a driver + * class name, including with modern JDBC drivers. See the + * <a href="https://docs.oracle.com/javase/9/docs/api/java/sql/package-summary.html">JDBC version history and features</a>. + * </p> + * <p> + * Q: How does DBCP select a JDBC driver? + * </p> + * <p> + * A: When using {@link org.apache.commons.dbcp2.BasicDataSource}, a driver instance supplied through + * {@link org.apache.commons.dbcp2.BasicDataSource#setDriver(java.sql.Driver)} takes precedence over a class name + * supplied through {@link org.apache.commons.dbcp2.BasicDataSource#setDriverClassName(String)}. If neither is + * configured, DBCP uses {@link java.sql.DriverManager#getDriver(String)} to select an available driver for the + * configured JDBC URL. See {@link org.apache.commons.dbcp2.BasicDataSource#createConnectionFactory()} for details + * of class loading and driver validation. + * </p> + * <p> + * URL-based selection predates JDBC 4.0. Automatic discovery changes how drivers become available to + * {@link java.sql.DriverManager}; selection uses drivers that are registered and accessible to the caller. A driver + * JAR can contain multiple implementations for compatibility or specialized use cases. If several drivers accept + * the same JDBC URL, that URL alone does not uniquely identify the intended implementation. + * </p> + * <p> + * Q: Does DBCP make an untrusted JDBC driver safe to use? + * </p> + * <p> + * A: No. Whether a driver JAR is supplied through a user interface, obtained as a Maven dependency, or made available + * through another build or deployment process, it contains executable code chosen for the application to load. + * Automatic discovery neither downloads the driver nor establishes its trustworthiness. Developers and + * administrators are responsible for selecting trusted dependencies and controlling who can change driver + * configuration. + * </p> + * <p> + * When {@link org.apache.commons.dbcp2.BasicDataSource} loads a driver class by name, it checks that the class + * implements {@link java.sql.Driver} before initializing it or invoking its constructor. This hardening prevents + * unrelated classes from being initialized or instantiated through that driver-creation path. A malicious + * implementation of {@link java.sql.Driver} can still pass the check and execute code with the privileges available + * to it. Type validation does not establish that a driver is safe, and DBCP does not provide a sandbox for driver + * implementations. The driver JAR must therefore be trusted. + * </p> */ package org.apache.commons.dbcp2;
