Skip to content

Commit

Permalink
Feat:增加对c3p0数据源支持 (#683)
Browse files Browse the repository at this point in the history
* feat:增加对c3p0数据源支持
  • Loading branch information
Joydevelop authored Nov 7, 2024
1 parent f93fff4 commit da131f3
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 2 deletions.
5 changes: 5 additions & 0 deletions dynamic-datasource-creator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<artifactId>beecp</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,6 +98,10 @@ public class DataSourceProperty {
* atomikos参数配置
*/
private AtomikosConfig atomikos = new AtomikosConfig();
/**
* c3p0参数配置
*/
private C3p0Config c3p0 = new C3p0Config();

/**
* 解密公匙(如果未设置默认使用全局的)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<C3p0Config, ComboPooledDataSource> 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<? extends DataSource> type = dataSourceProperty.getType();
return type == null || DdConstants.C3P0_DATASOURCE.equals(type.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ public interface DdConstants {
* Atomikos数据源
*/
String ATOMIKOS_DATASOURCE = "com.atomikos.jdbc.AtomikosDataSourceBean";
/**
* C3P0数据源
*/
String C3P0_DATASOURCE = "com.mchange.v2.c3p0.ComboPooledDataSource";
}
6 changes: 5 additions & 1 deletion dynamic-datasource-spring-boot-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
<artifactId>transactions-jdbc</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
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;
import com.baomidou.dynamic.datasource.creator.hikaricp.HikariDataSourceCreator;
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;
Expand All @@ -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)
Expand Down Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<lombok.version>1.18.30</lombok.version>
<h2.version>2.2.224</h2.version>
<atomikos.version>4.0.6</atomikos.version>
<c3p0.version>0.10.1</c3p0.version>
<junit-jupiter.version>5.10.1</junit-jupiter.version>

<maven-clean-plugin.version>3.3.2</maven-clean-plugin.version>
Expand Down Expand Up @@ -133,6 +134,11 @@
<artifactId>transactions-jdbc</artifactId>
<version>${atomikos.version}</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<dependency>
<groupId>com.github.chris2018998</groupId>
<artifactId>beecp</artifactId>
Expand Down

0 comments on commit da131f3

Please sign in to comment.