This commit is contained in:
Looly 2024-06-18 20:19:22 +08:00
parent fecbbb4ce8
commit 17433887e7
3 changed files with 53 additions and 5 deletions

View File

@ -13,14 +13,14 @@
package org.dromara.hutool.db.driver; package org.dromara.hutool.db.driver;
import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.reflect.ClassUtil;
import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.db.DbException; import org.dromara.hutool.db.DbException;
import org.dromara.hutool.db.ds.DSWrapper; import org.dromara.hutool.db.ds.DSWrapper;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.sql.Connection; import java.sql.*;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
/** /**
* 驱动相关工具类包括自动获取驱动类名 * 驱动相关工具类包括自动获取驱动类名
@ -29,6 +29,31 @@ import java.sql.SQLException;
* @since 4.0.10 * @since 4.0.10
*/ */
public class DriverUtil { public class DriverUtil {
/**
* 创建驱动
*
* @param driverName 驱动类名
* @return 驱动
* @since 6.0.0
*/
public static Driver createDriver(final String driverName) {
return createDriver(driverName, null);
}
/**
* 创建驱动
*
* @param driverName 驱动类名
* @param classLoader 类加载器
* @return 驱动
* @since 6.0.0
*/
public static Driver createDriver(final String driverName, final ClassLoader classLoader) {
final Class<?> driverClass = ClassUtil.forName(driverName, true, classLoader);
return (Driver) ConstructorUtil.newInstance(driverClass);
}
/** /**
* 通过JDBC URL等信息识别JDBC驱动名 * 通过JDBC URL等信息识别JDBC驱动名
* *

View File

@ -48,7 +48,7 @@ public class PooledConnection extends ConnectionWrapper implements Poolable<Conn
if(StrUtil.isNotBlank(driver)){ if(StrUtil.isNotBlank(driver)){
try { try {
Class.forName(driver); Class.forName(driver);
} catch (ClassNotFoundException e) { } catch (final ClassNotFoundException e) {
throw new DbException(e); throw new DbException(e);
} }
} }
@ -70,7 +70,11 @@ public class PooledConnection extends ConnectionWrapper implements Poolable<Conn
} }
try { try {
this.raw = DriverManager.getConnection(config.getUrl(), info); if(null != dataSource.driver){
this.raw = dataSource.driver.connect(config.getUrl(), info);
}else{
this.raw = DriverManager.getConnection(config.getUrl(), info);
}
} catch (final SQLException e) { } catch (final SQLException e) {
throw new DbException(e); throw new DbException(e);
} }

View File

@ -17,12 +17,15 @@ import org.dromara.hutool.core.pool.ObjectFactory;
import org.dromara.hutool.core.pool.ObjectPool; import org.dromara.hutool.core.pool.ObjectPool;
import org.dromara.hutool.core.pool.partition.PartitionObjectPool; import org.dromara.hutool.core.pool.partition.PartitionObjectPool;
import org.dromara.hutool.core.pool.partition.PartitionPoolConfig; import org.dromara.hutool.core.pool.partition.PartitionPoolConfig;
import org.dromara.hutool.core.text.StrUtil;
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.driver.DriverUtil;
import org.dromara.hutool.db.ds.simple.AbstractDataSource; import org.dromara.hutool.db.ds.simple.AbstractDataSource;
import org.dromara.hutool.setting.props.Props; import org.dromara.hutool.setting.props.Props;
import java.sql.Connection; import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException; import java.sql.SQLException;
/** /**
@ -37,6 +40,7 @@ public class PooledDataSource extends AbstractDataSource {
private static final String KEY_INITIAL_SIZE = "initialSize"; private static final String KEY_INITIAL_SIZE = "initialSize";
private static final String KEY_MAX_ACTIVE = "maxActive"; private static final String KEY_MAX_ACTIVE = "maxActive";
protected Driver driver;
private final int maxWait; private final int maxWait;
private final ObjectPool<Connection> connPool; private final ObjectPool<Connection> connPool;
@ -47,6 +51,10 @@ public class PooledDataSource extends AbstractDataSource {
*/ */
public PooledDataSource(final ConnectionConfig<?> config) { public PooledDataSource(final ConnectionConfig<?> config) {
final String driverName = config.getDriver();
if (StrUtil.isNotBlank(driverName)) {
this.driver = DriverUtil.createDriver(driverName);
}
final Props poolProps = Props.of(config.getPoolProps()); final Props poolProps = Props.of(config.getPoolProps());
this.maxWait = poolProps.getInt(KEY_MAX_WAIT, 6000); this.maxWait = poolProps.getInt(KEY_MAX_WAIT, 6000);
@ -59,6 +67,17 @@ public class PooledDataSource extends AbstractDataSource {
this.connPool = new PartitionObjectPool<>(poolConfig, createConnFactory(config)); this.connPool = new PartitionObjectPool<>(poolConfig, createConnFactory(config));
} }
/**
* 设置驱动
*
* @param driver 驱动
* @return this
*/
public PooledDataSource setDriver(final Driver driver) {
this.driver = driver;
return this;
}
@Override @Override
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
return (Connection) connPool.borrowObject(); return (Connection) connPool.borrowObject();