From e54dbe02018f4e6689f77f5c229eddf28d70edde Mon Sep 17 00:00:00 2001 From: ChenYuhao Date: Mon, 28 Sep 2020 10:25:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E4=BC=9A=E4=B8=80=E7=9B=B4=E9=87=8D=E8=BF=9E?= =?UTF-8?q?,=E5=B9=B6=E4=B8=94=E8=BF=9E=E6=8E=A5=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E4=B9=8B=E5=90=8E=E4=B9=9F=E4=B8=8D=E7=BC=93=E5=AD=98=E4=B8=8B?= =?UTF-8?q?=E6=9D=A5=20(#495)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: chenyh --- .../zhengjie/modules/mnt/util/SqlUtils.java | 121 +++++++----------- 1 file changed, 49 insertions(+), 72 deletions(-) diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java index df9c64f2..27acc0e7 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java @@ -20,13 +20,15 @@ import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.util.StringUtils; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; + import javax.sql.DataSource; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.sql.*; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * @author / @@ -36,20 +38,6 @@ public class SqlUtils { public static final String COLON = ":"; - private static volatile Map map = new HashMap<>(); - - private static String getKey(String jdbcUrl, String username, String password) { - StringBuilder sb = new StringBuilder(); - if (!StringUtils.isEmpty(username)) { - sb.append(username); - } - if (!StringUtils.isEmpty(password)) { - sb.append(COLON).append(password); - } - sb.append(COLON).append(jdbcUrl.trim()); - - return SecureUtil.md5(sb.toString()); - } /** * 获取数据源 @@ -60,55 +48,44 @@ public class SqlUtils { * @return DataSource */ private static DataSource getDataSource(String jdbcUrl, String userName, String password) { - String key = getKey(jdbcUrl, userName, password); - if (!map.containsKey(key) || null == map.get(key)) { - DruidDataSource druidDataSource = new DruidDataSource(); - - String className; - try { - className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName(); - } catch (SQLException e) { - throw new RuntimeException("Get class name error: =" + jdbcUrl); - } - if (StringUtils.isEmpty(className)) { - DataTypeEnum dataTypeEnum = DataTypeEnum.urlOf(jdbcUrl); - if (null == dataTypeEnum) { - throw new RuntimeException("Not supported data type: jdbcUrl=" + jdbcUrl); - } - druidDataSource.setDriverClassName(dataTypeEnum.getDriver()); - } else { - druidDataSource.setDriverClassName(className); - } - - - druidDataSource.setUrl(jdbcUrl); - druidDataSource.setUsername(userName); - druidDataSource.setPassword(password); - // 配置获取连接等待超时的时间 - druidDataSource.setMaxWait(3000); - // 配置初始化大小、最小、最大 - druidDataSource.setInitialSize(1); - druidDataSource.setMinIdle(1); - druidDataSource.setMaxActive(1); - - // 配置间隔多久才进行一次检测需要关闭的空闲连接,单位是毫秒 - druidDataSource.setTimeBetweenEvictionRunsMillis(50000); - // 配置一旦重试多次失败后等待多久再继续重试连接,单位是毫秒 - druidDataSource.setTimeBetweenConnectErrorMillis(18000); - // 配置一个连接在池中最小生存的时间,单位是毫秒 - druidDataSource.setMinEvictableIdleTimeMillis(300000); - // 这个特性能解决 MySQL 服务器8小时关闭连接的问题 - druidDataSource.setMaxEvictableIdleTimeMillis(25200000); - - try { - druidDataSource.init(); - } catch (SQLException e) { - log.error("Exception during pool initialization", e); - throw new RuntimeException(e.getMessage()); - } - map.put(key, druidDataSource); + DruidDataSource druidDataSource = new DruidDataSource(); + String className; + try { + className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName(); + } catch (SQLException e) { + throw new RuntimeException("Get class name error: =" + jdbcUrl); } - return map.get(key); + if (StringUtils.isEmpty(className)) { + DataTypeEnum dataTypeEnum = DataTypeEnum.urlOf(jdbcUrl); + if (null == dataTypeEnum) { + throw new RuntimeException("Not supported data type: jdbcUrl=" + jdbcUrl); + } + druidDataSource.setDriverClassName(dataTypeEnum.getDriver()); + } else { + druidDataSource.setDriverClassName(className); + } + + + druidDataSource.setUrl(jdbcUrl); + druidDataSource.setUsername(userName); + druidDataSource.setPassword(password); + // 配置获取连接等待超时的时间 + druidDataSource.setMaxWait(3000); + // 配置初始化大小、最小、最大 + druidDataSource.setInitialSize(1); + druidDataSource.setMinIdle(1); + druidDataSource.setMaxActive(1); + + // 如果链接出现异常则直接判定为失败而不是一直重试 + druidDataSource.setBreakAfterAcquireFailure(true); + try { + druidDataSource.init(); + } catch (SQLException e) { + log.error("Exception during pool initialization", e); + throw new RuntimeException(e.getMessage()); + } + + return druidDataSource; } private static Connection getConnection(String jdbcUrl, String userName, String password) { @@ -187,14 +164,14 @@ public class SqlUtils { * @param sqlList / */ public static void batchExecute(Connection connection, List sqlList) throws SQLException { - Statement st = connection.createStatement(); - for (String sql : sqlList) { - if (sql.endsWith(";")) { - sql = sql.substring(0, sql.length() - 1); - } - st.addBatch(sql); + Statement st = connection.createStatement(); + for (String sql : sqlList) { + if (sql.endsWith(";")) { + sql = sql.substring(0, sql.length() - 1); } - st.executeBatch(); + st.addBatch(sql); + } + st.executeBatch(); } /** From cb11af2a33f2ccd862c53ce8c15d3da8ecda7455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9A=86=E9=9D=9E?= <53246310+jie-fei30@users.noreply.github.com> Date: Wed, 7 Oct 2020 19:23:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fixed=EF=BC=9A=E4=BF=AE=E5=A4=8D=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E4=B8=AD=E8=AE=BE=E7=BD=AE=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E5=8D=95=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E6=97=A0?= =?UTF-8?q?=E6=95=88=20(#499)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor:Optimized DeptServiceImpl's deduplication method * fix:fixed single-login is invalid in application-dev.yml * fixed prod.yml 's single-login --- eladmin-system/src/main/resources/config/application-dev.yml | 2 +- eladmin-system/src/main/resources/config/application-prod.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/eladmin-system/src/main/resources/config/application-dev.yml b/eladmin-system/src/main/resources/config/application-dev.yml index 83da6985..65d5aa71 100644 --- a/eladmin-system/src/main/resources/config/application-dev.yml +++ b/eladmin-system/src/main/resources/config/application-dev.yml @@ -49,7 +49,7 @@ login: # 登录缓存 cache-enable: true # 是否限制单用户登录 - single: false + single-login: false # 验证码 login-code: # 验证码类型配置 查看 LoginProperties 类 diff --git a/eladmin-system/src/main/resources/config/application-prod.yml b/eladmin-system/src/main/resources/config/application-prod.yml index 54bd74f5..ced1f592 100644 --- a/eladmin-system/src/main/resources/config/application-prod.yml +++ b/eladmin-system/src/main/resources/config/application-prod.yml @@ -51,7 +51,7 @@ login: # 登录缓存 cache-enable: true # 是否限制单用户登录 - single: false + single-login: false # 验证码 login-code: # 验证码类型配置 查看 LoginProperties 类