com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure错误解决

com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure错误解决

项目上线一段时间后,多个环境经常出现***.mysql.cj.jdbc.exceptions.***municationsException: ***munications link failure错误,堆栈信息如下:
The last packet su***essfully received from the server was 10,003 milliseconds ago. The last packet sent su***essfully to the server was 10,003 milliseconds ago.
at ***.mysql.cj.jdbc.exceptions.SQLError.create***municationsException(SQLError.java:174)
at ***.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at ***.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1200)
at ***.alibaba.druid.pool.vendor.MySqlValidConnectionChecker.isValidConnection(MySqlValidConnectionChecker.java:140)
at ***.alibaba.druid.pool.DruidAbstractDataSource.validateConnection(DruidAbstractDataSource.java:1440)
at ***.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1812)
at ***.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2877)
Caused by: ***.mysql.cj.exceptions.CJ***municationsException: ***munications link failure

The last packet su***essfully received from the server was 10,003 milliseconds ago. The last packet sent su***essfully to the server was 10,003 milliseconds ago.
at sun.reflect.NativeConstructorA***essorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorA***essorImpl.newInstance(NativeConstructorA***essorImpl.java:62)
at sun.reflect.DelegatingConstructorA***essorImpl.newInstance(DelegatingConstructorA***essorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at ***.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at ***.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
at ***.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
at ***.mysql.cj.exceptions.ExceptionFactory.create***municationsException(ExceptionFactory.java:167)
at ***.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:538)
at ***.mysql.cj.protocol.a.NativeProtocol.checkErrorMessage(NativeProtocol.java:702)
at ***.mysql.cj.protocol.a.NativeProtocol.send***mand(NativeProtocol.java:641)
at ***.mysql.cj.protocol.a.NativeProtocol.sendQueryPacket(NativeProtocol.java:940)
at ***.mysql.cj.protocol.a.NativeProtocol.sendQueryString(NativeProtocol.java:886)
at ***.mysql.cj.NativeSession.execSQL(NativeSession.java:1073)
at ***.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1168)
… 4 ***mon frames omitted
Caused by: java.***.SocketTimeoutException: Read timed out
at java.***.SocketInputStream.socketRead0(Native Method)
at java.***.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.***.SocketInputStream.read(SocketInputStream.java:171)
at java.***.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:475)
at sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:469)
at sun.security.ssl.SSLSocketInputRecord.bytesIn***pletePacket(SSLSocketInputRecord.java:69)
at sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1228)
at sun.security.ssl.SSLSocketImpl.a***ess 300 ( S S L S o c k e t I m p l . j a v a : 75 ) a t s u n . s e c u r i t y . s s l . S S L S o c k e t I m p l 300(SSLSocketImpl.java:75) at sun.security.ssl.SSLSocketImpl 300(SSLSocketImpl.java:75)atsun.security.ssl.SSLSocketImplAppInputStream.read(SSLSocketImpl.java:915)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at ***.mysql.cj.protocol.FullReadInputStream.readFully(FullReadInputStream.java:64)
at ***.mysql.cj.protocol.a.SimplePacketReader.readHeader(SimplePacketReader.java:63)
at ***.mysql.cj.protocol.a.SimplePacketReader.readHeader(SimplePacketReader.java:45)
at ***.mysql.cj.protocol.a.TimeTrackingPacketReader.readHeader(TimeTrackingPacketReader.java:52)
at ***.mysql.cj.protocol.a.TimeTrackingPacketReader.readHeader(TimeTrackingPacketReader.java:41)
at ***.mysql.cj.protocol.a.MultiPacketReader.readHeader(MultiPacketReader.java:54)
at ***.mysql.cj.protocol.a.MultiPacketReader.readHeader(MultiPacketReader.java:44)
at ***.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:532)
… 10 ***mon frames omitted
经过多次排查和测试,终于排查到了原因。
出现问题的原因是因为我们的项目需要动态配置数据源,程序中根据需要即时动态创建DruidDataSource,没有设置SocketTimeout和ConnectTimeout参数,默认值都是0,有文章说:connectTimeout的默认值为0,表示驱动层面不设置超时时间,但这并不意味着不会超时,此时将由操作系统来决定超时时间。实际经过测试,当SocketTimeout和ConnectTimeout为默认值0时,两个超时时间可能都为10秒。
测试代码:

public static void main(String[] args) throws Exception {

    //创建数据源
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8");
    druidDataSource.setUsername("admin");
    druidDataSource.setPassword("admin");
    druidDataSource.setDriverClassName("***.mysql.cj.jdbc.Driver");
    String querySql = "SELECT sleep(11)";	//执行语句,该行语句的执行时间≥11秒
    druidDataSource.setConnectionErrorRetryAttempts(3);       //失败后重连次数
    druidDataSource.setBreakAfterAcquireFailure(true);
    druidDataSource.setMaxWait(30 * 1000);	//保持的最长时间
    
    // 以下配置自动检测链接的可用性
    druidDataSource.setTestWhileIdle(true);	//明确指定检测空闲链接是否可用
    druidDataSource.setValidationQuery("select 1");	//执行该SQL来检测空闲链接,如果不配置,实际不会执行检测
    //如果链接空闲时长超过60秒则执行检测,周而复始。如果该时长少于MySQL数据变量:wait_timeout 会出现链接不可用的情况。
    druidDataSource.setTimeBetweenEvictionRunsMillis(60 * 1000);
    
    druidDataSource.setSocketTimeout(20 * 1000);//超时时长:20秒,该值要大于执行语句的执行时间
    druidDataSource.setConnectTimeout(20 * 1000);//超时时长:20秒,该值要大于执行语句的执行时间

    List<Map<String, Object>> result = JdbcUtils.executeQuery(druidDataSource, querySql, Lists.newArrayList());
    System.out.println(JSONUtilExt.toJson(result));
}

请注意测试代码中的这两行代码:
druidDataSource.setSocketTimeout(20 * 1000);
druidDataSource.setConnectTimeout(20 * 1000);
当我将这两行代码中的任意一行代码注释掉或两行代码都注释掉时,都会出现文章开始的错误,且错误日志相同。

分析到此,基本可以确定,程序里出现上面错误就是因为SocketTimeout和ConnectTimeout没有设置,导致操作系统里实际默认的时间是10秒,而因为项目已经上线一段时间,个别表里数据量比较大,所以在执行语句时偶尔会出现执行时间超过10秒的情况,连接超时。因此,根据需要,在创建数据源时,增加设置SocketTimeout和ConnectTimeout的大小就可以避免再次出现上述错误。

转载请说明出处内容投诉
CSS教程_站长资源网 » com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure错误解决

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买