`
m635674608
  • 浏览: 5004655 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Communications link failure错误分析

 
阅读更多

1.异常描述

Java代码  收藏代码
  1. Exception:  
  2. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure  
  3.   
  4. The last packet successfully received from the server was 7 milliseconds ago.  The last packet sent successfully to the server was 1,023,250 milliseconds ago.  
  5.     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)  
  6.     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)  
  7.     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)  
  8.     at java.lang.reflect.Constructor.newInstance(Constructor.java:513)  
  9.     at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)  
  10.     at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)  
  11.     at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3589)  
  12.     at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3478)  
  13.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4019)  
  14.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:951)  
  15.     at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1929)  
  16.     at com.mysql.jdbc.RowDataDynamic.nextRecord(RowDataDynamic.java:414)  
  17.     at com.mysql.jdbc.RowDataDynamic.next(RowDataDynamic.java:393)  
  18.     at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6883)  
  19.     at com.seven.migrate.MySQLJdbcTest.main(MySQLJdbcTest.java:58)  
  20. Caused by: java.io.EOFException: Can not read response from server. Expected to read 18 bytes, read 14 bytes before connection was unexpectedly lost.  
  21.     at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3039)  
  22.     at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3545)  
  23.     ... 8 more  

 2. 问题分析:

 

从网上分析得知,此问题一般是MySQL端数据发送timeout所致。MySQL中可以设置net_write_timeout这个变量,来调整IO写的时候的超时时间。

 

connect_timeout

The number of seconds that the mysqld server waits for a connect packet before responding with Bad handshake.

interactive_timeout

The number of seconds the server waits for activity on an interactive connection before closing it.

wait_timeout

The number of seconds the server waits for activity on a noninteractive connection before closing it.

net_read_timeout

The number of seconds to wait for more data from a connection before aborting the read.

net_write_timeout

The number of seconds to wait for a block to be written to a connection before aborting the write.

 

由此可知,当客户端长时间不接收数据的时候,就会断开连接。接下来我们尝试在MySQL这边设置net_write_timeout,但发现还是无效,百思不得其解。

查看jdbc驱动发现,当调用PreparedStatement的executeQuery()方法的时候,如果我们是去获取流式resultset的话,就会默认执行SET net_write_timeout= ? 这个命令去重新设置timeout时间。源代码如下:

 

Java代码  收藏代码
  1. if (doStreaming && this.connection.getNetTimeoutForStreamingResults() > 0) {  
  2.                   
  3.                 java.sql.Statement stmt = null;  
  4.                   
  5.                 try {  
  6.                     stmt = this.connection.createStatement();  
  7.                       
  8.                     ((com.mysql.jdbc.StatementImpl)stmt).executeSimpleNonQuery(this.connection, "SET net_write_timeout="   
  9.                             + this.connection.getNetTimeoutForStreamingResults());  
  10.                 } finally {  
  11.                     if (stmt != null) {  
  12.                         stmt.close();  
  13.                     }  
  14.                 }  
  15.             }  

 因此我们需要为MySQL驱动的connection这个类设置NetTimeoutForStreamingResults。当不设置NetTimeoutForStreamingResults这个变量的时候,默认是600秒。

 

接下来查看MySQL的源代码,发现最后影响的是sockopt的SO_SNDTIMEO。

 

C代码  收藏代码
  1. void vio_timeout(Vio *vio, uint which, uint timeout)  
  2. {  
  3.   r= setsockopt(vio->sd, SOL_SOCKET, which ? SO_SNDTIMEO : SO_RCVTIMEO,  
  4.                 IF_WIN(const char*, const void*)&wait_timeout,  
  5.                 sizeof(wait_timeout));  
  6. }  

 在msdn上有如下解释

 

setsockopt(SO_RCVTIMEO) "If a send or receive operation times out on a 
socket, the socket state is indeterminate, and should not be used; TCP 
sockets in this state have a potential for data loss, since the 
operation could be canceled at the same moment the operation was to be 
completed." 

 

3.问题重现

在类ReadAheadInputStream中的fill方法中的this.underlyingStream.read处设置一个断点。停留长于NetTimeoutForStreamingResults的时间,这个问题就会重现了。MySQL驱动最终用来读取网络数据的对象是underlyingStream。

 

 

Java代码  收藏代码
  1. private void fill(int readAtLeastTheseManyBytes) throws IOException {  
  2.         checkClosed();  
  3.   
  4.         this.currentPosition = 0/* no mark: throw away the buffer */  
  5.   
  6.         this.endOfCurrentData = currentPosition;  
  7.   
  8.         // Read at least as many bytes as the caller wants, but don't  
  9.         // block to fill the whole buffer (like java.io.BufferdInputStream  
  10.         // does)  
  11.   
  12.         int bytesToRead = Math.min(this.buf.length - currentPosition,  
  13.                 readAtLeastTheseManyBytes);  
  14.   
  15.         int bytesAvailable = this.underlyingStream.available();  
  16.   
  17.         if (bytesAvailable > bytesToRead) {  
  18.   
  19.             // Great, there's more available, let's grab those  
  20.             // bytes too! (read-ahead)  
  21.   
  22.             bytesToRead = Math.min(this.buf.length - currentPosition,  
  23.                     bytesAvailable);  
  24.         }  
  25.   
  26.         if (this.doDebug) {  
  27.             StringBuffer debugBuf = new StringBuffer();  
  28.             debugBuf.append("  ReadAheadInputStream.fill(");  
  29.             debugBuf.append(readAtLeastTheseManyBytes);  
  30.             debugBuf.append("), buffer_size=");  
  31.             debugBuf.append(this.buf.length);  
  32.             debugBuf.append(", current_position=");  
  33.             debugBuf.append(currentPosition);  
  34.             debugBuf.append(", need to read ");  
  35.             debugBuf.append(Math.min(this.buf.length - currentPosition,  
  36.                     readAtLeastTheseManyBytes));  
  37.             debugBuf.append(" bytes to fill request,");  
  38.   
  39.             if (bytesAvailable > 0) {  
  40.                 debugBuf.append(" underlying InputStream reports ");  
  41.                 debugBuf.append(bytesAvailable);  
  42.   
  43.                 debugBuf.append(" total bytes available,");  
  44.             }  
  45.   
  46.             debugBuf.append(" attempting to read ");  
  47.             debugBuf.append(bytesToRead);  
  48.             debugBuf.append(" bytes.");  
  49.   
  50.             if (this.log != null) {  
  51.                 this.log.logTrace(debugBuf.toString());  
  52.             } else {  
  53.                 System.err.println(debugBuf.toString());  
  54.             }  
  55.         }  
  56.   
  57.         int n = this.underlyingStream.read(this.buf, currentPosition,  
  58.                 bytesToRead);  
  59.   
  60.         if (n > 0) {  
  61.             endOfCurrentData = n + currentPosition;  
  62.         }  
  63.     }  

 

http://frankfan915.iteye.com/blog/1672465

分享到:
评论

相关推荐

    Durid连接Mycat+MySQL过程中报1243错误的解决办法

    在将问题进一步聚焦之后,经过 Google 发现,在通过数据库连接池连接数据库中间件这种架构模式下,如果二者在个别参数上配置不匹配则会报“discard connection”或者“Communications link failure”的错误信息。...

    特好用的FTP管理工具

    8. **错误恢复**:能够检测并修复传输错误,确保文件完整性。 9. **日志记录**:详细记录每次传输活动,便于排查问题和审计。 10. **多语言支持**:包括中文在内的多种语言界面,方便全球用户使用。 通过以上分析...

    mysql-connector-java-8.0.23.zip druid-1.2.8.jar 资源包

    java 连接数据库资源包 mysql-connector-java-8.0.23.zip druid-1.2.8.jar

    mysql 异常com.mysql.jdbc.CommunicationsException

    本次异常的具体描述为:“Communications link failure due to underlying exception: **BEGINNESTED EXCEPTION** java.io.EOFException STACK TRACE: java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully...

    pycharm工具连接mysql数据库失败问题

    当PyCharm尝试连接MySQL时,如果遇到“Server returns invalid timezone”错误,我们需要进行一些调整来解决这个问题。首先,你可以尝试通过MySQL的命令行客户端来改变MySQL服务器的时区设置。登录到MySQL服务器后,...

    Kettle所有数据库数据库连接驱动Jar

    在IT领域,数据库连接是数据集成过程中的关键环节,特别是在使用工具如Kettle(也称为Pentaho Data Integration,简称PDI)时。Kettle是一款强大的ETL(Extract, Transform, Load)工具,用于从各种数据源抽取数据,...

    阿里云ECS云服务器(linux系统)安装mysql后远程连接不了(踩坑)

    主要介绍了阿里云ECS云服务器(linux系统)安装mysql后远程连接不了(踩坑),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    MySQL wait_timeout连接超时

    这种现象在应用中可能导致突然的数据通信中断,表现为“Communications link failure”等错误,提示“Last packet sent to the server was 0 ms ago.”。为了解决这个问题,我们需要深入理解`wait_timeout`参数以及...

    关于MySQL的wait-timeout连接超时问题报错解决方案.docx

    ERROR [org.hibernate.util.JDBCExceptionReporter] - Communications link failure Last packet sent to the server was 0 ms ago. 这种错误信息表明,MySQL 连接已经超时,无法与服务器进行通信。如果不是第一次...

    JDBC 程序的常见错误及调试方法

    错误信息可能表现为“***municationsException: Communications link failure due to underlying exception”以及一个***.ConnectException异常。这通常意味着无法建立到数据库服务器的网络连接。解决方法是确认IP...

    mycat使用禁区

    在使用MyCat过程中,可能会遇到“Communications link failure”这类错误提示,表明与后端数据库的连接出现了问题。 **原因分析:** 1. **网络异常**:包括但不限于网络断开、路由器故障等情况。 2. **后端数据库...

    BSS信令详解 掌握各信令的组成和用途

    在通信领域,BSS(Base Station Subsystem,基站子系统)信令是移动通信系统中的重要组成部分,特别是在GSM(Global System for Mobile Communications,全球移动通信系统)中。BSS信令主要涉及基站控制器(BSC)与...

    浅析mysql交互式连接&非交互式连接

    如果应用程序未检测到这一情况,继续使用已被关闭的连接,将会导致通信链接失败异常,如"Communications link failure"。因此,为了防止此类异常,开发人员应在使用数据库连接时进行有效性检查,并及时关闭不再使用...

    LCTF软件备份VariSpec™ Liquid Crystal Tunable Filters

    Of course, to use the new functions one must link the application code with the new .lib file containing these functions. Previous release: 1.20 Release date December 3rd, 2003 Known bugs: a) there ...

    TeeChart2013_131216_SourceCode

    applications as executables or dynamic link libraries, including as OCX ActiveX Controls or ActiveX Forms, excepting compilation as design-time packages or compilation into a DLL or OCX for use in a...

    TeeChart2013_130818_SourceCode

    applications as executables or dynamic link libraries, including as OCX ActiveX Controls or ActiveX Forms, excepting compilation as design-time packages or compilation into a DLL or OCX for use in a...

    Vagaa哇嘎画时代--体验群体智慧的力量!

    Vagaa does not have any responsibility or liability for any information, data, communications, products or materials available on such third-party sites. These linked and framed sites are only for ...

Global site tag (gtag.js) - Google Analytics