mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add SafeConcurrentHashMap
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.db.dialect;
|
||||
|
||||
import cn.hutool.core.classloader.ClassLoaderUtil;
|
||||
import cn.hutool.core.map.SafeConcurrentHashMap;
|
||||
import cn.hutool.core.regex.ReUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.db.dialect.impl.AnsiSqlDialect;
|
||||
@@ -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.text.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<>();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package cn.hutool.db.meta;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* JDBC中字段类型枚举
|
||||
@@ -63,7 +63,8 @@ public enum JdbcType {
|
||||
this.typeCode = code;
|
||||
}
|
||||
|
||||
private static final Map<Integer, JdbcType> CODE_MAP = new ConcurrentHashMap<>(100, 1);
|
||||
// 此处无写操作,使用HashMap没有线程安全问题
|
||||
private static final Map<Integer, JdbcType> CODE_MAP = new HashMap<>(128, 1);
|
||||
static {
|
||||
for (final JdbcType type : JdbcType.values()) {
|
||||
CODE_MAP.put(type.typeCode, type);
|
||||
@@ -74,7 +75,7 @@ public enum JdbcType {
|
||||
* 通过{@link java.sql.Types}中对应int值找到enum值
|
||||
*
|
||||
* @param code Jdbc type值
|
||||
* @return {@link JdbcType}
|
||||
* @return {@code JdbcType}
|
||||
*/
|
||||
public static JdbcType valueOf(final int code) {
|
||||
return CODE_MAP.get(code);
|
||||
|
Reference in New Issue
Block a user