Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MySQL Connector/J: EOFException, expected to read 4 bytes, read 0 bytes

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

Short answer

The MySQL server or something between you and it closed the connection, and the driver found out only when it tried to use it. The usual reason is an idle connection that outlived wait_timeout or a firewall timeout. Make the pool retire and test connections before then, and rule out a server crash or oversized packet.

  • Applies to MySQL Connector/J 9, MySQL 8.4, HikariCP 6, Java 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the server how long it keeps idle connections, and whether it restarted recently. Both facts separate the causes below.

    SHOW VARIABLES LIKE 'wait_timeout';
    SHOW GLOBAL STATUS LIKE 'Uptime';
  2. 2Match what you see, then jump to its fix

    1. 1The error appears on the first request after a quiet period, and the same request works when repeatedThe server closed an idle pooled connection (wait_timeout)Fix
    2. 2The failures follow a fixed idle time that is shorter than wait_timeoutA firewall, router or load balancer drops idle connectionsFix
    3. 3Uptime is short, or the MySQL error log shows a restart at the time of the failureThe server crashed, restarted, or killed the connectionFix

What does the error look like?

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

Cause 1 of 3

The server closed an idle pooled connection (wait_timeout)

1Check

MySQL closes connections that stay idle longer than wait_timeout (28800 seconds, eight hours, by default). A pool that holds a connection longer than that hands out a dead one.

SHOW VARIABLES LIKE 'wait_timeout';

2Fix

Keep the pool's maxLifetime several seconds below the shortest limit in the path (server wait_timeout, firewall, load balancer) and keep keepaliveTime below maxLifetime. HikariCP's default maxLifetime is 30 minutes, so this matters when wait_timeout is lower than that.

# application.properties (Spring Boot, HikariCP); example for wait_timeout = 600 s
spring.datasource.hikari.max-lifetime=570000
spring.datasource.hikari.keepalive-time=120000

3Verify

Leave the app idle past the old limit and call it again.

curl -i http://localhost:8080/health/db

Confirmed by github.com

Did this fix it?

Cause 2 of 3

A firewall, router or load balancer drops idle connections

1Check

The Connector/J guide says firewalls or routers may clamp down on idle connections, and that the MySQL protocol does not ping. Find the idle limit of every device between app and database.

mysql -h db.example.com -e "SHOW VARIABLES LIKE 'wait_timeout'"

2Fix

Set the pool's max lifetime below the shortest idle limit on the path, and make sure the JDBC URL does not turn off tcpKeepAlive (it is on by default).

jdbc:mysql://db.example.com:3306/app

3Verify

Idle the app past the firewall limit and repeat a request.

curl -i http://localhost:8080/health/db

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

The server crashed, restarted, or killed the connection

1Check

A packet that is too large or out of order, a crash and restart, or a KILL of the thread all end the connection. Compare the server's uptime and error log with the time of the exception.

mysqladmin version
sudo tail -n 50 /var/log/mysql/error.log

2Fix

Fix the reason found in the log. If it is oversized packets, raise max_allowed_packet on the server or send fewer rows per statement.

SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet';

3Verify

Repeat the workload that failed.

SHOW GLOBAL STATUS LIKE 'Uptime';

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Set the pool's max lifetime below every idle limit between the app and the database.
  • Do not use autoReconnect: the Connector/J guide advises against it and recommends a pool.
  • Keep the time a connection stays idle short, and release it as soon as the work is done.
  • Watch the server's uptime and error log so restarts are found before users report them.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Databases

MySQL refuses to delete a row because other rows point to it

SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails

Error 1451 means at least one child row still references the parent row you are deleting or changing.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.