This commit is contained in:
Looly 2024-05-13 17:32:49 +08:00
parent cf44ae005a
commit 4dd0b15da7
7 changed files with 93 additions and 34 deletions

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.db.ds;
import org.dromara.hutool.core.lang.Assert;
import javax.sql.DataSource;
/**
* 抽象数据源工厂
*
* @author looly
*/
public abstract class AbstractDSFactory implements DSFactory {
private static final long serialVersionUID = 1L;
private final String dataSourceName;
/**
* 构造
*
* @param dataSourceClass 数据库连接池实现类用于检测所提供的DataSource类是否存在当传入的DataSource类不存在时抛出ClassNotFoundException<br>
* 此参数的作用是在detectDSFactory方法自动检测所用连接池时如果实现类不存在调用此方法会自动抛出异常从而切换到下一种连接池的检测
* @param dataSourceName 数据源名称
*/
public AbstractDSFactory(final Class<? extends DataSource> dataSourceClass, final String dataSourceName) {
//此参数的作用是在detectDSFactory方法自动检测所用连接池时如果实现类不存在调用此方法会自动抛出异常从而切换到下一种连接池的检测
Assert.notNull(dataSourceClass);
this.dataSourceName = dataSourceName;
}
@Override
public String getDataSourceName() {
return this.dataSourceName;
}
}

View File

@ -14,7 +14,7 @@ package org.dromara.hutool.db.ds.bee;
import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.db.config.ConnectionConfig; import org.dromara.hutool.db.config.ConnectionConfig;
import org.dromara.hutool.db.ds.DSFactory; import org.dromara.hutool.db.ds.AbstractDSFactory;
import org.dromara.hutool.setting.props.Props; import org.dromara.hutool.setting.props.Props;
import org.stone.beecp.BeeDataSource; import org.stone.beecp.BeeDataSource;
import org.stone.beecp.BeeDataSourceConfig; import org.stone.beecp.BeeDataSourceConfig;
@ -27,12 +27,14 @@ import java.util.Properties;
* *
* @author Looly * @author Looly
*/ */
public class BeeDSFactory implements DSFactory { public class BeeDSFactory extends AbstractDSFactory {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Override /**
public String getDataSourceName() { * 构造
return "BeeCP"; */
public BeeDSFactory() {
super(BeeDataSource.class, "BeeCP");
} }
@Override @Override

View File

@ -16,7 +16,7 @@ import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.DbException;
import org.dromara.hutool.db.config.ConnectionConfig; import org.dromara.hutool.db.config.ConnectionConfig;
import org.dromara.hutool.db.ds.DSFactory; import org.dromara.hutool.db.ds.AbstractDSFactory;
import org.dromara.hutool.setting.props.Props; import org.dromara.hutool.setting.props.Props;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -29,14 +29,17 @@ import java.util.Properties;
* @author Looly * @author Looly
* *
*/ */
public class C3p0DSFactory implements DSFactory { public class C3p0DSFactory extends AbstractDSFactory {
private static final long serialVersionUID = -6090788225842047281L; private static final long serialVersionUID = 1L;
@Override /**
public String getDataSourceName() { * 构造
return "C3P0"; */
public C3p0DSFactory() {
super(ComboPooledDataSource.class, "C3P0");
} }
@Override @Override
public DataSource createDataSource(final ConnectionConfig<?> config) { public DataSource createDataSource(final ConnectionConfig<?> config) {
final ComboPooledDataSource ds = new ComboPooledDataSource(); final ComboPooledDataSource ds = new ComboPooledDataSource();

View File

@ -15,7 +15,7 @@ package org.dromara.hutool.db.ds.dbcp;
import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSource;
import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.db.config.ConnectionConfig; import org.dromara.hutool.db.config.ConnectionConfig;
import org.dromara.hutool.db.ds.DSFactory; import org.dromara.hutool.db.ds.AbstractDSFactory;
import org.dromara.hutool.setting.props.Props; import org.dromara.hutool.setting.props.Props;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -27,12 +27,14 @@ import java.util.Properties;
* @author Looly * @author Looly
* *
*/ */
public class DbcpDSFactory implements DSFactory { public class DbcpDSFactory extends AbstractDSFactory {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Override /**
public String getDataSourceName() { * 构造
return "commons-dbcp2"; */
public DbcpDSFactory() {
super(BasicDataSource.class, "commons-dbcp2");
} }
@Override @Override

View File

@ -16,7 +16,7 @@ import com.alibaba.druid.pool.DruidDataSource;
import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.db.config.ConnectionConfig; import org.dromara.hutool.db.config.ConnectionConfig;
import org.dromara.hutool.db.ds.DSFactory; import org.dromara.hutool.db.ds.AbstractDSFactory;
import org.dromara.hutool.setting.props.Props; import org.dromara.hutool.setting.props.Props;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -28,12 +28,14 @@ import java.util.Properties;
* @author Looly * @author Looly
* *
*/ */
public class DruidDSFactory implements DSFactory { public class DruidDSFactory extends AbstractDSFactory {
private static final long serialVersionUID = 4680621702534433222L; private static final long serialVersionUID = 1L;
@Override /**
public String getDataSourceName() { * 构造
return "Druid"; */
public DruidDSFactory() {
super(DruidDataSource.class, "Druid");
} }
@Override @Override

View File

@ -16,7 +16,7 @@ import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.db.config.ConnectionConfig; import org.dromara.hutool.db.config.ConnectionConfig;
import org.dromara.hutool.db.ds.DSFactory; import org.dromara.hutool.db.ds.AbstractDSFactory;
import org.dromara.hutool.setting.props.Props; import org.dromara.hutool.setting.props.Props;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -28,12 +28,14 @@ import java.util.Properties;
* @author Looly * @author Looly
* *
*/ */
public class HikariDSFactory implements DSFactory { public class HikariDSFactory extends AbstractDSFactory {
private static final long serialVersionUID = -8834744983614749401L; private static final long serialVersionUID = 1L;
@Override /**
public String getDataSourceName() { * 构造
return "HikariCP"; */
public HikariDSFactory() {
super(HikariDataSource.class, "HikariCP");
} }
@Override @Override

View File

@ -16,7 +16,7 @@ import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties; import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.db.config.ConnectionConfig; import org.dromara.hutool.db.config.ConnectionConfig;
import org.dromara.hutool.db.ds.DSFactory; import org.dromara.hutool.db.ds.AbstractDSFactory;
import org.dromara.hutool.setting.props.Props; import org.dromara.hutool.setting.props.Props;
import java.util.Properties; import java.util.Properties;
@ -26,12 +26,14 @@ import java.util.Properties;
* *
* @author Looly * @author Looly
*/ */
public class TomcatDSFactory implements DSFactory { public class TomcatDSFactory extends AbstractDSFactory {
private static final long serialVersionUID = 4925514193275150156L; private static final long serialVersionUID = 1L;
@Override /**
public String getDataSourceName() { * 构造
return "Tomcat-Jdbc-Pool"; */
public TomcatDSFactory() {
super(DataSource.class, "Tomcat-Jdbc-Pool");
} }
@Override @Override