Merge branch '2.7.x'

Closes gh-31922
pull/31928/head
Stephane Nicoll 2 years ago
commit 0e9f8a5994

@ -240,7 +240,7 @@ class DataSourceAutoConfigurationTests {
private static Function<ApplicationContextRunner, ApplicationContextRunner> hideConnectionPools() { private static Function<ApplicationContextRunner, ApplicationContextRunner> hideConnectionPools() {
return (runner) -> runner.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari", return (runner) -> runner.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
"org.apache.commons.dbcp2", "oracle.ucp.jdbc")); "org.apache.commons.dbcp2", "oracle.ucp.jdbc", "com.mchange"));
} }
private <T extends DataSource> void assertDataSource(Class<T> expectedType, List<String> hiddenPackages, private <T extends DataSource> void assertDataSource(Class<T> expectedType, List<String> hiddenPackages,

@ -135,6 +135,7 @@ The following connection pools are supported by `DataSourceBuilder`:
* Spring Framework's `SimpleDriverDataSource` * Spring Framework's `SimpleDriverDataSource`
* H2 `JdbcDataSource` * H2 `JdbcDataSource`
* PostgreSQL `PGSimpleDataSource` * PostgreSQL `PGSimpleDataSource`
* C3P0

@ -27,6 +27,13 @@ bom {
] ]
} }
} }
library("C3P0", "0.9.5.5") {
group("com.mchange") {
modules = [
"c3p0"
]
}
}
library("Commons Compress", "1.21") { library("Commons Compress", "1.21") {
group("org.apache.commons") { group("org.apache.commons") {
modules = [ modules = [

@ -25,6 +25,7 @@ dependencies {
optional("com.fasterxml.jackson.core:jackson-databind") optional("com.fasterxml.jackson.core:jackson-databind")
optional("com.h2database:h2") optional("com.h2database:h2")
optional("com.google.code.gson:gson") optional("com.google.code.gson:gson")
optional("com.mchange:c3p0")
optional("com.oracle.database.jdbc:ucp") optional("com.oracle.database.jdbc:ucp")
optional("com.oracle.database.jdbc:ojdbc8") optional("com.oracle.database.jdbc:ojdbc8")
optional("com.samskivert:jmustache") optional("com.samskivert:jmustache")

@ -16,6 +16,7 @@
package org.springframework.boot.jdbc; package org.springframework.boot.jdbc;
import java.beans.PropertyVetoException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collections; import java.util.Collections;
@ -27,6 +28,7 @@ import java.util.function.Supplier;
import javax.sql.DataSource; import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import oracle.jdbc.datasource.OracleDataSource; import oracle.jdbc.datasource.OracleDataSource;
import oracle.ucp.jdbc.PoolDataSource; import oracle.ucp.jdbc.PoolDataSource;
@ -419,6 +421,8 @@ public final class DataSourceBuilder<T extends DataSource> {
MappedDbcp2DataSource::new); MappedDbcp2DataSource::new);
result = lookup(classLoader, type, result, "oracle.ucp.jdbc.PoolDataSourceImpl", result = lookup(classLoader, type, result, "oracle.ucp.jdbc.PoolDataSourceImpl",
OraclePoolDataSourceProperties::new, "oracle.jdbc.OracleConnection"); OraclePoolDataSourceProperties::new, "oracle.jdbc.OracleConnection");
result = lookup(classLoader, type, result, "com.mchange.v2.c3p0.ComboPooledDataSource",
ComboPooledDataSourceProperties::new);
return result; return result;
} }
@ -678,6 +682,29 @@ public final class DataSourceBuilder<T extends DataSource> {
} }
/**
* {@link DataSourceProperties} for C3P0.
*/
private static class ComboPooledDataSourceProperties extends MappedDataSourceProperties<ComboPooledDataSource> {
ComboPooledDataSourceProperties() {
add(DataSourceProperty.URL, ComboPooledDataSource::getJdbcUrl, ComboPooledDataSource::setJdbcUrl);
add(DataSourceProperty.DRIVER_CLASS_NAME, ComboPooledDataSource::getDriverClass, this::setDriverClass);
add(DataSourceProperty.USERNAME, ComboPooledDataSource::getUser, ComboPooledDataSource::setUser);
add(DataSourceProperty.PASSWORD, ComboPooledDataSource::getPassword, ComboPooledDataSource::setPassword);
}
private void setDriverClass(ComboPooledDataSource dataSource, String driverClass) {
try {
dataSource.setDriverClass(driverClass);
}
catch (PropertyVetoException ex) {
throw new IllegalArgumentException(ex);
}
}
}
/** /**
* {@link DataSourceProperties} for Spring's {@link SimpleDriverDataSource}. * {@link DataSourceProperties} for Spring's {@link SimpleDriverDataSource}.
*/ */

@ -29,6 +29,7 @@ import java.util.logging.Logger;
import javax.sql.DataSource; import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource; import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import oracle.jdbc.internal.OpaqueString; import oracle.jdbc.internal.OpaqueString;
@ -400,6 +401,19 @@ class DataSourceBuilderTests {
assertThat(testSource.getPassword()).isEqualTo("secret"); assertThat(testSource.getPassword()).isEqualTo("secret");
} }
@Test // gh-31920
void buildWhenC3P0TypeSpecifiedReturnsExpectedDataSource() {
this.dataSource = DataSourceBuilder.create().url("jdbc:postgresql://localhost:5432/postgres")
.type(ComboPooledDataSource.class).username("test").password("secret")
.driverClassName("com.example.Driver").build();
assertThat(this.dataSource).isInstanceOf(ComboPooledDataSource.class);
ComboPooledDataSource c3p0DataSource = (ComboPooledDataSource) this.dataSource;
assertThat(c3p0DataSource.getJdbcUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
assertThat(c3p0DataSource.getUser()).isEqualTo("test");
assertThat(c3p0DataSource.getPassword()).isEqualTo("secret");
assertThat(c3p0DataSource.getDriverClass()).isEqualTo("com.example.Driver");
}
private DataSource wrap(DataSource target) { private DataSource wrap(DataSource target) {
return new DataSourceWrapper(target); return new DataSourceWrapper(target);
} }

Loading…
Cancel
Save