mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
db support remarks etc propertis
This commit is contained in:
parent
3b80f0a3d1
commit
9c434da9fb
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
## 5.3.8 (2020-06-15)
|
## 5.3.8 (2020-06-16)
|
||||||
|
|
||||||
### 新特性
|
### 新特性
|
||||||
* 【core 】 增加ISO8601日期格式(issue#904@Github)
|
* 【core 】 增加ISO8601日期格式(issue#904@Github)
|
||||||
@ -12,6 +12,8 @@
|
|||||||
* 【core 】 复制创建一个Bean对象, 并忽略某些属性(pr#130@Gitee)
|
* 【core 】 复制创建一个Bean对象, 并忽略某些属性(pr#130@Gitee)
|
||||||
* 【core 】 DateUtil.parse支持更多日期格式(issue#I1KHTB@Gitee)
|
* 【core 】 DateUtil.parse支持更多日期格式(issue#I1KHTB@Gitee)
|
||||||
* 【crypto 】 增加获取密钥空指针的检查(issue#925@Github)
|
* 【crypto 】 增加获取密钥空指针的检查(issue#925@Github)
|
||||||
|
* 【core 】 增加StrUtil.removeAny方法(issue#923@Github)
|
||||||
|
* 【db 】 增加部分Connection参数支持(issue#924@Github)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【json 】 修复append方法导致的JSONConfig传递失效问题(issue#906@Github)
|
* 【json 】 修复append方法导致的JSONConfig传递失效问题(issue#906@Github)
|
||||||
|
@ -965,6 +965,25 @@ public class StrUtil {
|
|||||||
return str.toString().replace(strToRemove, EMPTY);
|
return str.toString().replace(strToRemove, EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除字符串中所有给定字符串,当某个字符串出现多次,则全部移除<br>
|
||||||
|
* 例:removeAny("aa-bb-cc-dd", "a", "b") =》 --cc-dd
|
||||||
|
*
|
||||||
|
* @param str 字符串
|
||||||
|
* @param strsToRemove 被移除的字符串
|
||||||
|
* @return 移除后的字符串
|
||||||
|
* @since 5.3.8
|
||||||
|
*/
|
||||||
|
public static String removeAny(CharSequence str, CharSequence... strsToRemove) {
|
||||||
|
String result = str(str);
|
||||||
|
if (isNotEmpty(str)) {
|
||||||
|
for (CharSequence strToRemove : strsToRemove) {
|
||||||
|
result = removeAll(str, strToRemove);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 去除字符串中指定的多个字符,如有多个则全部去除
|
* 去除字符串中指定的多个字符,如有多个则全部去除
|
||||||
*
|
*
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
package cn.hutool.db.ds;
|
package cn.hutool.db.ds;
|
||||||
|
|
||||||
import java.io.Closeable;
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.db.ds.c3p0.C3p0DSFactory;
|
import cn.hutool.db.ds.c3p0.C3p0DSFactory;
|
||||||
import cn.hutool.db.ds.dbcp.DbcpDSFactory;
|
import cn.hutool.db.ds.dbcp.DbcpDSFactory;
|
||||||
@ -16,6 +11,10 @@ import cn.hutool.log.Log;
|
|||||||
import cn.hutool.log.LogFactory;
|
import cn.hutool.log.LogFactory;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 抽象数据源工厂类<br>
|
* 抽象数据源工厂类<br>
|
||||||
* 通过实现{@link #getDataSource(String)} 方法实现数据源的获取<br>
|
* 通过实现{@link #getDataSource(String)} 方法实现数据源的获取<br>
|
||||||
@ -29,6 +28,9 @@ public abstract class DSFactory implements Closeable, Serializable{
|
|||||||
|
|
||||||
private static final Log log = LogFactory.get();
|
private static final Log log = LogFactory.get();
|
||||||
|
|
||||||
|
/** 某些数据库需要的特殊配置项需要的配置项 */
|
||||||
|
public static final String[] KEY_CONN_PROPS = {"remarks", "useInformationSchema"};
|
||||||
|
|
||||||
/** 别名字段名:URL */
|
/** 别名字段名:URL */
|
||||||
public static final String[] KEY_ALIAS_URL = { "url", "jdbcUrl" };
|
public static final String[] KEY_ALIAS_URL = { "url", "jdbcUrl" };
|
||||||
/** 别名字段名:驱动名 */
|
/** 别名字段名:驱动名 */
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
package cn.hutool.db.ds.c3p0;
|
package cn.hutool.db.ds.c3p0;
|
||||||
|
|
||||||
import java.beans.PropertyVetoException;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
|
||||||
|
|
||||||
import cn.hutool.db.DbRuntimeException;
|
import cn.hutool.db.DbRuntimeException;
|
||||||
import cn.hutool.db.ds.AbstractDSFactory;
|
import cn.hutool.db.ds.AbstractDSFactory;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
|
import cn.hutool.setting.dialect.Props;
|
||||||
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.beans.PropertyVetoException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Druid数据源工厂类
|
* Druid数据源工厂类
|
||||||
@ -48,8 +48,21 @@ public class C3p0DSFactory extends AbstractDSFactory {
|
|||||||
}
|
}
|
||||||
ds.setUser(user);
|
ds.setUser(user);
|
||||||
ds.setPassword(pass);
|
ds.setPassword(pass);
|
||||||
poolSetting.toBean(ds);// 注入属性
|
|
||||||
|
// remarks等特殊配置,since 5.3.8
|
||||||
|
final Props connProps = new Props();
|
||||||
|
String connValue;
|
||||||
|
for (String key : KEY_CONN_PROPS) {
|
||||||
|
connValue = poolSetting.getAndRemoveStr(key);
|
||||||
|
if(StrUtil.isNotBlank(connValue)){
|
||||||
|
connProps.setProperty(key, connValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ds.setProperties(connProps);
|
||||||
|
|
||||||
|
// 注入属性
|
||||||
|
poolSetting.toBean(ds);
|
||||||
|
|
||||||
return ds;
|
return ds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package cn.hutool.db.ds.dbcp;
|
package cn.hutool.db.ds.dbcp;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import org.apache.commons.dbcp2.BasicDataSource;
|
|
||||||
|
|
||||||
import cn.hutool.db.ds.AbstractDSFactory;
|
import cn.hutool.db.ds.AbstractDSFactory;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
|
import org.apache.commons.dbcp2.BasicDataSource;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DBCP2数据源工厂类
|
* DBCP2数据源工厂类
|
||||||
@ -34,8 +34,19 @@ public class DbcpDSFactory extends AbstractDSFactory {
|
|||||||
ds.setDriverClassName(driver);
|
ds.setDriverClassName(driver);
|
||||||
ds.setUsername(user);
|
ds.setUsername(user);
|
||||||
ds.setPassword(pass);
|
ds.setPassword(pass);
|
||||||
poolSetting.toBean(ds);// 注入属性
|
|
||||||
|
// remarks等特殊配置,since 5.3.8
|
||||||
|
String connValue;
|
||||||
|
for (String key : KEY_CONN_PROPS) {
|
||||||
|
connValue = poolSetting.getAndRemoveStr(key);
|
||||||
|
if(StrUtil.isNotBlank(connValue)){
|
||||||
|
ds.addConnectionProperty(key, connValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 注入属性
|
||||||
|
poolSetting.toBean(ds);
|
||||||
|
|
||||||
return ds;
|
return ds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,12 @@
|
|||||||
package cn.hutool.db.ds.druid;
|
package cn.hutool.db.ds.druid;
|
||||||
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import com.alibaba.druid.pool.DruidDataSource;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.db.ds.AbstractDSFactory;
|
import cn.hutool.db.ds.AbstractDSFactory;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
|
import cn.hutool.setting.dialect.Props;
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Druid数据源工厂类
|
* Druid数据源工厂类
|
||||||
@ -42,19 +39,24 @@ public class DruidDSFactory extends AbstractDSFactory {
|
|||||||
protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
|
protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
|
||||||
final DruidDataSource ds = new DruidDataSource();
|
final DruidDataSource ds = new DruidDataSource();
|
||||||
|
|
||||||
|
// 基本信息
|
||||||
ds.setUrl(jdbcUrl);
|
ds.setUrl(jdbcUrl);
|
||||||
ds.setDriverClassName(driver);
|
ds.setDriverClassName(driver);
|
||||||
ds.setUsername(user);
|
ds.setUsername(user);
|
||||||
ds.setPassword(pass);
|
ds.setPassword(pass);
|
||||||
|
|
||||||
// 规范化属性名
|
// remarks等特殊配置,since 5.3.8
|
||||||
Properties druidProps = new Properties();
|
String connValue;
|
||||||
String keyStr;
|
for (String key : KEY_CONN_PROPS) {
|
||||||
for (Entry<String, String> entry : poolSetting.entrySet()) {
|
connValue = poolSetting.getAndRemoveStr(key);
|
||||||
keyStr = StrUtil.addPrefixIfNot(entry.getKey(), "druid.");
|
if(StrUtil.isNotBlank(connValue)){
|
||||||
druidProps.put(keyStr, entry.getValue());
|
ds.addConnectionProperty(key, connValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 连接池信息
|
|
||||||
|
// Druid连接池配置信息,规范化属性名
|
||||||
|
final Props druidProps = new Props();
|
||||||
|
poolSetting.forEach((key, value)-> druidProps.put(StrUtil.addPrefixIfNot(key, "druid."), value));
|
||||||
ds.configFromPropety(druidProps);
|
ds.configFromPropety(druidProps);
|
||||||
|
|
||||||
// 检查关联配置,在用户未设置某项配置时,
|
// 检查关联配置,在用户未设置某项配置时,
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package cn.hutool.db.ds.hikari;
|
package cn.hutool.db.ds.hikari;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import cn.hutool.core.lang.Console;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
|
||||||
|
|
||||||
import cn.hutool.db.ds.AbstractDSFactory;
|
import cn.hutool.db.ds.AbstractDSFactory;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
import cn.hutool.setting.dialect.Props;
|
import cn.hutool.setting.dialect.Props;
|
||||||
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HikariCP数据源工厂类
|
* HikariCP数据源工厂类
|
||||||
@ -30,6 +31,16 @@ public class HikariDSFactory extends AbstractDSFactory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
|
protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
|
||||||
|
// remarks等特殊配置,since 5.3.8
|
||||||
|
final Props connProps = new Props();
|
||||||
|
String connValue;
|
||||||
|
for (String key : KEY_CONN_PROPS) {
|
||||||
|
connValue = poolSetting.getAndRemoveStr(key);
|
||||||
|
if(StrUtil.isNotBlank(connValue)){
|
||||||
|
connProps.setProperty(key, connValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final Props config = new Props();
|
final Props config = new Props();
|
||||||
config.putAll(poolSetting);
|
config.putAll(poolSetting);
|
||||||
|
|
||||||
@ -44,6 +55,9 @@ public class HikariDSFactory extends AbstractDSFactory {
|
|||||||
config.put("password", pass);
|
config.put("password", pass);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new HikariDataSource(new HikariConfig(config));
|
final HikariConfig hikariConfig = new HikariConfig(config);
|
||||||
|
hikariConfig.setDataSourceProperties(connProps);
|
||||||
|
|
||||||
|
return new HikariDataSource(hikariConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,32 +3,38 @@ package cn.hutool.db.ds.pooled;
|
|||||||
import cn.hutool.db.DbRuntimeException;
|
import cn.hutool.db.DbRuntimeException;
|
||||||
import cn.hutool.db.dialect.DriverUtil;
|
import cn.hutool.db.dialect.DriverUtil;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库配置
|
* 数据库配置
|
||||||
* @author Looly
|
|
||||||
*
|
*
|
||||||
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
public class DbConfig {
|
public class DbConfig {
|
||||||
|
|
||||||
//-------------------------------------------------------------------- Fields start
|
//-------------------------------------------------------------------- Fields start
|
||||||
private String driver; //数据库驱动
|
private String driver; //数据库驱动
|
||||||
private String url; //jdbc url
|
private String url; //jdbc url
|
||||||
private String user; //用户名
|
private String user; //用户名
|
||||||
private String pass; //密码
|
private String pass; //密码
|
||||||
|
|
||||||
private int initialSize; //初始连接数
|
private int initialSize; //初始连接数
|
||||||
private int minIdle; //最小闲置连接数
|
private int minIdle; //最小闲置连接数
|
||||||
private int maxActive; //最大活跃连接数
|
private int maxActive; //最大活跃连接数
|
||||||
private long maxWait; //获取连接的超时等待
|
private long maxWait; //获取连接的超时等待
|
||||||
|
|
||||||
|
// 连接配置
|
||||||
|
private Properties connProps;
|
||||||
//-------------------------------------------------------------------- Fields end
|
//-------------------------------------------------------------------- Fields end
|
||||||
|
|
||||||
//-------------------------------------------------------------------- Constructor start
|
//-------------------------------------------------------------------- Constructor start
|
||||||
public DbConfig() {
|
public DbConfig() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
* @param url jdbc url
|
*
|
||||||
|
* @param url jdbc url
|
||||||
* @param user 用户名
|
* @param user 用户名
|
||||||
* @param pass 密码
|
* @param pass 密码
|
||||||
*/
|
*/
|
||||||
@ -36,10 +42,11 @@ public class DbConfig {
|
|||||||
init(url, user, pass);
|
init(url, user, pass);
|
||||||
}
|
}
|
||||||
//-------------------------------------------------------------------- Constructor end
|
//-------------------------------------------------------------------- Constructor end
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化
|
* 初始化
|
||||||
* @param url jdbc url
|
*
|
||||||
|
* @param url jdbc url
|
||||||
* @param user 用户名
|
* @param user 用户名
|
||||||
* @param pass 密码
|
* @param pass 密码
|
||||||
*/
|
*/
|
||||||
@ -54,56 +61,85 @@ public class DbConfig {
|
|||||||
throw new DbRuntimeException(e, "Get jdbc driver from [{}] error!", url);
|
throw new DbRuntimeException(e, "Get jdbc driver from [{}] error!", url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------- Getters and Setters start
|
//-------------------------------------------------------------------- Getters and Setters start
|
||||||
public String getDriver() {
|
public String getDriver() {
|
||||||
return driver;
|
return driver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDriver(String driver) {
|
public void setDriver(String driver) {
|
||||||
this.driver = driver;
|
this.driver = driver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUrl() {
|
public String getUrl() {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUrl(String url) {
|
public void setUrl(String url) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUser() {
|
public String getUser() {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUser(String user) {
|
public void setUser(String user) {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPass() {
|
public String getPass() {
|
||||||
return pass;
|
return pass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPass(String pass) {
|
public void setPass(String pass) {
|
||||||
this.pass = pass;
|
this.pass = pass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getInitialSize() {
|
public int getInitialSize() {
|
||||||
return initialSize;
|
return initialSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setInitialSize(int initialSize) {
|
public void setInitialSize(int initialSize) {
|
||||||
this.initialSize = initialSize;
|
this.initialSize = initialSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMinIdle() {
|
public int getMinIdle() {
|
||||||
return minIdle;
|
return minIdle;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMinIdle(int minIdle) {
|
public void setMinIdle(int minIdle) {
|
||||||
this.minIdle = minIdle;
|
this.minIdle = minIdle;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMaxActive() {
|
public int getMaxActive() {
|
||||||
return maxActive;
|
return maxActive;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMaxActive(int maxActive) {
|
public void setMaxActive(int maxActive) {
|
||||||
this.maxActive = maxActive;
|
this.maxActive = maxActive;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getMaxWait() {
|
public long getMaxWait() {
|
||||||
return maxWait;
|
return maxWait;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMaxWait(long maxWait) {
|
public void setMaxWait(long maxWait) {
|
||||||
this.maxWait = maxWait;
|
this.maxWait = maxWait;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Properties getConnProps() {
|
||||||
|
return connProps;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnProps(Properties connProps) {
|
||||||
|
this.connProps = connProps;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addConnProps(String key, String value){
|
||||||
|
if(null == this.connProps){
|
||||||
|
this.connProps = new Properties();
|
||||||
|
}
|
||||||
|
this.connProps.setProperty(key, value);
|
||||||
|
}
|
||||||
//-------------------------------------------------------------------- Getters and Setters end
|
//-------------------------------------------------------------------- Getters and Setters end
|
||||||
}
|
}
|
@ -71,6 +71,15 @@ public class DbSetting {
|
|||||||
dbConfig.setMaxActive(setting.getInt("maxActive", group, 8));
|
dbConfig.setMaxActive(setting.getInt("maxActive", group, 8));
|
||||||
dbConfig.setMaxWait(setting.getLong("maxWait", group, 6000L));
|
dbConfig.setMaxWait(setting.getLong("maxWait", group, 6000L));
|
||||||
|
|
||||||
|
// remarks等特殊配置,since 5.3.8
|
||||||
|
String connValue;
|
||||||
|
for (String key : DSFactory.KEY_CONN_PROPS) {
|
||||||
|
connValue = config.get(key);
|
||||||
|
if(StrUtil.isNotBlank(connValue)){
|
||||||
|
dbConfig.addConnProps(key, connValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return dbConfig;
|
return dbConfig;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package cn.hutool.db.ds.pooled;
|
package cn.hutool.db.ds.pooled;
|
||||||
|
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.db.DbUtil;
|
import cn.hutool.db.DbUtil;
|
||||||
|
import cn.hutool.setting.dialect.Props;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 池化
|
* 池化
|
||||||
@ -15,11 +18,34 @@ public class PooledConnection extends ConnectionWraper{
|
|||||||
|
|
||||||
private final PooledDataSource ds;
|
private final PooledDataSource ds;
|
||||||
private boolean isClosed;
|
private boolean isClosed;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param ds 数据源
|
||||||
|
* @throws SQLException SQL异常
|
||||||
|
*/
|
||||||
public PooledConnection(PooledDataSource ds) throws SQLException {
|
public PooledConnection(PooledDataSource ds) throws SQLException {
|
||||||
this.ds = ds;
|
this.ds = ds;
|
||||||
DbConfig config = ds.getConfig();
|
final DbConfig config = ds.getConfig();
|
||||||
this.raw = DriverManager.getConnection(config.getUrl(), config.getUser(), config.getPass());
|
|
||||||
|
final Props info = new Props();
|
||||||
|
final String user = config.getUser();
|
||||||
|
if (user != null) {
|
||||||
|
info.setProperty("user", user);
|
||||||
|
}
|
||||||
|
final String password = config.getPass();
|
||||||
|
if (password != null) {
|
||||||
|
info.setProperty("password", password);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其它参数
|
||||||
|
final Properties connProps = config.getConnProps();
|
||||||
|
if(MapUtil.isNotEmpty(connProps)){
|
||||||
|
info.putAll(connProps);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.raw = DriverManager.getConnection(config.getUrl(), info);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PooledConnection(PooledDataSource ds, Connection conn) {
|
public PooledConnection(PooledDataSource ds, Connection conn) {
|
||||||
@ -31,7 +57,7 @@ public class PooledConnection extends ConnectionWraper{
|
|||||||
* 重写关闭连接,实际操作是归还到连接池中
|
* 重写关闭连接,实际操作是归还到连接池中
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void close() throws SQLException {
|
public void close() {
|
||||||
this.ds.free(this);
|
this.ds.free(this);
|
||||||
this.isClosed = true;
|
this.isClosed = true;
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package cn.hutool.db.ds.pooled;
|
package cn.hutool.db.ds.pooled;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import cn.hutool.db.ds.AbstractDSFactory;
|
import cn.hutool.db.ds.AbstractDSFactory;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hutool自身实现的池化数据源工厂类
|
* Hutool自身实现的池化数据源工厂类
|
||||||
*
|
*
|
||||||
@ -38,6 +39,15 @@ public class PooledDSFactory extends AbstractDSFactory {
|
|||||||
dbConfig.setMaxActive(poolSetting.getInt("maxActive", 8));
|
dbConfig.setMaxActive(poolSetting.getInt("maxActive", 8));
|
||||||
dbConfig.setMaxWait(poolSetting.getLong("maxWait", 6000L));
|
dbConfig.setMaxWait(poolSetting.getLong("maxWait", 6000L));
|
||||||
|
|
||||||
|
// remarks等特殊配置,since 5.3.8
|
||||||
|
String connValue;
|
||||||
|
for (String key : KEY_CONN_PROPS) {
|
||||||
|
connValue = poolSetting.get(key);
|
||||||
|
if(StrUtil.isNotBlank(connValue)){
|
||||||
|
dbConfig.addConnProps(key, connValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return new PooledDataSource(dbConfig);
|
return new PooledDataSource(dbConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package cn.hutool.db.ds.simple;
|
package cn.hutool.db.ds.simple;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import cn.hutool.db.ds.AbstractDSFactory;
|
import cn.hutool.db.ds.AbstractDSFactory;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 简单数据源工厂类
|
* 简单数据源工厂类
|
||||||
*
|
*
|
||||||
@ -26,12 +26,13 @@ public class SimpleDSFactory extends AbstractDSFactory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
|
protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
|
||||||
return new SimpleDataSource(//
|
SimpleDataSource ds = new SimpleDataSource(//
|
||||||
jdbcUrl, //
|
jdbcUrl, //
|
||||||
user, //
|
user, //
|
||||||
pass, //
|
pass, //
|
||||||
driver//
|
driver//
|
||||||
);
|
);
|
||||||
|
ds.setConnProps(poolSetting.getProps(Setting.DEFAULT_GROUP));
|
||||||
|
return ds;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
package cn.hutool.db.ds.simple;
|
package cn.hutool.db.ds.simple;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.db.DbRuntimeException;
|
import cn.hutool.db.DbRuntimeException;
|
||||||
import cn.hutool.db.dialect.DriverUtil;
|
import cn.hutool.db.dialect.DriverUtil;
|
||||||
import cn.hutool.db.ds.DSFactory;
|
import cn.hutool.db.ds.DSFactory;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
|
import cn.hutool.setting.dialect.Props;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* 简易数据源,没有使用连接池,仅供测试或打开关闭连接非常少的场合使用!
|
* 简易数据源,没有使用连接池,仅供测试或打开关闭连接非常少的场合使用!
|
||||||
@ -28,6 +30,9 @@ public class SimpleDataSource extends AbstractDataSource {
|
|||||||
private String url; // jdbc url
|
private String url; // jdbc url
|
||||||
private String user; // 用户名
|
private String user; // 用户名
|
||||||
private String pass; // 密码
|
private String pass; // 密码
|
||||||
|
|
||||||
|
// 连接配置
|
||||||
|
private Properties connProps;
|
||||||
// -------------------------------------------------------------------- Fields end
|
// -------------------------------------------------------------------- Fields end
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -87,6 +92,9 @@ public class SimpleDataSource extends AbstractDataSource {
|
|||||||
config.getAndRemoveStr(DSFactory.KEY_ALIAS_PASSWORD), //
|
config.getAndRemoveStr(DSFactory.KEY_ALIAS_PASSWORD), //
|
||||||
config.getAndRemoveStr(DSFactory.KEY_ALIAS_DRIVER)//
|
config.getAndRemoveStr(DSFactory.KEY_ALIAS_DRIVER)//
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 其它连接参数
|
||||||
|
this.connProps = config.getProps(Setting.DEFAULT_GROUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -178,11 +186,40 @@ public class SimpleDataSource extends AbstractDataSource {
|
|||||||
public void setPass(String pass) {
|
public void setPass(String pass) {
|
||||||
this.pass = pass;
|
this.pass = pass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Properties getConnProps() {
|
||||||
|
return connProps;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnProps(Properties connProps) {
|
||||||
|
this.connProps = connProps;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addConnProps(String key, String value){
|
||||||
|
if(null == this.connProps){
|
||||||
|
this.connProps = new Properties();
|
||||||
|
}
|
||||||
|
this.connProps.setProperty(key, value);
|
||||||
|
}
|
||||||
// -------------------------------------------------------------------- Getters and Setters end
|
// -------------------------------------------------------------------- Getters and Setters end
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Connection getConnection() throws SQLException {
|
public Connection getConnection() throws SQLException {
|
||||||
return DriverManager.getConnection(this.url, this.user, this.pass);
|
final Props info = new Props();
|
||||||
|
if (this.user != null) {
|
||||||
|
info.setProperty("user", this.user);
|
||||||
|
}
|
||||||
|
if (this.pass != null) {
|
||||||
|
info.setProperty("password", this.pass);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其它参数
|
||||||
|
final Properties connProps = this.connProps;
|
||||||
|
if(MapUtil.isNotEmpty(connProps)){
|
||||||
|
info.putAll(connProps);
|
||||||
|
}
|
||||||
|
|
||||||
|
return DriverManager.getConnection(this.url, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -191,7 +228,7 @@ public class SimpleDataSource extends AbstractDataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() {
|
||||||
// Not need to close;
|
// Not need to close;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package cn.hutool.db.ds.tomcat;
|
package cn.hutool.db.ds.tomcat;
|
||||||
|
|
||||||
import org.apache.tomcat.jdbc.pool.DataSource;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import org.apache.tomcat.jdbc.pool.PoolProperties;
|
|
||||||
|
|
||||||
import cn.hutool.db.ds.AbstractDSFactory;
|
import cn.hutool.db.ds.AbstractDSFactory;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
|
import cn.hutool.setting.dialect.Props;
|
||||||
|
import org.apache.tomcat.jdbc.pool.DataSource;
|
||||||
|
import org.apache.tomcat.jdbc.pool.PoolProperties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tomcat-Jdbc-Pool数据源工厂类
|
* Tomcat-Jdbc-Pool数据源工厂类
|
||||||
@ -40,6 +41,20 @@ public class TomcatDSFactory extends AbstractDSFactory {
|
|||||||
poolProps.setDriverClassName(driver);
|
poolProps.setDriverClassName(driver);
|
||||||
poolProps.setUsername(user);
|
poolProps.setUsername(user);
|
||||||
poolProps.setPassword(pass);
|
poolProps.setPassword(pass);
|
||||||
|
|
||||||
|
// remarks等特殊配置,since 5.3.8
|
||||||
|
final Props connProps = new Props();
|
||||||
|
String connValue;
|
||||||
|
for (String key : KEY_CONN_PROPS) {
|
||||||
|
connValue = poolSetting.getAndRemoveStr(key);
|
||||||
|
if(StrUtil.isNotBlank(connValue)){
|
||||||
|
connProps.setProperty(key, connValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
poolProps.setDbProperties(connProps);
|
||||||
|
|
||||||
|
// 连接池相关参数
|
||||||
|
poolSetting.toBean(poolProps);
|
||||||
|
|
||||||
return new DataSource(poolProps);
|
return new DataSource(poolProps);
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,13 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* 数据库元数据信息工具类
|
* 数据库元数据信息工具类
|
||||||
*
|
*
|
||||||
|
* <p>
|
||||||
|
* 需要注意的是,此工具类在某些数据库(比如Oracle)下无效,此时需要手动在数据库配置中增加:
|
||||||
|
* <pre>
|
||||||
|
* remarks = true
|
||||||
|
* useInformationSchema = true
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
*/
|
*/
|
||||||
public class MetaUtil {
|
public class MetaUtil {
|
||||||
|
@ -18,45 +18,54 @@ sqlLevel = debug
|
|||||||
|
|
||||||
# 默认数据源
|
# 默认数据源
|
||||||
url = jdbc:sqlite:test.db
|
url = jdbc:sqlite:test.db
|
||||||
|
remarks = true
|
||||||
|
|
||||||
|
|
||||||
# 测试数据源
|
# 测试数据源
|
||||||
[test]
|
[test]
|
||||||
url = jdbc:sqlite:test.db
|
url = jdbc:sqlite:test.db
|
||||||
|
remarks = true
|
||||||
|
|
||||||
# 测试用HSQLDB数据库
|
# 测试用HSQLDB数据库
|
||||||
[hsqldb]
|
[hsqldb]
|
||||||
url = jdbc:hsqldb:mem:mem_hutool
|
url = jdbc:hsqldb:mem:mem_hutool
|
||||||
user = SA
|
user = SA
|
||||||
pass =
|
pass =
|
||||||
|
remarks = true
|
||||||
|
|
||||||
# 测试用HSQLDB数据库
|
# 测试用HSQLDB数据库
|
||||||
[h2]
|
[h2]
|
||||||
url = jdbc:h2:mem:h2_hutool
|
url = jdbc:h2:mem:h2_hutool
|
||||||
user = sa
|
user = sa
|
||||||
pass =
|
pass =
|
||||||
|
remarks = true
|
||||||
|
|
||||||
# 测试用HSQLDB数据库
|
# 测试用HSQLDB数据库
|
||||||
[derby]
|
[derby]
|
||||||
url = jdbc:derby:.derby/test_db;create=true
|
url = jdbc:derby:.derby/test_db;create=true
|
||||||
|
remarks = true
|
||||||
|
|
||||||
# 测试用Oracle数据库
|
# 测试用Oracle数据库
|
||||||
[orcl]
|
[orcl]
|
||||||
url = jdbc:oracle:thin:@//looly.centos:1521/XE
|
url = jdbc:oracle:thin:@//looly.centos:1521/XE
|
||||||
user = looly
|
user = looly
|
||||||
pass = 123456
|
pass = 123456
|
||||||
|
remarks = true
|
||||||
|
|
||||||
[mysql]
|
[mysql]
|
||||||
url = jdbc:mysql://looly.centos8:3306/hutool_test?useSSL=false
|
url = jdbc:mysql://looly.centos8:3306/hutool_test?useSSL=false
|
||||||
user = root
|
user = root
|
||||||
pass = 123456
|
pass = 123456
|
||||||
|
remarks = true
|
||||||
|
|
||||||
[postgre]
|
[postgre]
|
||||||
url = jdbc:postgresql://looly.centos:5432/test_hutool
|
url = jdbc:postgresql://looly.centos:5432/test_hutool
|
||||||
user = postgres
|
user = postgres
|
||||||
pass = 123456
|
pass = 123456
|
||||||
|
remarks = true
|
||||||
|
|
||||||
[sqlserver]
|
[sqlserver]
|
||||||
url = jdbc:sqlserver://looly.database.chinacloudapi.cn:1433;database=test;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.chinacloudapi.cn;loginTimeout=30;
|
url = jdbc:sqlserver://looly.database.chinacloudapi.cn:1433;database=test;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.chinacloudapi.cn;loginTimeout=30;
|
||||||
user = looly@looly
|
user = looly@looly
|
||||||
pass = 123
|
pass = 123
|
||||||
|
remarks = true
|
Loading…
x
Reference in New Issue
Block a user