mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix c3p0 bug
This commit is contained in:
parent
9b01a23a60
commit
9679165161
@ -2,7 +2,7 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.7.21 (2022-02-07)
|
||||
# 5.7.21 (2022-02-11)
|
||||
|
||||
### 🐣新特性
|
||||
* 【extra 】 增加jetbrick模板支持
|
||||
@ -24,6 +24,7 @@
|
||||
* 【extra 】 修复EmojiUtil.toHtmlHex()方法(pr#519@Gitee)
|
||||
* 【system 】 修复CpuInfo.getUsed()方法(issue#2116@Github)
|
||||
* 【dfa 】 修复密集匹配和贪婪匹配冲突问题(issue#2126@Github)
|
||||
* 【db 】 修复c3p0丢失信息问题(issue#I4T7XZ@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.7.20 (2022-01-20)
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.hutool.db.ds.c3p0;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
import cn.hutool.db.ds.AbstractDSFactory;
|
||||
@ -12,13 +13,13 @@ import java.beans.PropertyVetoException;
|
||||
|
||||
/**
|
||||
* Druid数据源工厂类
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class C3p0DSFactory extends AbstractDSFactory {
|
||||
private static final long serialVersionUID = -6090788225842047281L;
|
||||
|
||||
|
||||
public static final String DS_NAME = "C3P0";
|
||||
|
||||
/**
|
||||
@ -30,24 +31,16 @@ public class C3p0DSFactory extends AbstractDSFactory {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param setting 配置
|
||||
*/
|
||||
public C3p0DSFactory(Setting setting) {
|
||||
super(DS_NAME, ComboPooledDataSource.class, setting);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
|
||||
final ComboPooledDataSource ds = new ComboPooledDataSource();
|
||||
ds.setJdbcUrl(jdbcUrl);
|
||||
try {
|
||||
ds.setDriverClass(driver);
|
||||
} catch (PropertyVetoException e) {
|
||||
throw new DbRuntimeException(e);
|
||||
}
|
||||
ds.setUser(user);
|
||||
ds.setPassword(pass);
|
||||
|
||||
// remarks等特殊配置,since 5.3.8
|
||||
final Props connProps = new Props();
|
||||
@ -58,7 +51,18 @@ public class C3p0DSFactory extends AbstractDSFactory {
|
||||
connProps.setProperty(key, connValue);
|
||||
}
|
||||
}
|
||||
ds.setProperties(connProps);
|
||||
if(MapUtil.isNotEmpty(connProps)){
|
||||
ds.setProperties(connProps);
|
||||
}
|
||||
|
||||
ds.setJdbcUrl(jdbcUrl);
|
||||
try {
|
||||
ds.setDriverClass(driver);
|
||||
} catch (PropertyVetoException e) {
|
||||
throw new DbRuntimeException(e);
|
||||
}
|
||||
ds.setUser(user);
|
||||
ds.setPassword(pass);
|
||||
|
||||
// 注入属性
|
||||
poolSetting.toBean(ds);
|
||||
|
@ -2,6 +2,7 @@ package cn.hutool.db;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.db.ds.DSFactory;
|
||||
import cn.hutool.db.ds.DataSourceWrapper;
|
||||
import cn.hutool.db.ds.bee.BeeDSFactory;
|
||||
import cn.hutool.db.ds.c3p0.C3p0DSFactory;
|
||||
import cn.hutool.db.ds.dbcp.DbcpDSFactory;
|
||||
@ -9,6 +10,7 @@ import cn.hutool.db.ds.druid.DruidDSFactory;
|
||||
import cn.hutool.db.ds.hikari.HikariDSFactory;
|
||||
import cn.hutool.db.ds.pooled.PooledDSFactory;
|
||||
import cn.hutool.db.ds.tomcat.TomcatDSFactory;
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -87,6 +89,15 @@ public class DsTest {
|
||||
Assert.assertTrue(CollUtil.isNotEmpty(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void c3p0DsUserAndPassTest() {
|
||||
// https://gitee.com/dromara/hutool/issues/I4T7XZ
|
||||
DSFactory.setCurrentDSFactory(new C3p0DSFactory());
|
||||
ComboPooledDataSource ds = (ComboPooledDataSource) ((DataSourceWrapper) DSFactory.get("mysql")).getRaw();
|
||||
Assert.assertEquals("root", ds.getUser());
|
||||
Assert.assertEquals("123456", ds.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hutoolPoolTest() throws SQLException {
|
||||
DSFactory.setCurrentDSFactory(new PooledDSFactory());
|
||||
|
Loading…
x
Reference in New Issue
Block a user