Browse Source

Restore support for C3P0

Closes gh-31920
pull/31958/head
Stephane Nicoll 3 years ago
parent
commit
3a5b40645a
  1. 4
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java
  2. 1
      spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/sql.adoc
  3. 7
      spring-boot-project/spring-boot-parent/build.gradle
  4. 1
      spring-boot-project/spring-boot/build.gradle
  5. 27
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java
  6. 14
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java

4
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -253,7 +253,7 @@ class DataSourceAutoConfigurationTests { @@ -253,7 +253,7 @@ class DataSourceAutoConfigurationTests {
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,

1
spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/sql.adoc

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

7
spring-boot-project/spring-boot-parent/build.gradle

@ -27,6 +27,13 @@ bom { @@ -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 = [

1
spring-boot-project/spring-boot/build.gradle

@ -28,6 +28,7 @@ dependencies { @@ -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")

27
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java

@ -16,6 +16,7 @@ @@ -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;
@ -27,6 +28,7 @@ import java.util.function.Supplier; @@ -27,6 +28,7 @@ import java.util.function.Supplier;
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;
@ -391,6 +393,8 @@ public final class DataSourceBuilder<T extends DataSource> { @@ -391,6 +393,8 @@ public final class DataSourceBuilder<T extends DataSource> {
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;
}
@ -650,6 +654,29 @@ public final class DataSourceBuilder<T extends DataSource> { @@ -650,6 +654,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}.
*/

14
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java

@ -26,6 +26,7 @@ import java.util.Arrays; @@ -26,6 +26,7 @@ import java.util.Arrays;
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;
@ -384,6 +385,19 @@ class DataSourceBuilderTests { @@ -384,6 +385,19 @@ class DataSourceBuilderTests {
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;

Loading…
Cancel
Save