简体   繁体   English

什么是 java.io.EOFException,消息:无法从服务器读取响应。 期望读取4字节,读取0字节

[英]what is java.io.EOFException, Message: Can not read response from server. Expected to read 4 bytes, read 0 bytes

This question has been asked a couple of times in SO and many times in other sites.这个问题在 SO 中被问过几次,在其他站点也被问过很多次。 But I didn't get any satisfiable answer.但我没有得到任何令人满意的答案。

My problem:我的问题:
I have a java web application which uses simple JDBC to connect to mysql database through Glassfish application server.我有一个 java web 应用程序,它使用简单的JDBC通过Glassfish应用服务器连接到mysql数据库。

I have used connection pooling in glassfish server with the following configurations:我在具有以下配置的 glassfish 服务器中使用了连接池:
Initial Pool Size: 25初始池大小:25
Maximum Pool Size: 100最大池大小:100
Pool Resize Quantity: 2池调整大小数量:2
Idle Timeout: 300 seconds空闲超时:300 秒
Max Wait Time: 60,000 milliseconds最长等待时间:60,000 毫秒

The application has been deployed for last 3 months and it was running flawlessly too.该应用程序已经部署了 3 个月,并且运行也非常完美。
But from last 2 days the following error is coming at the time of login.但从最近 2 天开始,登录时出现以下错误。

Partial StackTrace部分堆栈跟踪

com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:  

** BEGIN NESTED EXCEPTION **  

com.mysql.jdbc.CommunicationsException  
MESSAGE: Communications link failure due to underlying exception:  

** BEGIN NESTED EXCEPTION **  

java.io.EOFException  
MESSAGE: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.  

STACKTRACE:  

java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.  
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)  
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2411)  
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2916)  
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)  
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)  
at com.mysql.jdbc.Connection.execSQL(Connection.java:3256)  
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1313)  
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1448)  
............  
............  
my application traces....  

What caused this error suddenly?突然出现这个错误的原因是什么? I have lost a lot of time for this.我为此浪费了很多时间。

EDIT: The problem even persists after restarting the server.编辑:问题甚至在重新启动服务器后仍然存在。 As per DBA two of the important mysql server configurations are:根据 DBA,两个重要的 mysql 服务器配置是:
wait_timeout: 1800 seconds等待超时: 1800 秒
connect_timeout: 10 seconds连接超时: 10 秒
NOTE: Other applications deployed in the same server connecting to the same database and using different pools are running smoothly.注意:部署在同一台服务器上的其他应用程序连接到同一数据库并使用不同的池可以顺利运行。

EDIT-2: After reading a lot of things and expecting some positive outcome I made these changes to my connection pool. EDIT-2:在阅读了很多东西并期待一些积极的结果之后,我对我的连接池进行了这些更改。

Max Wait Time: 0 (previously it was 60 seconds)最大等待时间: 0(以前是 60 秒)
Connection Validation: Required连接验证:必需
Validation Method: table验证方法:表格
Table Name: Demo表名:演示
Validate Atmost Once: 40 seconds最多验证一次: 40 秒
Creation Retry Attempts: 1创建重试次数: 1
Retry Intervals: 5 seconds重试间隔: 5 秒
Max Connection Usage: 5最大连接使用: 5

And this worked as the application is running for 3 days consistently.这是因为应用程序持续运行了 3 天。 But I got a very strange and interesting result of out this.但是我得到了一个非常奇怪和有趣的结果。 While monitoring the connection pool, I found these figures:在监控连接池时,我发现了这些数字:

NumConnAcquired: 44919 Count NumConnAcquired: 44919 计数
NumConnReleased: 44919 Count NumConnReleased: 44919 计数
NumConnCreated: 9748 Count NumConnCreated: 9748 计数
NumConnDestroyed: 9793 Count NumConnDestroyed: 9793 计数
NumConnFailedValidation: 70 Count NumConnFailedValidation: 70 计数
NumConnFree: 161 Count NumConnFree: 161 计数
NumConnUsed: -136 Count NumConnUsed: -136 计数

How can the NumConnFree become 161 as I have Maximum Pool Size = 100 ?当我的Maximum Pool Size = 100时, NumConnFree如何变成 161
How can the NumConnUsed become -136, a negative number ? NumConnUsed怎么会变成-136,一个负数
How can the NumConnDestroyed > NumConnCreated ? NumConnDestroyed > NumConnCreated怎么

The connection has failed, possibly due to a firewall idle-timeout, etc. If you don't have your JDBC driver configured to reconnect on failure, then this error will not go away unless you open a new connection.连接失败,可能是由于防火墙空闲超时等原因。如果您没有将 JDBC 驱动程序配置为在失败时重新连接,那么除非您打开新连接,否则此错误不会消失。

If you are using a database connection pool (you are using one, right?), then you probably want to enable it's connection-checking features like issuing a query to check to see if the connection is working before handing it back to the application.如果您正在使用数据库连接池(您正在使用一个,对吗?),那么您可能希望启用它的连接检查功能,例如发出查询以检查连接是否正常工作,然后再将其交还给应用程序。 In Apache commons-dbcp, this is called the validationQuery and is often set to something simple like SELECT 1 .在 Apache commons-dbcp 中,这称为validationQuery并且通常设置为简单的东西,例如SELECT 1

Since you are using MySQL, you ought to use a Connector/J-specific "ping" query that is lighter-weight than actually issuing a true SQL query and set your validation query to /* ping */ SELECT 1 (the ping part needs to be exact ).由于您使用的是 MySQL,您应该使用一个比实际发出真正的 SQL 查询更轻量级的 Connector/J 特定的“ping”查询,并将您的验证查询设置为/* ping */ SELECT 1 (ping 部分需要准确地说)。

这是java中的EndOfFileException,当您的光标起点在终点或数据库连接因某些意外异常而关闭时发生。

It most likely means that the database has restarted or the network connection to the database has been broken (eg a NAT connection has timed out) ... and your webapp is trying to use a stale database connection.这很可能意味着数据库已重新启动或与数据库的网络连接已中断(例如 NAT 连接已超时)……并且您的 web 应用程序正在尝试使用过时的数据库连接。

If the problem persists after restarting the web container, it could be something more serious.如果重新启动 Web 容器后问题仍然存在,则可能是更严重的问题。


You asked the following:你问了以下问题:

How can the NumConnFree become 161 as I have Maximum Pool Size = 100 ?
How can the NumConnUsed become -136, a negative number ?
How can the NumConnDestroyed > NumConnCreated ? 

On the face of it, these don't make sense.从表面上看,这些没有意义。 However, they could simply be the result of some usage counters being updated in a non-thread-safe way.但是,它们可能只是某些使用计数器以非线程安全方式更新的结果。 This is not necessarily related to your original problem.这不一定与您的原始问题有关。

While I don't have definitive solutions, it seems something is interfering communication between the app server and db.虽然我没有明确的解决方案,但似乎有些东西干扰了应用程序服务器和数据库之间的通信。 Following are few things you can try to isolate the problems:以下是您可以尝试隔离问题的几件事:

  • Try to determine wether this is mysql problem or java code problem.尝试确定这是 mysql 问题还是 java 代码问题。 Try connect to mysql using the command line tool from the same host as the app server and issue similar SQL to perform login.尝试使用与应用服务器位于同一主机的命令行工具连接到 mysql,并发出类似的 SQL 以执行登录。 Test using a simple java code that does a select, deploy it to the same infrastructure, see what happen etc. Also check the mysql server log, see if you can find anything useful使用执行选择的简单 java 代码进行测试,将其部署到相同的基础设施,看看会发生什么等。还要检查 mysql 服务器日志,看看是否可以找到任何有用的东西

  • There are two way an idle connection get closed: by the connection pool code that runs inside app server, or by mysql itself.空闲连接有两种关闭方式:通过在应用服务器内部运行的连接池代码,或通过 mysql 本身。 Make sure you check the config on both sides确保检查双方的配置

  • Check if any network infrastructure config has changed recently.检查最近是否有任何网络基础设施配置发生了变化。 Was there any new firewall rule in place interfering with app server <--> mysql connectivity?是否有任何新的防火墙规则干扰了应用服务器 <--> mysql 连接? Was there any settings that prohibits open TCP connection idling longer than X?是否有任何设置禁止打开 TCP 连接空闲时间超过 X?

  • Try a different connection pooling library just to eliminate the possibility it's the connection pooling尝试不同的连接池库只是为了消除连接池的可能性

Good luck祝你好运

这可能是防火墙相关的问题。

I had this problem but I it wasn't possible for me to make changes in MySQL database configuration.我遇到了这个问题,但我无法更改 MySQL 数据库配置。 Therefore I ensured that in my sql connector class the connection is always closed before it's initiated again.因此,我确保在我的 sql 连接器类中,连接始终在再次启动之前关闭。 Something like:就像是:

public static Connection getConnection() {

    if (DatabaseConnnector.conn == null) {
        initConn();
    } else {
        try {
            DatabaseConnnector.conn.close();          
        } catch (SQLException e) {
            e.printStackTrace();
        }
          initConn();
    }
    return DatabaseConnnector.conn;
}

And this solved the problem.这解决了问题。

Got this error in PHPStorm while trying to connect and/or get some data from mysql server.尝试连接和/或从 mysql 服务器获取一些数据时,在 PHPStorm 中出现此错误。 Checkbox "single connection mode" fixed things for me.复选框“单连接模式”为我解决了问题。 I'll just leave it here.我就把它放在这里。 Perhaps this will help some martyr in the future.也许这会在未来帮助一些烈士。

https://www.jetbrains.com/help/phpstorm/configuring-database-connections.html https://www.jetbrains.com/help/phpstorm/configuring-database-connections.html

i meet the same problem, it caused by lock for too much time.我遇到了同样的问题,它是由锁定时间过长造成的。

we have a long transcation A, if execute it, the other trascation will be locked util transcation A finished, so the others always be killed by our mysql tool(pt-kill)我们有一个很长的事务A,如果执行它,另一个事务将被锁定 util事务A完成,所以其他事务总是被我们的mysql工具(pt-kill)杀死

In my case, it was the java mysql driver/connector version.在我的例子中,它是 java mysql 驱动程序/连接器版本。 Ie (Even if the server was version 8) somehow when I used mysql driver/connector version 5.1.44, then it started working.即(即使服务器是版本 8)不知何故,当我使用 mysql 驱动程序/连接器版本 5.1.44 时,它开始工作。

it may be the server connection is full try to reduce the server connection or modify the max connection param可能是服务器连接已满尝试减少服务器连接或修改最大连接参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL:EOFException:无法读取来自服务器的响应。 预期读取4个字节,在连接意外丢失之前读取0个字节 - MySQL : EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost Java:Hazelcast:java.io.EOFException:无法读取4个字节 - Java: Hazelcast : java.io.EOFException: Cannot read 4 bytes 从套接字读取/解密时发生java.io.EOFException - java.io.EOFException when read/decrypt from a socket 尝试从套接字读取时发生java.io.EOFException - java.io.EOFException when try to read from a socket 每次我读取RandomAccessFile都会引发java.io.EOFException - java.io.EOFException Thrown everytime I read a RandomAccessFile 使用DataInputStream.readUTF读取String时的java.io.EOFException - java.io.EOFException at using DataInputStream.readUTF to read String 我想读写文件,运行时出现此错误:java.io.EOFException - I want to write and read from a file and I have this error when I run : java.io.EOFException 从文件Java读取字节 - Read bytes from file Java 从Java中的套接字读取字节 - Read bytes from socket in Java TwelveMonkeys 3.1.1:PSD文件ImageIO.read给出java.io.EOFException:PackBits流的意外结束 - TwelveMonkeys 3.1.1: PSD file ImageIO.read gives java.io.EOFException: Unexpected end of PackBits stream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM