Skip to content

Commit da131f3

Browse files
authored
Feat:增加对c3p0数据源支持 (#683)
* feat:增加对c3p0数据源支持
1 parent f93fff4 commit da131f3

File tree

9 files changed

+140
-2
lines changed

9 files changed

+140
-2
lines changed

dynamic-datasource-creator/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
<artifactId>beecp</artifactId>
2929
<optional>true</optional>
3030
</dependency>
31+
<dependency>
32+
<groupId>com.mchange</groupId>
33+
<artifactId>c3p0</artifactId>
34+
<optional>true</optional>
35+
</dependency>
3136
<dependency>
3237
<groupId>org.apache.commons</groupId>
3338
<artifactId>commons-dbcp2</artifactId>

dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/DataSourceProperty.java

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.baomidou.dynamic.datasource.creator.atomikos.AtomikosConfig;
1919
import com.baomidou.dynamic.datasource.creator.beecp.BeeCpConfig;
20+
import com.baomidou.dynamic.datasource.creator.c3p0.C3p0Config;
2021
import com.baomidou.dynamic.datasource.creator.dbcp.Dbcp2Config;
2122
import com.baomidou.dynamic.datasource.creator.druid.DruidConfig;
2223
import com.baomidou.dynamic.datasource.creator.hikaricp.HikariCpConfig;
@@ -97,6 +98,10 @@ public class DataSourceProperty {
9798
* atomikos参数配置
9899
*/
99100
private AtomikosConfig atomikos = new AtomikosConfig();
101+
/**
102+
* c3p0参数配置
103+
*/
104+
private C3p0Config c3p0 = new C3p0Config();
100105

101106
/**
102107
* 解密公匙(如果未设置默认使用全局的)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baomidou.dynamic.datasource.creator.c3p0;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
7+
/**
8+
* @author Joy
9+
*/
10+
@Getter
11+
@Setter
12+
public class C3p0Config {
13+
private Integer acquireIncrement;
14+
private Integer acquireRetryAttempts;
15+
private Integer acquireRetryDelay;
16+
private Boolean attemptResurrectOnCheckin;
17+
private Boolean autoCommitOnClose;
18+
private String automaticTestTable;
19+
private Boolean breakAfterAcquireFailure;
20+
private Integer checkoutTimeout;
21+
private String connectionCustomizerClassName;
22+
private Integer connectionIsValidTimeout;
23+
private String connectionTesterClassName;
24+
private String contextClassLoaderSource;
25+
private Boolean debugUnreturnedConnectionStackTraces;
26+
private String factoryClassLocation;
27+
private Boolean forceIgnoreUnresolvedTransactions;
28+
private Boolean forceSynchronousCheckins;
29+
private Integer idleConnectionTestPeriod;
30+
private Integer initialPoolSize;
31+
private String markSessionBoundaries;
32+
private Integer maxAdministrativeTaskTime;
33+
private Integer maxConnectionAge;
34+
private Integer maxIdleTime;
35+
private Integer maxIdleTimeExcessConnections;
36+
private Integer maxPoolSize;
37+
private Integer maxStatements;
38+
private Integer maxStatementsPerConnection;
39+
private Integer minPoolSize;
40+
private String overrideDefaultPassword;
41+
private String overrideDefaultUser;
42+
private String preferredTestQuery;
43+
private Boolean privilegeSpawnedThreads;
44+
private Integer propertyCycle;
45+
private Integer statementCacheNumDeferredCloseThreads;
46+
private String taskRunnerFactoryClassName;
47+
private Boolean testConnectionOnCheckin;
48+
private Boolean testConnectionOnCheckout;
49+
private Integer unreturnedConnectionTimeout;
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baomidou.dynamic.datasource.creator.c3p0;
2+
3+
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
4+
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
5+
import com.baomidou.dynamic.datasource.enums.DdConstants;
6+
import com.baomidou.dynamic.datasource.toolkit.ConfigMergeCreator;
7+
import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
8+
import com.mchange.v2.c3p0.ComboPooledDataSource;
9+
import lombok.AllArgsConstructor;
10+
import lombok.NoArgsConstructor;
11+
import lombok.SneakyThrows;
12+
13+
import javax.sql.DataSource;
14+
15+
/**
16+
* @author Joy
17+
*/
18+
@NoArgsConstructor
19+
@AllArgsConstructor
20+
public class C3p0DataSourceCreator implements DataSourceCreator {
21+
22+
private static final ConfigMergeCreator<C3p0Config, ComboPooledDataSource> MERGE_CREATOR = new ConfigMergeCreator<>("C3p0", C3p0Config.class, ComboPooledDataSource.class);
23+
24+
private C3p0Config c3p0Config;
25+
26+
@SneakyThrows
27+
@Override
28+
public DataSource createDataSource(DataSourceProperty dataSourceProperty) {
29+
ComboPooledDataSource dataSource = MERGE_CREATOR.create(c3p0Config, dataSourceProperty.getC3p0());
30+
dataSource.setUser(dataSourceProperty.getUsername());
31+
dataSource.setJdbcUrl(dataSourceProperty.getUrl());
32+
dataSource.setPassword(dataSourceProperty.getPassword());
33+
String driverClassName = dataSourceProperty.getDriverClassName();
34+
if (DsStrUtils.hasText(driverClassName)) {
35+
dataSource.setDriverClass(driverClassName);
36+
}
37+
return dataSource;
38+
}
39+
40+
@Override
41+
public boolean support(DataSourceProperty dataSourceProperty) {
42+
Class<? extends DataSource> type = dataSourceProperty.getType();
43+
return type == null || DdConstants.C3P0_DATASOURCE.equals(type.getName());
44+
}
45+
}

dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/enums/DdConstants.java

+4
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@ public interface DdConstants {
5252
* Atomikos数据源
5353
*/
5454
String ATOMIKOS_DATASOURCE = "com.atomikos.jdbc.AtomikosDataSourceBean";
55+
/**
56+
* C3P0数据源
57+
*/
58+
String C3P0_DATASOURCE = "com.mchange.v2.c3p0.ComboPooledDataSource";
5559
}

dynamic-datasource-spring-boot-common/pom.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@
4545
<artifactId>transactions-jdbc</artifactId>
4646
<optional>true</optional>
4747
</dependency>
48-
48+
<dependency>
49+
<groupId>com.mchange</groupId>
50+
<artifactId>c3p0</artifactId>
51+
<optional>true</optional>
52+
</dependency>
4953
<dependency>
5054
<groupId>com.baomidou</groupId>
5155
<artifactId>mybatis-plus</artifactId>

dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceCreatorAutoConfiguration.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
import com.baomidou.dynamic.datasource.creator.atomikos.AtomikosDataSourceCreator;
2323
import com.baomidou.dynamic.datasource.creator.basic.BasicDataSourceCreator;
2424
import com.baomidou.dynamic.datasource.creator.beecp.BeeCpDataSourceCreator;
25+
import com.baomidou.dynamic.datasource.creator.c3p0.C3p0DataSourceCreator;
2526
import com.baomidou.dynamic.datasource.creator.dbcp.Dbcp2DataSourceCreator;
2627
import com.baomidou.dynamic.datasource.creator.druid.DruidConfig;
2728
import com.baomidou.dynamic.datasource.creator.druid.DruidDataSourceCreator;
2829
import com.baomidou.dynamic.datasource.creator.hikaricp.HikariDataSourceCreator;
2930
import com.baomidou.dynamic.datasource.creator.jndi.JndiDataSourceCreator;
3031
import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
3132
import com.baomidou.dynamic.datasource.tx.AtomikosTransactionFactory;
33+
import com.mchange.v2.c3p0.ComboPooledDataSource;
3234
import com.zaxxer.hikari.HikariDataSource;
3335
import lombok.extern.slf4j.Slf4j;
3436
import org.apache.commons.dbcp2.BasicDataSource;
@@ -55,7 +57,8 @@ public class DynamicDataSourceCreatorAutoConfiguration {
5557
public static final int BEECP_ORDER = 4000;
5658
public static final int DBCP2_ORDER = 5000;
5759
public static final int ATOMIKOS_ORDER = 6000;
58-
public static final int DEFAULT_ORDER = 7000;
60+
public static final int C3P0_ORDER = 7000;
61+
public static final int DEFAULT_ORDER = 8000;
5962

6063
@Bean
6164
@Order(DEFAULT_ORDER)
@@ -161,4 +164,14 @@ public TransactionFactory atomikosTransactionFactory() {
161164
}
162165

163166
}
167+
168+
@ConditionalOnClass({ComboPooledDataSource.class})
169+
@Configuration
170+
static class C3p0DataSourceCreatorConfiguration {
171+
@Bean
172+
@Order(C3P0_ORDER)
173+
public C3p0DataSourceCreator c3p0DataSourceCreator(DynamicDataSourceProperties properties) {
174+
return new C3p0DataSourceCreator(properties.getC3p0());
175+
}
176+
}
164177
}

dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceProperties.java

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
1919
import com.baomidou.dynamic.datasource.creator.atomikos.AtomikosConfig;
2020
import com.baomidou.dynamic.datasource.creator.beecp.BeeCpConfig;
21+
import com.baomidou.dynamic.datasource.creator.c3p0.C3p0Config;
2122
import com.baomidou.dynamic.datasource.creator.dbcp.Dbcp2Config;
2223
import com.baomidou.dynamic.datasource.creator.druid.DruidConfig;
2324
import com.baomidou.dynamic.datasource.creator.hikaricp.HikariCpConfig;
@@ -113,6 +114,11 @@ public class DynamicDataSourceProperties {
113114
*/
114115
@NestedConfigurationProperty
115116
private AtomikosConfig atomikos = new AtomikosConfig();
117+
/**
118+
* c3p0全局参数配置
119+
*/
120+
@NestedConfigurationProperty
121+
private C3p0Config c3p0 = new C3p0Config();
116122

117123
/**
118124
* aop with default ds annotation

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<lombok.version>1.18.30</lombok.version>
6363
<h2.version>2.2.224</h2.version>
6464
<atomikos.version>4.0.6</atomikos.version>
65+
<c3p0.version>0.10.1</c3p0.version>
6566
<junit-jupiter.version>5.10.1</junit-jupiter.version>
6667

6768
<maven-clean-plugin.version>3.3.2</maven-clean-plugin.version>
@@ -133,6 +134,11 @@
133134
<artifactId>transactions-jdbc</artifactId>
134135
<version>${atomikos.version}</version>
135136
</dependency>
137+
<dependency>
138+
<groupId>com.mchange</groupId>
139+
<artifactId>c3p0</artifactId>
140+
<version>${c3p0.version}</version>
141+
</dependency>
136142
<dependency>
137143
<groupId>com.github.chris2018998</groupId>
138144
<artifactId>beecp</artifactId>

0 commit comments

Comments
 (0)