diff --git a/dynamic-datasource-creator/pom.xml b/dynamic-datasource-creator/pom.xml index 1b22a678..d880f5a5 100644 --- a/dynamic-datasource-creator/pom.xml +++ b/dynamic-datasource-creator/pom.xml @@ -28,6 +28,11 @@ beecp true + + com.mchange + c3p0 + true + org.apache.commons commons-dbcp2 diff --git a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/DataSourceProperty.java b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/DataSourceProperty.java index c0317aef..61190bd6 100644 --- a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/DataSourceProperty.java +++ b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/DataSourceProperty.java @@ -17,6 +17,7 @@ import com.baomidou.dynamic.datasource.creator.atomikos.AtomikosConfig; import com.baomidou.dynamic.datasource.creator.beecp.BeeCpConfig; +import com.baomidou.dynamic.datasource.creator.c3p0.C3p0Config; import com.baomidou.dynamic.datasource.creator.dbcp.Dbcp2Config; import com.baomidou.dynamic.datasource.creator.druid.DruidConfig; import com.baomidou.dynamic.datasource.creator.hikaricp.HikariCpConfig; @@ -97,6 +98,10 @@ public class DataSourceProperty { * atomikos参数配置 */ private AtomikosConfig atomikos = new AtomikosConfig(); + /** + * c3p0参数配置 + */ + private C3p0Config c3p0 = new C3p0Config(); /** * 解密公匙(如果未设置默认使用全局的) diff --git a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/c3p0/C3p0Config.java b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/c3p0/C3p0Config.java new file mode 100644 index 00000000..06b5077d --- /dev/null +++ b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/c3p0/C3p0Config.java @@ -0,0 +1,50 @@ +package com.baomidou.dynamic.datasource.creator.c3p0; + +import lombok.Getter; +import lombok.Setter; + + +/** + * @author Joy + */ +@Getter +@Setter +public class C3p0Config { + private Integer acquireIncrement; + private Integer acquireRetryAttempts; + private Integer acquireRetryDelay; + private Boolean attemptResurrectOnCheckin; + private Boolean autoCommitOnClose; + private String automaticTestTable; + private Boolean breakAfterAcquireFailure; + private Integer checkoutTimeout; + private String connectionCustomizerClassName; + private Integer connectionIsValidTimeout; + private String connectionTesterClassName; + private String contextClassLoaderSource; + private Boolean debugUnreturnedConnectionStackTraces; + private String factoryClassLocation; + private Boolean forceIgnoreUnresolvedTransactions; + private Boolean forceSynchronousCheckins; + private Integer idleConnectionTestPeriod; + private Integer initialPoolSize; + private String markSessionBoundaries; + private Integer maxAdministrativeTaskTime; + private Integer maxConnectionAge; + private Integer maxIdleTime; + private Integer maxIdleTimeExcessConnections; + private Integer maxPoolSize; + private Integer maxStatements; + private Integer maxStatementsPerConnection; + private Integer minPoolSize; + private String overrideDefaultPassword; + private String overrideDefaultUser; + private String preferredTestQuery; + private Boolean privilegeSpawnedThreads; + private Integer propertyCycle; + private Integer statementCacheNumDeferredCloseThreads; + private String taskRunnerFactoryClassName; + private Boolean testConnectionOnCheckin; + private Boolean testConnectionOnCheckout; + private Integer unreturnedConnectionTimeout; +} diff --git a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/c3p0/C3p0DataSourceCreator.java b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/c3p0/C3p0DataSourceCreator.java new file mode 100644 index 00000000..dae9b0c1 --- /dev/null +++ b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/creator/c3p0/C3p0DataSourceCreator.java @@ -0,0 +1,45 @@ +package com.baomidou.dynamic.datasource.creator.c3p0; + +import com.baomidou.dynamic.datasource.creator.DataSourceCreator; +import com.baomidou.dynamic.datasource.creator.DataSourceProperty; +import com.baomidou.dynamic.datasource.enums.DdConstants; +import com.baomidou.dynamic.datasource.toolkit.ConfigMergeCreator; +import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; +import com.mchange.v2.c3p0.ComboPooledDataSource; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +import lombok.SneakyThrows; + +import javax.sql.DataSource; + +/** + * @author Joy + */ +@NoArgsConstructor +@AllArgsConstructor +public class C3p0DataSourceCreator implements DataSourceCreator { + + private static final ConfigMergeCreator MERGE_CREATOR = new ConfigMergeCreator<>("C3p0", C3p0Config.class, ComboPooledDataSource.class); + + private C3p0Config c3p0Config; + + @SneakyThrows + @Override + public DataSource createDataSource(DataSourceProperty dataSourceProperty) { + ComboPooledDataSource dataSource = MERGE_CREATOR.create(c3p0Config, dataSourceProperty.getC3p0()); + dataSource.setUser(dataSourceProperty.getUsername()); + dataSource.setJdbcUrl(dataSourceProperty.getUrl()); + dataSource.setPassword(dataSourceProperty.getPassword()); + String driverClassName = dataSourceProperty.getDriverClassName(); + if (DsStrUtils.hasText(driverClassName)) { + dataSource.setDriverClass(driverClassName); + } + return dataSource; + } + + @Override + public boolean support(DataSourceProperty dataSourceProperty) { + Class type = dataSourceProperty.getType(); + return type == null || DdConstants.C3P0_DATASOURCE.equals(type.getName()); + } +} diff --git a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/enums/DdConstants.java b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/enums/DdConstants.java index a2941799..173ad477 100644 --- a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/enums/DdConstants.java +++ b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/enums/DdConstants.java @@ -52,4 +52,8 @@ public interface DdConstants { * Atomikos数据源 */ String ATOMIKOS_DATASOURCE = "com.atomikos.jdbc.AtomikosDataSourceBean"; + /** + * C3P0数据源 + */ + String C3P0_DATASOURCE = "com.mchange.v2.c3p0.ComboPooledDataSource"; } \ No newline at end of file diff --git a/dynamic-datasource-spring-boot-common/pom.xml b/dynamic-datasource-spring-boot-common/pom.xml index 9a4aadb5..817d4b7c 100644 --- a/dynamic-datasource-spring-boot-common/pom.xml +++ b/dynamic-datasource-spring-boot-common/pom.xml @@ -45,7 +45,11 @@ transactions-jdbc true - + + com.mchange + c3p0 + true + com.baomidou mybatis-plus diff --git a/dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceCreatorAutoConfiguration.java b/dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceCreatorAutoConfiguration.java index 421e1bcf..f644e04c 100644 --- a/dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceCreatorAutoConfiguration.java +++ b/dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceCreatorAutoConfiguration.java @@ -22,6 +22,7 @@ import com.baomidou.dynamic.datasource.creator.atomikos.AtomikosDataSourceCreator; import com.baomidou.dynamic.datasource.creator.basic.BasicDataSourceCreator; import com.baomidou.dynamic.datasource.creator.beecp.BeeCpDataSourceCreator; +import com.baomidou.dynamic.datasource.creator.c3p0.C3p0DataSourceCreator; import com.baomidou.dynamic.datasource.creator.dbcp.Dbcp2DataSourceCreator; import com.baomidou.dynamic.datasource.creator.druid.DruidConfig; import com.baomidou.dynamic.datasource.creator.druid.DruidDataSourceCreator; @@ -29,6 +30,7 @@ import com.baomidou.dynamic.datasource.creator.jndi.JndiDataSourceCreator; import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import com.baomidou.dynamic.datasource.tx.AtomikosTransactionFactory; +import com.mchange.v2.c3p0.ComboPooledDataSource; import com.zaxxer.hikari.HikariDataSource; import lombok.extern.slf4j.Slf4j; import org.apache.commons.dbcp2.BasicDataSource; @@ -55,7 +57,8 @@ public class DynamicDataSourceCreatorAutoConfiguration { public static final int BEECP_ORDER = 4000; public static final int DBCP2_ORDER = 5000; public static final int ATOMIKOS_ORDER = 6000; - public static final int DEFAULT_ORDER = 7000; + public static final int C3P0_ORDER = 7000; + public static final int DEFAULT_ORDER = 8000; @Bean @Order(DEFAULT_ORDER) @@ -161,4 +164,14 @@ public TransactionFactory atomikosTransactionFactory() { } } + + @ConditionalOnClass({ComboPooledDataSource.class}) + @Configuration + static class C3p0DataSourceCreatorConfiguration { + @Bean + @Order(C3P0_ORDER) + public C3p0DataSourceCreator c3p0DataSourceCreator(DynamicDataSourceProperties properties) { + return new C3p0DataSourceCreator(properties.getC3p0()); + } + } } \ No newline at end of file diff --git a/dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceProperties.java b/dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceProperties.java index 4692442a..0fc1f0a6 100644 --- a/dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceProperties.java +++ b/dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceProperties.java @@ -18,6 +18,7 @@ import com.baomidou.dynamic.datasource.creator.DataSourceProperty; import com.baomidou.dynamic.datasource.creator.atomikos.AtomikosConfig; import com.baomidou.dynamic.datasource.creator.beecp.BeeCpConfig; +import com.baomidou.dynamic.datasource.creator.c3p0.C3p0Config; import com.baomidou.dynamic.datasource.creator.dbcp.Dbcp2Config; import com.baomidou.dynamic.datasource.creator.druid.DruidConfig; import com.baomidou.dynamic.datasource.creator.hikaricp.HikariCpConfig; @@ -113,6 +114,11 @@ public class DynamicDataSourceProperties { */ @NestedConfigurationProperty private AtomikosConfig atomikos = new AtomikosConfig(); + /** + * c3p0全局参数配置 + */ + @NestedConfigurationProperty + private C3p0Config c3p0 = new C3p0Config(); /** * aop with default ds annotation diff --git a/pom.xml b/pom.xml index 3e0fc307..b8e9c1f5 100644 --- a/pom.xml +++ b/pom.xml @@ -62,6 +62,7 @@ 1.18.30 2.2.224 4.0.6 + 0.10.1 5.10.1 3.3.2 @@ -133,6 +134,11 @@ transactions-jdbc ${atomikos.version} + + com.mchange + c3p0 + ${c3p0.version} + com.github.chris2018998 beecp