mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
a7abbf5402
commit
883441a113
@ -57,41 +57,41 @@ public class DbSetting {
|
||||
* @param group 分组
|
||||
* @return 分组
|
||||
*/
|
||||
public DbConfig getDbConfig(final String group) {
|
||||
public PooledDbConfig getDbConfig(final String group) {
|
||||
final Setting config = setting.getSetting(group);
|
||||
if (MapUtil.isEmpty(config)) {
|
||||
throw new DbRuntimeException("No Hutool pool config for group: [{}]", group);
|
||||
}
|
||||
|
||||
final DbConfig dbConfig = new DbConfig();
|
||||
final PooledDbConfig pooledDbConfig = new PooledDbConfig();
|
||||
|
||||
// 基本信息
|
||||
final String url = config.getAndRemove(DSKeys.KEY_ALIAS_URL);
|
||||
if (StrUtil.isBlank(url)) {
|
||||
throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
|
||||
}
|
||||
dbConfig.setUrl(url);
|
||||
pooledDbConfig.setUrl(url);
|
||||
// 自动识别Driver
|
||||
final String driver = config.getAndRemove(DSKeys.KEY_ALIAS_DRIVER);
|
||||
dbConfig.setDriver(StrUtil.isNotBlank(driver) ? driver : DriverUtil.identifyDriver(url));
|
||||
dbConfig.setUser(config.getAndRemove(DSKeys.KEY_ALIAS_USER));
|
||||
dbConfig.setPass(config.getAndRemove(DSKeys.KEY_ALIAS_PASSWORD));
|
||||
pooledDbConfig.setDriver(StrUtil.isNotBlank(driver) ? driver : DriverUtil.identifyDriver(url));
|
||||
pooledDbConfig.setUser(config.getAndRemove(DSKeys.KEY_ALIAS_USER));
|
||||
pooledDbConfig.setPass(config.getAndRemove(DSKeys.KEY_ALIAS_PASSWORD));
|
||||
|
||||
// 连接池相关信息
|
||||
dbConfig.setInitialSize(setting.getIntByGroup("initialSize", group, 0));
|
||||
dbConfig.setMinIdle(setting.getIntByGroup("minIdle", group, 0));
|
||||
dbConfig.setMaxActive(setting.getIntByGroup("maxActive", group, 8));
|
||||
dbConfig.setMaxWait(setting.getLongByGroup("maxWait", group, 6000L));
|
||||
pooledDbConfig.setInitialSize(setting.getIntByGroup("initialSize", group, 0));
|
||||
pooledDbConfig.setMinIdle(setting.getIntByGroup("minIdle", group, 0));
|
||||
pooledDbConfig.setMaxActive(setting.getIntByGroup("maxActive", group, 8));
|
||||
pooledDbConfig.setMaxWait(setting.getLongByGroup("maxWait", group, 6000L));
|
||||
|
||||
// remarks等特殊配置,since 5.3.8
|
||||
String connValue;
|
||||
for (final String key : DSKeys.KEY_CONN_PROPS) {
|
||||
connValue = config.get(key);
|
||||
if(StrUtil.isNotBlank(connValue)){
|
||||
dbConfig.addConnProps(key, connValue);
|
||||
pooledDbConfig.addConnProps(key, connValue);
|
||||
}
|
||||
}
|
||||
|
||||
return dbConfig;
|
||||
return pooledDbConfig;
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ public class PooledConnection extends ConnectionWrapper {
|
||||
*/
|
||||
public PooledConnection(final PooledDataSource ds) throws SQLException {
|
||||
this.ds = ds;
|
||||
final DbConfig config = ds.getConfig();
|
||||
final PooledDbConfig config = ds.getConfig();
|
||||
|
||||
final Props info = new Props();
|
||||
final String user = config.getUser();
|
||||
|
@ -51,27 +51,27 @@ public class PooledDSFactory extends AbstractDSFactory {
|
||||
|
||||
@Override
|
||||
protected DataSource createDataSource(final String jdbcUrl, final String driver, final String user, final String pass, final Setting poolSetting) {
|
||||
final DbConfig dbConfig = new DbConfig();
|
||||
dbConfig.setUrl(jdbcUrl);
|
||||
dbConfig.setDriver(driver);
|
||||
dbConfig.setUser(user);
|
||||
dbConfig.setPass(pass);
|
||||
final PooledDbConfig pooledDbConfig = new PooledDbConfig();
|
||||
pooledDbConfig.setUrl(jdbcUrl);
|
||||
pooledDbConfig.setDriver(driver);
|
||||
pooledDbConfig.setUser(user);
|
||||
pooledDbConfig.setPass(pass);
|
||||
|
||||
// 连接池相关信息
|
||||
dbConfig.setInitialSize(poolSetting.getInt("initialSize", 0));
|
||||
dbConfig.setMinIdle(poolSetting.getInt("minIdle", 0));
|
||||
dbConfig.setMaxActive(poolSetting.getInt("maxActive", 8));
|
||||
dbConfig.setMaxWait(poolSetting.getLong("maxWait", 6000L));
|
||||
pooledDbConfig.setInitialSize(poolSetting.getInt("initialSize", 0));
|
||||
pooledDbConfig.setMinIdle(poolSetting.getInt("minIdle", 0));
|
||||
pooledDbConfig.setMaxActive(poolSetting.getInt("maxActive", 8));
|
||||
pooledDbConfig.setMaxWait(poolSetting.getLong("maxWait", 6000L));
|
||||
|
||||
// remarks等特殊配置,since 5.3.8
|
||||
String connValue;
|
||||
for (final String key : DSKeys.KEY_CONN_PROPS) {
|
||||
connValue = poolSetting.get(key);
|
||||
if(StrUtil.isNotBlank(connValue)){
|
||||
dbConfig.addConnProps(key, connValue);
|
||||
pooledDbConfig.addConnProps(key, connValue);
|
||||
}
|
||||
}
|
||||
|
||||
return new PooledDataSource(dbConfig);
|
||||
return new PooledDataSource(pooledDbConfig);
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ public class PooledDataSource extends AbstractDataSource {
|
||||
private Queue<PooledConnection> freePool;
|
||||
private int activeCount; // 活跃连接数
|
||||
|
||||
private final DbConfig config;
|
||||
private final PooledDbConfig config;
|
||||
|
||||
/**
|
||||
* 获得一个数据源
|
||||
@ -88,7 +88,7 @@ public class PooledDataSource extends AbstractDataSource {
|
||||
*
|
||||
* @param config 数据库配置
|
||||
*/
|
||||
public PooledDataSource(final DbConfig config) {
|
||||
public PooledDataSource(final PooledDbConfig config) {
|
||||
this.config = config;
|
||||
freePool = new LinkedList<>();
|
||||
int initialSize = config.getInitialSize();
|
||||
@ -136,7 +136,7 @@ public class PooledDataSource extends AbstractDataSource {
|
||||
return new PooledConnection(this);
|
||||
}
|
||||
|
||||
public DbConfig getConfig() {
|
||||
public PooledDbConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2023 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.pooled;
|
||||
|
||||
import org.dromara.hutool.db.ds.simple.DbConfig;
|
||||
|
||||
/**
|
||||
* 数据库配置
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class PooledDbConfig extends DbConfig {
|
||||
|
||||
private int initialSize; //初始连接数
|
||||
private int minIdle; //最小闲置连接数
|
||||
private int maxActive; //最大活跃连接数
|
||||
private long maxWait; //获取连接的超时等待
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public PooledDbConfig() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param url jdbc url
|
||||
* @param user 用户名
|
||||
* @param pass 密码
|
||||
*/
|
||||
public PooledDbConfig(final String url, final String user, final String pass) {
|
||||
super(url, user, pass);
|
||||
}
|
||||
|
||||
public int getInitialSize() {
|
||||
return initialSize;
|
||||
}
|
||||
|
||||
public void setInitialSize(final int initialSize) {
|
||||
this.initialSize = initialSize;
|
||||
}
|
||||
|
||||
public int getMinIdle() {
|
||||
return minIdle;
|
||||
}
|
||||
|
||||
public void setMinIdle(final int minIdle) {
|
||||
this.minIdle = minIdle;
|
||||
}
|
||||
|
||||
public int getMaxActive() {
|
||||
return maxActive;
|
||||
}
|
||||
|
||||
public void setMaxActive(final int maxActive) {
|
||||
this.maxActive = maxActive;
|
||||
}
|
||||
|
||||
public long getMaxWait() {
|
||||
return maxWait;
|
||||
}
|
||||
|
||||
public void setMaxWait(final long maxWait) {
|
||||
this.maxWait = maxWait;
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.db.ds.pooled;
|
||||
package org.dromara.hutool.db.ds.simple;
|
||||
|
||||
import org.dromara.hutool.db.DbRuntimeException;
|
||||
import org.dromara.hutool.db.driver.DriverUtil;
|
||||
@ -24,22 +24,17 @@ import java.util.Properties;
|
||||
*/
|
||||
public class DbConfig {
|
||||
|
||||
//-------------------------------------------------------------------- Fields start
|
||||
private String driver; //数据库驱动
|
||||
private String url; //jdbc url
|
||||
private String user; //用户名
|
||||
private String pass; //密码
|
||||
|
||||
private int initialSize; //初始连接数
|
||||
private int minIdle; //最小闲置连接数
|
||||
private int maxActive; //最大活跃连接数
|
||||
private long maxWait; //获取连接的超时等待
|
||||
|
||||
// 连接配置
|
||||
private Properties connProps;
|
||||
//-------------------------------------------------------------------- Fields end
|
||||
|
||||
//-------------------------------------------------------------------- Constructor start
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public DbConfig() {
|
||||
}
|
||||
|
||||
@ -53,7 +48,6 @@ public class DbConfig {
|
||||
public DbConfig(final String url, final String user, final String pass) {
|
||||
init(url, user, pass);
|
||||
}
|
||||
//-------------------------------------------------------------------- Constructor end
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
@ -74,7 +68,6 @@ public class DbConfig {
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------- Getters and Setters start
|
||||
public String getDriver() {
|
||||
return driver;
|
||||
}
|
||||
@ -107,38 +100,6 @@ public class DbConfig {
|
||||
this.pass = pass;
|
||||
}
|
||||
|
||||
public int getInitialSize() {
|
||||
return initialSize;
|
||||
}
|
||||
|
||||
public void setInitialSize(final int initialSize) {
|
||||
this.initialSize = initialSize;
|
||||
}
|
||||
|
||||
public int getMinIdle() {
|
||||
return minIdle;
|
||||
}
|
||||
|
||||
public void setMinIdle(final int minIdle) {
|
||||
this.minIdle = minIdle;
|
||||
}
|
||||
|
||||
public int getMaxActive() {
|
||||
return maxActive;
|
||||
}
|
||||
|
||||
public void setMaxActive(final int maxActive) {
|
||||
this.maxActive = maxActive;
|
||||
}
|
||||
|
||||
public long getMaxWait() {
|
||||
return maxWait;
|
||||
}
|
||||
|
||||
public void setMaxWait(final long maxWait) {
|
||||
this.maxWait = maxWait;
|
||||
}
|
||||
|
||||
public Properties getConnProps() {
|
||||
return connProps;
|
||||
}
|
||||
@ -153,5 +114,4 @@ public class DbConfig {
|
||||
}
|
||||
this.connProps.setProperty(key, value);
|
||||
}
|
||||
//-------------------------------------------------------------------- Getters and Setters end
|
||||
}
|
@ -49,13 +49,10 @@ public class SimpleDSFactory extends AbstractDSFactory {
|
||||
|
||||
@Override
|
||||
protected DataSource createDataSource(final String jdbcUrl, final String driver, final String user, final String pass, final Setting poolSetting) {
|
||||
final SimpleDataSource ds = new SimpleDataSource(//
|
||||
jdbcUrl, //
|
||||
user, //
|
||||
pass, //
|
||||
driver//
|
||||
);
|
||||
ds.setConnProps(poolSetting.getProps(Setting.DEFAULT_GROUP));
|
||||
return ds;
|
||||
final DbConfig dbConfig = new DbConfig(jdbcUrl, user, pass);
|
||||
dbConfig.setDriver(driver);
|
||||
dbConfig.setConnProps(poolSetting.getProps(Setting.DEFAULT_GROUP));
|
||||
|
||||
return new SimpleDataSource(dbConfig);
|
||||
}
|
||||
}
|
||||
|
@ -36,22 +36,14 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
/** 默认的数据库连接配置文件路径 */
|
||||
public final static String DEFAULT_DB_CONFIG_PATH = "config/db.setting";
|
||||
|
||||
// -------------------------------------------------------------------- Fields start
|
||||
private String driver; // 数据库驱动
|
||||
private String url; // jdbc url
|
||||
private String user; // 用户名
|
||||
private String pass; // 密码
|
||||
|
||||
// 连接配置
|
||||
private Properties connProps;
|
||||
// -------------------------------------------------------------------- Fields end
|
||||
private final DbConfig config;
|
||||
|
||||
// -------------------------------------------------------------------- Constructor start
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public SimpleDataSource() {
|
||||
this(null);
|
||||
this(StrUtil.EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -73,150 +65,74 @@ public class SimpleDataSource extends AbstractDataSource {
|
||||
if (null == setting) {
|
||||
setting = new Setting(DEFAULT_DB_CONFIG_PATH);
|
||||
}
|
||||
final Setting config = setting.getSetting(group);
|
||||
if (MapUtil.isEmpty(config)) {
|
||||
final Setting dbSetting = setting.getSetting(group);
|
||||
if (MapUtil.isEmpty(dbSetting)) {
|
||||
throw new DbRuntimeException("No DataSource config for group: [{}]", group);
|
||||
}
|
||||
|
||||
init(//
|
||||
config.getAndRemove(DSKeys.KEY_ALIAS_URL), //
|
||||
config.getAndRemove(DSKeys.KEY_ALIAS_USER), //
|
||||
config.getAndRemove(DSKeys.KEY_ALIAS_PASSWORD), //
|
||||
config.getAndRemove(DSKeys.KEY_ALIAS_DRIVER)//
|
||||
);
|
||||
final DbConfig dbConfig = new DbConfig();
|
||||
|
||||
// 其它连接参数
|
||||
this.connProps = config.getProps(Setting.DEFAULT_GROUP);
|
||||
// 基本信息
|
||||
final String url = dbSetting.getAndRemove(DSKeys.KEY_ALIAS_URL);
|
||||
if (StrUtil.isBlank(url)) {
|
||||
throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
|
||||
}
|
||||
dbConfig.setUrl(url);
|
||||
|
||||
// 自动识别Driver
|
||||
final String driver = dbSetting.getAndRemove(DSKeys.KEY_ALIAS_DRIVER);
|
||||
dbConfig.setDriver(StrUtil.isNotBlank(driver) ? driver : DriverUtil.identifyDriver(url));
|
||||
dbConfig.setUser(dbSetting.getAndRemove(DSKeys.KEY_ALIAS_USER));
|
||||
dbConfig.setPass(dbSetting.getAndRemove(DSKeys.KEY_ALIAS_PASSWORD));
|
||||
|
||||
// remarks等特殊配置,since 5.3.8
|
||||
String connValue;
|
||||
for (final String key : DSKeys.KEY_CONN_PROPS) {
|
||||
connValue = dbSetting.get(key);
|
||||
if(StrUtil.isNotBlank(connValue)){
|
||||
dbConfig.addConnProps(key, connValue);
|
||||
}
|
||||
}
|
||||
|
||||
this.config = dbConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param url jdbc url
|
||||
* @param user 用户名
|
||||
* @param pass 密码
|
||||
* @param config 数据库连接配置
|
||||
*/
|
||||
public SimpleDataSource(final String url, final String user, final String pass) {
|
||||
init(url, user, pass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param url jdbc url
|
||||
* @param user 用户名
|
||||
* @param pass 密码
|
||||
* @param driver JDBC驱动类
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public SimpleDataSource(final String url, final String user, final String pass, final String driver) {
|
||||
init(url, user, pass, driver);
|
||||
public SimpleDataSource(final DbConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
// -------------------------------------------------------------------- Constructor end
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
* @param url jdbc url
|
||||
* @param user 用户名
|
||||
* @param pass 密码
|
||||
*/
|
||||
public void init(final String url, final String user, final String pass) {
|
||||
init(url, user, pass, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
* @param url jdbc url
|
||||
* @param user 用户名
|
||||
* @param pass 密码
|
||||
* @param driver JDBC驱动类,传入空则自动识别驱动类
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public void init(final String url, final String user, final String pass, final String driver) {
|
||||
this.driver = StrUtil.isNotBlank(driver) ? driver : DriverUtil.identifyDriver(url);
|
||||
try {
|
||||
Class.forName(this.driver);
|
||||
} catch (final ClassNotFoundException e) {
|
||||
throw new DbRuntimeException(e, "Get jdbc driver [{}] error!", driver);
|
||||
}
|
||||
this.url = url;
|
||||
this.user = user;
|
||||
this.pass = pass;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- Getters and Setters start
|
||||
public String getDriver() {
|
||||
return driver;
|
||||
}
|
||||
|
||||
public void setDriver(final String driver) {
|
||||
this.driver = driver;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(final String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(final String user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
return pass;
|
||||
}
|
||||
|
||||
public void setPass(final String pass) {
|
||||
this.pass = pass;
|
||||
}
|
||||
|
||||
public Properties getConnProps() {
|
||||
return connProps;
|
||||
}
|
||||
|
||||
public void setConnProps(final Properties connProps) {
|
||||
this.connProps = connProps;
|
||||
}
|
||||
|
||||
public void addConnProps(final String key, final String value){
|
||||
if(null == this.connProps){
|
||||
this.connProps = new Properties();
|
||||
}
|
||||
this.connProps.setProperty(key, value);
|
||||
}
|
||||
// -------------------------------------------------------------------- Getters and Setters end
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
final DbConfig config = this.config;
|
||||
final Props info = new Props();
|
||||
if (this.user != null) {
|
||||
info.setProperty("user", this.user);
|
||||
|
||||
final String user = config.getUser();
|
||||
if (user != null) {
|
||||
info.setProperty("user", user);
|
||||
}
|
||||
if (this.pass != null) {
|
||||
info.setProperty("password", this.pass);
|
||||
final String pass = config.getPass();
|
||||
if (pass != null) {
|
||||
info.setProperty("password", pass);
|
||||
}
|
||||
|
||||
// 其它参数
|
||||
final Properties connProps = this.connProps;
|
||||
final Properties connProps = config.getConnProps();
|
||||
if(MapUtil.isNotEmpty(connProps)){
|
||||
info.putAll(connProps);
|
||||
}
|
||||
|
||||
return DriverManager.getConnection(this.url, info);
|
||||
return DriverManager.getConnection(config.getUrl(), info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection(final String username, final String password) throws SQLException {
|
||||
return DriverManager.getConnection(this.url, username, password);
|
||||
return DriverManager.getConnection(config.getUrl(), username, password);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user