Merge branch 'master' of github.com:elunez/eladmin

pull/516/head
ZhengJie 2020-10-11 15:14:37 +08:00
commit 718c1460df
3 changed files with 51 additions and 74 deletions

View File

@ -20,13 +20,15 @@ import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.StringUtils; import com.alibaba.druid.util.StringUtils;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import javax.sql.DataSource; 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.nio.charset.StandardCharsets;
import java.sql.*; import java.sql.*;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author / * @author /
@ -36,20 +38,6 @@ public class SqlUtils {
public static final String COLON = ":"; public static final String COLON = ":";
private static volatile Map<String, DruidDataSource> 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,10 +48,7 @@ public class SqlUtils {
* @return DataSource * @return DataSource
*/ */
private static DataSource getDataSource(String jdbcUrl, String userName, String password) { 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(); DruidDataSource druidDataSource = new DruidDataSource();
String className; String className;
try { try {
className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName(); className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName();
@ -91,24 +76,16 @@ public class SqlUtils {
druidDataSource.setMinIdle(1); druidDataSource.setMinIdle(1);
druidDataSource.setMaxActive(1); druidDataSource.setMaxActive(1);
// 配置间隔多久才进行一次检测需要关闭的空闲连接,单位是毫秒 // 如果链接出现异常则直接判定为失败而不是一直重试
druidDataSource.setTimeBetweenEvictionRunsMillis(50000); druidDataSource.setBreakAfterAcquireFailure(true);
// 配置一旦重试多次失败后等待多久再继续重试连接,单位是毫秒
druidDataSource.setTimeBetweenConnectErrorMillis(18000);
// 配置一个连接在池中最小生存的时间,单位是毫秒
druidDataSource.setMinEvictableIdleTimeMillis(300000);
// 这个特性能解决 MySQL 服务器8小时关闭连接的问题
druidDataSource.setMaxEvictableIdleTimeMillis(25200000);
try { try {
druidDataSource.init(); druidDataSource.init();
} catch (SQLException e) { } catch (SQLException e) {
log.error("Exception during pool initialization", e); log.error("Exception during pool initialization", e);
throw new RuntimeException(e.getMessage()); throw new RuntimeException(e.getMessage());
} }
map.put(key, druidDataSource);
} return druidDataSource;
return map.get(key);
} }
private static Connection getConnection(String jdbcUrl, String userName, String password) { private static Connection getConnection(String jdbcUrl, String userName, String password) {

View File

@ -49,7 +49,7 @@ login:
# 登录缓存 # 登录缓存
cache-enable: true cache-enable: true
# 是否限制单用户登录 # 是否限制单用户登录
single: false single-login: false
# 验证码 # 验证码
login-code: login-code:
# 验证码类型配置 查看 LoginProperties 类 # 验证码类型配置 查看 LoginProperties 类

View File

@ -51,7 +51,7 @@ login:
# 登录缓存 # 登录缓存
cache-enable: true cache-enable: true
# 是否限制单用户登录 # 是否限制单用户登录
single: false single-login: false
# 验证码 # 验证码
login-code: login-code:
# 验证码类型配置 查看 LoginProperties 类 # 验证码类型配置 查看 LoginProperties 类