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. CF1548B题解

    在日报上面看到的,发现 NOIP 模拟赛考过这个 trick( 首先我们把题目要求的条件这么写: \[a_i=x_i \times m+k \] 那么我们要找到满足条件的数组,差分后的数组一定都是 \ ...

  2. 基于ECS搭建云上博客(云小宝码上送祝福,免费抽iphone13任务详解)

    码上送祝福,带云小宝回家 做任务免费抽iphone13,还可得阿里云新春限量手办 日期:2021.12.27-2022.1.16 云小宝地址:https://developer.aliyun.com/ ...

  3. Superset安装部署操作

    目录 1.安装Miniconda 1.下载Miniconda 2.安装 3.开启一个新的shell窗口 4.设置新窗口不自动开启conda 2.创建Python3.7环境 1.配置国内镜像 2.常用命 ...

  4. (一)【转】asp.net mvc生成验证码

    网站添加验证码,主要为防止机器人程序批量注册,或对特定的注册用户用特定程序暴力破解方式,以进行不断的登录.灌水等危害网站的操作.验证码被广泛应用在注册.登录.留言等提交信息到服务器端处理的页面中.   ...

  5. Vue中import和require的对比

    Vue中import和require的对比 一.前言 ​ vue框架想必是我们前端朋友们必学的知识点,说它难也没有那么难,说简单也没有那么简单,主要技术就是那么几个,可是里面的细节很多,有些时候我们会 ...

  6. error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数。。。使用

    一,问题描述 MSVCRTD.lib(crtexew.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引 ...

  7. Kerberos与各大组件的集成

    1. 概述 Kerberos可以与CDH集成,CDH里面可以管理与hdfs.yarn.hbase.yarn.kafka等相关组件的kerberos凭证.但当我们不使用CDH的时候,也需要了解hdfs. ...

  8. 学习Tomcat(二)

    一. Java简介 JDK: 面向开发人员使用的SDK,提供Java的开发环境和运行环境 SDK: 软件开发包,包括函数库.编译程序等 JRE: Java的运行环境,面向Java的使用者,不是开发者 ...

  9. 学习SVN02

    代码发布方案: 1,安装,优化 软件环境,(nginx,lvs)  <-------运维工程师 2,程序代码(不断更新).   <--------开发工程师,(开发,运维都可以发布) 3, ...

  10. 查看mysql相关信息

    查看本机mysql的相关信息,执行以下SQL即可: SHOW VARIABLES LIKE "%char%";