Druid 1.2.3 discard long time none received connection原因及解决方法
druid
阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
项目地址:https://gitcode.com/gh_mirrors/druid/druid
免费下载资源
·
异常
discard long time none received connection. , jdbcUrl : jdbc:mysql://******?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false, version : 1.2.3, lastPacketReceivedIdleMillis : 172675
源码
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
if (conn.isClosed()) {
return false;
} else {
if (this.usePingMethod) {
if (conn instanceof DruidPooledConnection) {
conn = ((DruidPooledConnection)conn).getConnection();
}
if (conn instanceof ConnectionProxy) {
conn = ((ConnectionProxy)conn).getRawObject();
}
if (this.clazz.isAssignableFrom(conn.getClass())) {
if (validationQueryTimeout <= 0) {
validationQueryTimeout = 1;
}
try {
this.ping.invoke(conn, true, validationQueryTimeout * 1000);
return true;
} catch (InvocationTargetException var11) {
Throwable cause = var11.getCause();
if (cause instanceof SQLException) {
throw (SQLException)cause;
}
throw var11;
}
}
}
String query = validateQuery;
if (validateQuery == null || validateQuery.isEmpty()) {
query = "SELECT 1";
}
Statement stmt = null;
ResultSet rs = null;
boolean var7;
try {
stmt = conn.createStatement();
if (validationQueryTimeout > 0) {
stmt.setQueryTimeout(validationQueryTimeout);
}
rs = stmt.executeQuery(query);
var7 = true;
} finally {
JdbcUtils.close(rs);
JdbcUtils.close(stmt);
}
return var7;
}
}
this.usePingMethod为true时会调用ping检查连接,除非抛异常,否则返回true;另一种是select 1方式检查。
if (this.ping != null) {
this.usePingMethod = true;
}
该类的构造方法中初始化为true,但是可以通过以下方式配置
public void configFromProperties(Properties properties) {
if (properties != null) {
String property = properties.getProperty("druid.mysql.usePingMethod");
if ("true".equals(property)) {
this.setUsePingMethod(true);
} else if ("false".equals(property)) {
this.setUsePingMethod(false);
}
}
}
解决方式
一、启动时增加参数
-Ddruid.mysql.usePingMethod=false
二、Spring Boot启动类增加以下代码
/* 解决 discard long time none received connection */
static {
System.setProperty("druid.mysql.usePingMethod","false");
}
三、自定义DruidConfig配置类增加
@PostConstruct
public void setProperties(){
System.setProperty("druid.mysql.usePingMethod","false");
}
GitHub 加速计划 / druid / druid
27.83 K
8.56 K
下载
阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
最近提交(Master分支:3 个月前 )
f060c270 - 5 天前
1613a765
* Improve gaussdb ddl parser
* fix temp table 6 天前
更多推荐
已为社区贡献5条内容
所有评论(0)