Skip to content

Commit 3a5b406

Browse files
committed
Restore support for C3P0
Closes gh-31920
1 parent 2596d61 commit 3a5b406

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

Diff for: spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -253,7 +253,7 @@ void whenNoInitializationRelatedSpringDataSourcePropertiesAreConfiguredThenIniti
253253

254254
private static Function<ApplicationContextRunner, ApplicationContextRunner> hideConnectionPools() {
255255
return (runner) -> runner.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
256-
"org.apache.commons.dbcp2", "oracle.ucp.jdbc"));
256+
"org.apache.commons.dbcp2", "oracle.ucp.jdbc", "com.mchange"));
257257
}
258258

259259
private <T extends DataSource> void assertDataSource(Class<T> expectedType, List<String> hiddenPackages,

Diff for: spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/sql.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ The following connection pools are supported by `DataSourceBuilder`:
135135
* Spring Framework's `SimpleDriverDataSource`
136136
* H2 `JdbcDataSource`
137137
* PostgreSQL `PGSimpleDataSource`
138+
* C3P0
138139

139140

140141

Diff for: spring-boot-project/spring-boot-parent/build.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ bom {
2727
]
2828
}
2929
}
30+
library("C3P0", "0.9.5.5") {
31+
group("com.mchange") {
32+
modules = [
33+
"c3p0"
34+
]
35+
}
36+
}
3037
library("Commons Compress", "1.21") {
3138
group("org.apache.commons") {
3239
modules = [

Diff for: spring-boot-project/spring-boot/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies {
2828
optional("com.fasterxml.jackson.core:jackson-databind")
2929
optional("com.h2database:h2")
3030
optional("com.google.code.gson:gson")
31+
optional("com.mchange:c3p0")
3132
optional("com.oracle.database.jdbc:ucp")
3233
optional("com.oracle.database.jdbc:ojdbc8")
3334
optional("com.samskivert:jmustache")

Diff for: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java

+27
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.jdbc;
1818

19+
import java.beans.PropertyVetoException;
1920
import java.lang.reflect.Method;
2021
import java.sql.SQLException;
2122
import java.util.Collections;
@@ -27,6 +28,7 @@
2728

2829
import javax.sql.DataSource;
2930

31+
import com.mchange.v2.c3p0.ComboPooledDataSource;
3032
import com.zaxxer.hikari.HikariDataSource;
3133
import oracle.jdbc.datasource.OracleDataSource;
3234
import oracle.ucp.jdbc.PoolDataSource;
@@ -391,6 +393,8 @@ private static <T extends DataSource> MappedDataSourceProperties<T> lookupPooled
391393
MappedDbcp2DataSource::new);
392394
result = lookup(classLoader, type, result, "oracle.ucp.jdbc.PoolDataSourceImpl",
393395
OraclePoolDataSourceProperties::new, "oracle.jdbc.OracleConnection");
396+
result = lookup(classLoader, type, result, "com.mchange.v2.c3p0.ComboPooledDataSource",
397+
ComboPooledDataSourceProperties::new);
394398
return result;
395399
}
396400

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

651655
}
652656

657+
/**
658+
* {@link DataSourceProperties} for C3P0.
659+
*/
660+
private static class ComboPooledDataSourceProperties extends MappedDataSourceProperties<ComboPooledDataSource> {
661+
662+
ComboPooledDataSourceProperties() {
663+
add(DataSourceProperty.URL, ComboPooledDataSource::getJdbcUrl, ComboPooledDataSource::setJdbcUrl);
664+
add(DataSourceProperty.DRIVER_CLASS_NAME, ComboPooledDataSource::getDriverClass, this::setDriverClass);
665+
add(DataSourceProperty.USERNAME, ComboPooledDataSource::getUser, ComboPooledDataSource::setUser);
666+
add(DataSourceProperty.PASSWORD, ComboPooledDataSource::getPassword, ComboPooledDataSource::setPassword);
667+
}
668+
669+
private void setDriverClass(ComboPooledDataSource dataSource, String driverClass) {
670+
try {
671+
dataSource.setDriverClass(driverClass);
672+
}
673+
catch (PropertyVetoException ex) {
674+
throw new IllegalArgumentException(ex);
675+
}
676+
}
677+
678+
}
679+
653680
/**
654681
* {@link DataSourceProperties} for Spring's {@link SimpleDriverDataSource}.
655682
*/

Diff for: spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import javax.sql.DataSource;
2828

29+
import com.mchange.v2.c3p0.ComboPooledDataSource;
2930
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
3031
import com.zaxxer.hikari.HikariDataSource;
3132
import oracle.jdbc.internal.OpaqueString;
@@ -384,6 +385,19 @@ void buildWhenDerivedFromCustomTypeWithTypeChange() {
384385
assertThat(testSource.getPassword()).isEqualTo("secret");
385386
}
386387

388+
@Test // gh-31920
389+
void buildWhenC3P0TypeSpecifiedReturnsExpectedDataSource() {
390+
this.dataSource = DataSourceBuilder.create().url("jdbc:postgresql://localhost:5432/postgres")
391+
.type(ComboPooledDataSource.class).username("test").password("secret")
392+
.driverClassName("com.example.Driver").build();
393+
assertThat(this.dataSource).isInstanceOf(ComboPooledDataSource.class);
394+
ComboPooledDataSource c3p0DataSource = (ComboPooledDataSource) this.dataSource;
395+
assertThat(c3p0DataSource.getJdbcUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
396+
assertThat(c3p0DataSource.getUser()).isEqualTo("test");
397+
assertThat(c3p0DataSource.getPassword()).isEqualTo("secret");
398+
assertThat(c3p0DataSource.getDriverClass()).isEqualTo("com.example.Driver");
399+
}
400+
387401
final class HidePackagesClassLoader extends URLClassLoader {
388402

389403
private final String[] hiddenPackages;

0 commit comments

Comments
 (0)