Skip to content

Commit

Permalink
Merge branch '2.6.x' into 2.7.x
Browse files Browse the repository at this point in the history
Closes gh-31921
  • Loading branch information
snicoll committed Jul 29, 2022
2 parents 6d279ab + 3a5b406 commit 4806881
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
Expand Up @@ -240,7 +240,7 @@ void whenNoInitializationRelatedSpringDataSourcePropertiesAreConfiguredThenIniti

private static Function<ApplicationContextRunner, ApplicationContextRunner> hideConnectionPools() {
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,
Expand Down
Expand Up @@ -135,6 +135,7 @@ The following connection pools are supported by `DataSourceBuilder`:
* Spring Framework's `SimpleDriverDataSource`
* H2 `JdbcDataSource`
* PostgreSQL `PGSimpleDataSource`
* C3P0



Expand Down
7 changes: 7 additions & 0 deletions spring-boot-project/spring-boot-parent/build.gradle
Expand Up @@ -27,6 +27,13 @@ bom {
]
}
}
library("C3P0", "0.9.5.5") {
group("com.mchange") {
modules = [
"c3p0"
]
}
}
library("Commons Compress", "1.21") {
group("org.apache.commons") {
modules = [
Expand Down
1 change: 1 addition & 0 deletions spring-boot-project/spring-boot/build.gradle
Expand Up @@ -28,6 +28,7 @@ dependencies {
optional("com.fasterxml.jackson.core:jackson-databind")
optional("com.h2database:h2")
optional("com.google.code.gson:gson")
optional("com.mchange:c3p0")
optional("com.oracle.database.jdbc:ucp")
optional("com.oracle.database.jdbc:ojdbc8")
optional("com.samskivert:jmustache")
Expand Down
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.jdbc;

import java.beans.PropertyVetoException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.Collections;
Expand All @@ -27,6 +28,7 @@

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.zaxxer.hikari.HikariDataSource;
import oracle.jdbc.datasource.OracleDataSource;
import oracle.ucp.jdbc.PoolDataSource;
Expand Down Expand Up @@ -391,6 +393,8 @@ private static <T extends DataSource> MappedDataSourceProperties<T> lookupPooled
MappedDbcp2DataSource::new);
result = lookup(classLoader, type, result, "oracle.ucp.jdbc.PoolDataSourceImpl",
OraclePoolDataSourceProperties::new, "oracle.jdbc.OracleConnection");
result = lookup(classLoader, type, result, "com.mchange.v2.c3p0.ComboPooledDataSource",
ComboPooledDataSourceProperties::new);
return result;
}

Expand Down Expand Up @@ -650,6 +654,29 @@ public Class<? extends PoolDataSource> getDataSourceInstanceType() {

}

/**
* {@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}.
*/
Expand Down
Expand Up @@ -26,6 +26,7 @@

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.zaxxer.hikari.HikariDataSource;
import oracle.jdbc.internal.OpaqueString;
Expand Down Expand Up @@ -384,6 +385,19 @@ void buildWhenDerivedFromCustomTypeWithTypeChange() {
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");
}

final class HidePackagesClassLoader extends URLClassLoader {

private final String[] hiddenPackages;
Expand Down

0 comments on commit 4806881

Please sign in to comment.