Druid 1.1.24 在控制台打印"discard long time none received connection. , jdbcUrl : jdbc:mysql://..."错误日志

参考资料

报错原因

  1. 我使用的是mysql 数据库,驱动包使用的是mysql-connector-java-8.0.27.jar
  2. druid 使用的是 druid-1.1.24.jar
  3. 经过每个包的翻看,在 com.alibaba.druid.pool.vendor 包下查到这个MySqlValidConnectionChecker类 ,根据类名猜测出这个是 MySQL 数据连接校验类.
  4. MySqlValidConnectionChecker 源码如下:
点击查看代码
public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {

    public static final int DEFAULT_VALIDATION_QUERY_TIMEOUT = 1;
public static final String DEFAULT_VALIDATION_QUERY = "SELECT 1"; private static final long serialVersionUID = 1L;
private static final Log LOG = LogFactory.getLog(MySqlValidConnectionChecker.class); private Class<?> clazz;
private Method ping;
private boolean usePingMethod = false; public MySqlValidConnectionChecker(){
try {
clazz = Utils.loadClass("com.mysql.jdbc.MySQLConnection");
if (clazz == null) {
clazz = Utils.loadClass("com.mysql.cj.jdbc.ConnectionImpl");
} if (clazz != null) {
ping = clazz.getMethod("pingInternal", boolean.class, int.class);
} if (ping != null) {
usePingMethod = true;
}
} catch (Exception e) {
LOG.warn("Cannot resolve com.mysql.jdbc.Connection.ping method. Will use 'SELECT 1' instead.", e);
} configFromProperties(System.getProperties());
} @Override
public void configFromProperties(Properties properties) {
String property = properties.getProperty("druid.mysql.usePingMethod");
if ("true".equals(property)) {
setUsePingMethod(true);
} else if ("false".equals(property)) {
setUsePingMethod(false);
}
} public boolean isUsePingMethod() {
return usePingMethod;
} public void setUsePingMethod(boolean usePingMethod) {
this.usePingMethod = usePingMethod;
} public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
if (conn.isClosed()) {
return false;
} if (usePingMethod) {
if (conn instanceof DruidPooledConnection) {
conn = ((DruidPooledConnection) conn).getConnection();
} if (conn instanceof ConnectionProxy) {
conn = ((ConnectionProxy) conn).getRawObject();
} if (clazz.isAssignableFrom(conn.getClass())) {
if (validationQueryTimeout <= 0) {
validationQueryTimeout = DEFAULT_VALIDATION_QUERY_TIMEOUT;
} try {
ping.invoke(conn, true, validationQueryTimeout * 1000);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof SQLException) {
throw (SQLException) cause;
}
throw e;
}
return true;
}
} String query = validateQuery;
if (validateQuery == null || validateQuery.isEmpty()) {
query = DEFAULT_VALIDATION_QUERY;
} Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
if (validationQueryTimeout > 0) {
stmt.setQueryTimeout(validationQueryTimeout);
}
rs = stmt.executeQuery(query);
return true;
} finally {
JdbcUtils.close(rs);
JdbcUtils.close(stmt);
} } }
  1. 根据源码中 isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) 方法,有个判断 :
// 如果使用 ping 方法,则会反射调用 ping 方法验证数据库连接是否有效,而不执行 validateQuery 验证.
if (usePingMethod) {
if (conn instanceof DruidPooledConnection) {
conn = ((DruidPooledConnection) conn).getConnection();
} if (conn instanceof ConnectionProxy) {
conn = ((ConnectionProxy) conn).getRawObject();
} if (clazz.isAssignableFrom(conn.getClass())) {
if (validationQueryTimeout <= 0) {
validationQueryTimeout = DEFAULT_VALIDATION_QUERY_TIMEOUT;
} try {
ping.invoke(conn, true, validationQueryTimeout * 1000);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof SQLException) {
throw (SQLException) cause;
}
throw e;
}
return true;
}
}

那么 usePingMethod 是怎么来的?
查看构造器方法:

public MySqlValidConnectionChecker(){
try {
// 先加载的驱动类
clazz = Utils.loadClass("com.mysql.jdbc.MySQLConnection");
if (clazz == null) {
clazz = Utils.loadClass("com.mysql.cj.jdbc.ConnectionImpl");
}
// 反射获取驱动类的 pingInternal 方法
if (clazz != null) {
ping = clazz.getMethod("pingInternal", boolean.class, int.class);
}
// 若驱动类的 pingInternal 方法如果存在则 usePingMethod = true
if (ping != null) {
usePingMethod = true;
}
} catch (Exception e) {
LOG.warn("Cannot resolve com.mysql.jdbc.Connection.ping method. Will use 'SELECT 1' instead.", e);
}
// 从系统配置文件配置属性,
configFromProperties(System.getProperties());
}
@Override
public void configFromProperties(Properties properties) {
// 从系统属性(JVM启动参数)中获取'druid.mysql.usePingMethod'属性 覆盖 usePingMethod 的值
String property = properties.getProperty("druid.mysql.usePingMethod");
if ("true".equals(property)) {
setUsePingMethod(true);
} else if ("false".equals(property)) {
setUsePingMethod(false);
}
}

以上可以得出结论, 通过配置JVM启动参数"druid.mysql.usePingMethod=false" 告知 Druid 停止使用 usePingMethod 方法验证数据库连接,转而使用 validateQuery 验证.

解决方法

1. JVM启动参数添加如下配置

-Ddruid.mysql.usePingMethod=false

Druid 1.1.24 在控制台打印"discard long time none received connection. , jdbcUrl : jdbc:mysql://...."错误日志的更多相关文章

  1. druid discard long time none received connection问题解析

    最新项目中用的druid连接数据库遇到一个困扰很久的问题 1 开始用的druid版本是1.1.22版本,由于业务需求,单个连接需要执行很久,理论上不需要用到自动回收,但为了安全,还是加了自动回收,时间 ...

  2. Spring Boot 集成日志logback + 控制台打印SQL

    一: 控制台打印SQL application.properties中添加如下即可在控制台打印sql logging.level.com.fx.fxxt.mapper=debug 二:日志 因为Spr ...

  3. 关于在Xcode控制台打印的注意点

    注意!!在控制台中打印语句的返回值,这句代码也算是被执行过了一次 比如在下列代码的if语句执行之前,现在控制台打印 [_dataBaseexecuteUpdate:createSql] 的布尔值 if ...

  4. mybatis3.2.3+spring3 控制台打印sql解决办法

    学习mybatis的时候遇到打印不出sql 的问题,在这里做个总结: 1:首先log4j.properties这样配置: log4j.rootLogger=DEBUG,console,R log4j. ...

  5. myeclipse 控制台打印空指针 ,黏贴控制台sql到plsql有结果集,异常处理

    信用公司框架,不够熟悉. 在完成嗲点登录后,写动态页面是遇到,了问题:myeclipse 控制台打印空指针 ,黏贴控制台sql到plsql有结果集,异常处理. 最后大神给看,在接口实现重写的方法里返回 ...

  6. VS2010-win32下cocos2dx控制台打印的方法

    在xcode中  直接使用printf 或者 cout<<""<<endl;可以直接在控制台打印 但是在VS2010 却死活不好用   真郁闷 ------ ...

  7. 控制台打印Hibernate的SQL语句显示绑定参数值

    问题? 使用Hibernate提供的show_sql内置属性true只能输出类似于下面的SQL语句:Hibernate:   insert into user(name,password) value ...

  8. node 在控制台打印有色彩的输出

    在学习 node 过程中,因为没有找到有断点的调试方法,只能退而次之,在控制台打印调试. 但整个控制台的输出都是一种颜色,有时候很难找到自己需要的信息,这时,有颜色的打印就会帮上很大的忙. conso ...

  9. Spring Boot使用AOP在控制台打印请求、响应信息

    AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等. AOP简介 AOP全称Aspect Oriented Programming,面向切面,AOP主要实现的 ...

随机推荐

  1. CSS 选择器学习总结

    1.id 选择器 #idname{color:red;} 2.class选择器 .classname{} 3.标签选择器 div{} 4.通配符选择器 *{} 5. 属性选择器 [id]{ } 5.选 ...

  2. Net之多线程用法

    1.多线程 2.线程池 3.Task using System; using System.Collections.Generic; using System.Linq; using System.T ...

  3. Golang 数据结构

    每种语言在实现数据结构有些许不同.go 是如何实现的呢? 1. 数组 Array go 中数组是相同的元素组成的集合,计算机会为数组分配一段连续的内存来保存元素,可以利用索引快速访问元素. go 中数 ...

  4. Maven——setting.xml配置

    <settings> <localRepository>C:\Users\gcl\.m2\repository</localRepository> <serv ...

  5. http请求中的三种参数类型

    1.URL参数:实际就是querry string的方式,参数拼接在url之后以?隔开,参数之间以&连接. 优点:简单,页面跳转比较快. 缺点:1.基于浏览器对urk长度有限制,不能超过204 ...

  6. web安全常用端口

    21 FTP 22 SSH 23 Telent 25 SMTP 53 DNS 80 HTTP 135 139 443 HTTPS 445 SMB 1433 SQLSERVER 1521 ORCAL 3 ...

  7. SpringMVC 和SpringBoot中的注解是如何起作用的,如何实现的

    SpringMVC源码解读 - HandlerMapping - RequestMappingHandlerMapping初始化   https://www.cnblogs.com/leftthen/ ...

  8. mq 的缺点 ?

    系统可用性降低 系统引入的外部依赖越多,越容易挂掉,本来你就是 A 系统调用 BCD 三个系统的 接口就好了,人 ABCD 四个系统好好的,没啥问题,你偏加个 MQ 进来,万一 MQ 挂了咋整?MQ ...

  9. Servlet之间的关联

  10. 微信小程序要求HTTPS,如何选择SSL证书?

    为了保护小程序应用安全,微信官方的需求文档要求,每个微信小程序必须事先设置一个通讯域名,并通过HTTPS请求进行网络通信,不满足条件的域名和协议无法请求.因此开发者应先准备好配置好HTTPS证书的域名 ...