mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
增加SafeConcurrentHashMap
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.db.dialect;
|
||||
|
||||
import cn.hutool.core.map.SafeConcurrentHashMap;
|
||||
import cn.hutool.core.util.ClassLoaderUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@@ -16,7 +17,6 @@ import cn.hutool.log.StaticLog;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 方言工厂类
|
||||
@@ -26,7 +26,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*/
|
||||
public class DialectFactory implements DriverNamePool{
|
||||
|
||||
private static final Map<DataSource, Dialect> DIALECT_POOL = new ConcurrentHashMap<>();
|
||||
private static final Map<DataSource, Dialect> DIALECT_POOL = new SafeConcurrentHashMap<>();
|
||||
|
||||
private DialectFactory() {
|
||||
}
|
||||
@@ -170,11 +170,7 @@ public class DialectFactory implements DriverNamePool{
|
||||
// 数据源作为锁的意义在于:不同数据源不会导致阻塞,相同数据源获取方言时可保证互斥
|
||||
//noinspection SynchronizationOnLocalVariableOrMethodParameter
|
||||
synchronized (ds) {
|
||||
dialect = DIALECT_POOL.get(ds);
|
||||
if(null == dialect) {
|
||||
dialect = newDialect(ds);
|
||||
DIALECT_POOL.put(ds, dialect);
|
||||
}
|
||||
dialect = DIALECT_POOL.computeIfAbsent(ds, DialectFactory::newDialect);
|
||||
}
|
||||
}
|
||||
return dialect;
|
||||
|
@@ -2,6 +2,7 @@ package cn.hutool.db.ds;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.map.SafeConcurrentHashMap;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
import cn.hutool.db.DbUtil;
|
||||
@@ -12,7 +13,6 @@ import cn.hutool.setting.Setting;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 抽象数据源工厂<br>
|
||||
@@ -54,7 +54,7 @@ public abstract class AbstractDSFactory extends DSFactory {
|
||||
DbUtil.setShowSqlGlobal(setting);
|
||||
|
||||
this.setting = setting;
|
||||
this.dsMap = new ConcurrentHashMap<>();
|
||||
this.dsMap = new SafeConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,7 +143,6 @@ public abstract class AbstractDSFactory extends DSFactory {
|
||||
DataSourceWrapper ds = dsMap.get(group);
|
||||
if (ds != null) {
|
||||
ds.close();
|
||||
//noinspection resource
|
||||
dsMap.remove(group);
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,8 @@
|
||||
package cn.hutool.db.meta;
|
||||
|
||||
import cn.hutool.core.map.SafeConcurrentHashMap;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* JDBC中字段类型枚举
|
||||
@@ -56,14 +57,14 @@ public enum JdbcType {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param code {@link java.sql.Types} 中对应的值
|
||||
*/
|
||||
JdbcType(int code) {
|
||||
this.typeCode = code;
|
||||
}
|
||||
|
||||
private static final Map<Integer, JdbcType> CODE_MAP = new ConcurrentHashMap<>(100, 1);
|
||||
private static final Map<Integer, JdbcType> CODE_MAP = new SafeConcurrentHashMap<>(100, 1);
|
||||
static {
|
||||
for (JdbcType type : JdbcType.values()) {
|
||||
CODE_MAP.put(type.typeCode, type);
|
||||
@@ -72,12 +73,12 @@ public enum JdbcType {
|
||||
|
||||
/**
|
||||
* 通过{@link java.sql.Types}中对应int值找到enum值
|
||||
*
|
||||
*
|
||||
* @param code Jdbc type值
|
||||
* @return {@link JdbcType}
|
||||
* @return {@code JdbcType}
|
||||
*/
|
||||
public static JdbcType valueOf(int code) {
|
||||
return CODE_MAP.get(code);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -1,13 +1,13 @@
|
||||
package cn.hutool.db.nosql.mongo;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.map.SafeConcurrentHashMap;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.RuntimeUtil;
|
||||
import cn.hutool.setting.Setting;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* {@link MongoDS}工厂类,用于创建
|
||||
@@ -24,7 +24,7 @@ public class MongoFactory {
|
||||
/**
|
||||
* 数据源池
|
||||
*/
|
||||
private static final Map<String, MongoDS> DS_MAP = new ConcurrentHashMap<>();
|
||||
private static final Map<String, MongoDS> DS_MAP = new SafeConcurrentHashMap<>();
|
||||
|
||||
// JVM关闭前关闭MongoDB连接
|
||||
static {
|
||||
@@ -42,14 +42,7 @@ public class MongoFactory {
|
||||
*/
|
||||
public static MongoDS getDS(String host, int port) {
|
||||
final String key = host + ":" + port;
|
||||
MongoDS ds = DS_MAP.get(key);
|
||||
if (null == ds) {
|
||||
// 没有在池中加入之
|
||||
ds = new MongoDS(host, port);
|
||||
DS_MAP.put(key, ds);
|
||||
}
|
||||
|
||||
return ds;
|
||||
return DS_MAP.computeIfAbsent(key, (k)->new MongoDS(host, port));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user