Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
e54dbe02
Unverified
Commit
e54dbe02
authored
Sep 28, 2020
by
ChenYuhao
Committed by
GitHub
Sep 28, 2020
Browse files
修复链接失败会一直重连,并且连接成功之后也不缓存下来 (#495)
Co-authored-by:
chenyh
<
chenyh.me@gamil.com
>
parent
53af24e1
Changes
1
Hide whitespace changes
Inline
Side-by-side
eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java
View file @
e54dbe02
...
@@ -20,13 +20,15 @@ import com.alibaba.druid.pool.DruidDataSource;
...
@@ -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 {
...
@@ -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,55 +48,44 @@ public class SqlUtils {
...
@@ -60,55 +48,44 @@ 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
);
DruidDataSource
druidDataSource
=
new
DruidDataSource
();
if
(!
map
.
containsKey
(
key
)
||
null
==
map
.
get
(
key
))
{
String
className
;
DruidDataSource
druidDataSource
=
new
DruidDataSource
();
try
{
className
=
DriverManager
.
getDriver
(
jdbcUrl
.
trim
()).
getClass
().
getName
();
String
className
;
}
catch
(
SQLException
e
)
{
try
{
throw
new
RuntimeException
(
"Get class name error: ="
+
jdbcUrl
);
className
=
DriverManager
.
getDriver
(
jdbcUrl
.
trim
()).
getClass
().
getName
();
}
}
catch
(
SQLException
e
)
{
if
(
StringUtils
.
isEmpty
(
className
))
{
throw
new
RuntimeException
(
"Get class name error: ="
+
jdbcUrl
);
DataTypeEnum
dataTypeEnum
=
DataTypeEnum
.
urlOf
(
jdbcUrl
);
}
if
(
null
==
dataTypeEnum
)
{
if
(
StringUtils
.
isEmpty
(
className
))
{
throw
new
RuntimeException
(
"Not supported data type: jdbcUrl="
+
jdbcUrl
);
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
.
setDriverClassName
(
dataTypeEnum
.
getDriver
());
}
else
{
druidDataSource
.
setDriverClassName
(
className
);
}
druidDataSource
.
setUrl
(
jdbcUrl
);
druidDataSource
.
setUrl
(
jdbcUrl
);
druidDataSource
.
setUsername
(
userName
);
druidDataSource
.
setUsername
(
userName
);
druidDataSource
.
setPassword
(
password
);
druidDataSource
.
setPassword
(
password
);
// 配置获取连接等待超时的时间
// 配置获取连接等待超时的时间
druidDataSource
.
setMaxWait
(
3000
);
druidDataSource
.
setMaxWait
(
3000
);
// 配置初始化大小、最小、最大
// 配置初始化大小、最小、最大
druidDataSource
.
setInitialSize
(
1
);
druidDataSource
.
setInitialSize
(
1
);
druidDataSource
.
setMinIdle
(
1
);
druidDataSource
.
setMinIdle
(
1
);
druidDataSource
.
setMaxActive
(
1
);
druidDataSource
.
setMaxActive
(
1
);
// 配置间隔多久才进行一次检测需要关闭的空闲连接,单位是毫秒
druidDataSource
.
setTimeBetweenEvictionRunsMillis
(
50000
);
// 配置一旦重试多次失败后等待多久再继续重试连接,单位是毫秒
druidDataSource
.
setTimeBetweenConnectErrorMillis
(
18000
);
// 配置一个连接在池中最小生存的时间,单位是毫秒
druidDataSource
.
setMinEvictableIdleTimeMillis
(
300000
);
// 这个特性能解决 MySQL 服务器8小时关闭连接的问题
druidDataSource
.
setMaxEvictableIdleTimeMillis
(
25200000
);
try
{
// 如果链接出现异常则直接判定为失败而不是一直重试
druidDataSource
.
init
(
);
druidDataSource
.
setBreakAfterAcquireFailure
(
true
);
}
catch
(
SQLException
e
)
{
try
{
log
.
error
(
"Exception during pool initialization"
,
e
);
druidDataSource
.
init
(
);
throw
new
RuntimeException
(
e
.
getMessage
());
}
catch
(
SQLException
e
)
{
}
log
.
error
(
"Exception during pool initialization"
,
e
);
map
.
put
(
key
,
druidDataSource
);
throw
new
RuntimeException
(
e
.
getMessage
()
);
}
}
return
map
.
get
(
key
);
return
druidDataSource
;
}
}
private
static
Connection
getConnection
(
String
jdbcUrl
,
String
userName
,
String
password
)
{
private
static
Connection
getConnection
(
String
jdbcUrl
,
String
userName
,
String
password
)
{
...
@@ -187,14 +164,14 @@ public class SqlUtils {
...
@@ -187,14 +164,14 @@ public class SqlUtils {
* @param sqlList /
* @param sqlList /
*/
*/
public
static
void
batchExecute
(
Connection
connection
,
List
<
String
>
sqlList
)
throws
SQLException
{
public
static
void
batchExecute
(
Connection
connection
,
List
<
String
>
sqlList
)
throws
SQLException
{
Statement
st
=
connection
.
createStatement
();
Statement
st
=
connection
.
createStatement
();
for
(
String
sql
:
sqlList
)
{
for
(
String
sql
:
sqlList
)
{
if
(
sql
.
endsWith
(
";"
))
{
if
(
sql
.
endsWith
(
";"
))
{
sql
=
sql
.
substring
(
0
,
sql
.
length
()
-
1
);
sql
=
sql
.
substring
(
0
,
sql
.
length
()
-
1
);
}
st
.
addBatch
(
sql
);
}
}
st
.
executeBatch
();
st
.
addBatch
(
sql
);
}
st
.
executeBatch
();
}
}
/**
/**
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment