1.异常描述
- Exception:
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
- 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.
- at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
- at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
- at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
- at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
- at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
- at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
- at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3589)
- at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3478)
- at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4019)
- at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:951)
- at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1929)
- at com.mysql.jdbc.RowDataDynamic.nextRecord(RowDataDynamic.java:414)
- at com.mysql.jdbc.RowDataDynamic.next(RowDataDynamic.java:393)
- at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6883)
- at com.seven.migrate.MySQLJdbcTest.main(MySQLJdbcTest.java:58)
- Caused by: java.io.EOFException: Can not read response from server. Expected to read 18 bytes, read 14 bytes before connection was unexpectedly lost.
- at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3039)
- at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3545)
- ... 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时间。源代码如下:
- if (doStreaming && this.connection.getNetTimeoutForStreamingResults() > 0) {
- java.sql.Statement stmt = null;
- try {
- stmt = this.connection.createStatement();
- ((com.mysql.jdbc.StatementImpl)stmt).executeSimpleNonQuery(this.connection, "SET net_write_timeout="
- + this.connection.getNetTimeoutForStreamingResults());
- } finally {
- if (stmt != null) {
- stmt.close();
- }
- }
- }
因此我们需要为MySQL驱动的connection这个类设置NetTimeoutForStreamingResults。当不设置NetTimeoutForStreamingResults这个变量的时候,默认是600秒。
接下来查看MySQL的源代码,发现最后影响的是sockopt的SO_SNDTIMEO。
- void vio_timeout(Vio *vio, uint which, uint timeout)
- {
- r= setsockopt(vio->sd, SOL_SOCKET, which ? SO_SNDTIMEO : SO_RCVTIMEO,
- IF_WIN(const char*, const void*)&wait_timeout,
- sizeof(wait_timeout));
- }
在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。
- private void fill(int readAtLeastTheseManyBytes) throws IOException {
- checkClosed();
- this.currentPosition = 0; /* no mark: throw away the buffer */
- this.endOfCurrentData = currentPosition;
- // Read at least as many bytes as the caller wants, but don't
- // block to fill the whole buffer (like java.io.BufferdInputStream
- // does)
- int bytesToRead = Math.min(this.buf.length - currentPosition,
- readAtLeastTheseManyBytes);
- int bytesAvailable = this.underlyingStream.available();
- if (bytesAvailable > bytesToRead) {
- // Great, there's more available, let's grab those
- // bytes too! (read-ahead)
- bytesToRead = Math.min(this.buf.length - currentPosition,
- bytesAvailable);
- }
- if (this.doDebug) {
- StringBuffer debugBuf = new StringBuffer();
- debugBuf.append(" ReadAheadInputStream.fill(");
- debugBuf.append(readAtLeastTheseManyBytes);
- debugBuf.append("), buffer_size=");
- debugBuf.append(this.buf.length);
- debugBuf.append(", current_position=");
- debugBuf.append(currentPosition);
- debugBuf.append(", need to read ");
- debugBuf.append(Math.min(this.buf.length - currentPosition,
- readAtLeastTheseManyBytes));
- debugBuf.append(" bytes to fill request,");
- if (bytesAvailable > 0) {
- debugBuf.append(" underlying InputStream reports ");
- debugBuf.append(bytesAvailable);
- debugBuf.append(" total bytes available,");
- }
- debugBuf.append(" attempting to read ");
- debugBuf.append(bytesToRead);
- debugBuf.append(" bytes.");
- if (this.log != null) {
- this.log.logTrace(debugBuf.toString());
- } else {
- System.err.println(debugBuf.toString());
- }
- }
- int n = this.underlyingStream.read(this.buf, currentPosition,
- bytesToRead);
- if (n > 0) {
- endOfCurrentData = n + currentPosition;
- }
- }
http://frankfan915.iteye.com/blog/1672465
相关推荐
在将问题进一步聚焦之后,经过 Google 发现,在通过数据库连接池连接数据库中间件这种架构模式下,如果二者在个别参数上配置不匹配则会报“discard connection”或者“Communications link failure”的错误信息。...
8. **错误恢复**:能够检测并修复传输错误,确保文件完整性。 9. **日志记录**:详细记录每次传输活动,便于排查问题和审计。 10. **多语言支持**:包括中文在内的多种语言界面,方便全球用户使用。 通过以上分析...
java 连接数据库资源包 mysql-connector-java-8.0.23.zip druid-1.2.8.jar
本次异常的具体描述为:“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时,如果遇到“Server returns invalid timezone”错误,我们需要进行一些调整来解决这个问题。首先,你可以尝试通过MySQL的命令行客户端来改变MySQL服务器的时区设置。登录到MySQL服务器后,...
在IT领域,数据库连接是数据集成过程中的关键环节,特别是在使用工具如Kettle(也称为Pentaho Data Integration,简称PDI)时。Kettle是一款强大的ETL(Extract, Transform, Load)工具,用于从各种数据源抽取数据,...
主要介绍了阿里云ECS云服务器(linux系统)安装mysql后远程连接不了(踩坑),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这种现象在应用中可能导致突然的数据通信中断,表现为“Communications link failure”等错误,提示“Last packet sent to the server was 0 ms ago.”。为了解决这个问题,我们需要深入理解`wait_timeout`参数以及...
ERROR [org.hibernate.util.JDBCExceptionReporter] - Communications link failure Last packet sent to the server was 0 ms ago. 这种错误信息表明,MySQL 连接已经超时,无法与服务器进行通信。如果不是第一次...
错误信息可能表现为“***municationsException: Communications link failure due to underlying exception”以及一个***.ConnectException异常。这通常意味着无法建立到数据库服务器的网络连接。解决方法是确认IP...
在使用MyCat过程中,可能会遇到“Communications link failure”这类错误提示,表明与后端数据库的连接出现了问题。 **原因分析:** 1. **网络异常**:包括但不限于网络断开、路由器故障等情况。 2. **后端数据库...
在通信领域,BSS(Base Station Subsystem,基站子系统)信令是移动通信系统中的重要组成部分,特别是在GSM(Global System for Mobile Communications,全球移动通信系统)中。BSS信令主要涉及基站控制器(BSC)与...
如果应用程序未检测到这一情况,继续使用已被关闭的连接,将会导致通信链接失败异常,如"Communications link failure"。因此,为了防止此类异常,开发人员应在使用数据库连接时进行有效性检查,并及时关闭不再使用...
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 ...
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...
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 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 ...