MySQL Connector/J: EOFException, expected to read 4 bytes, read 0 bytes
java.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.
Fix it fast
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';2Match what you see, then jump to its fix
- 1
The 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
The failures follow a fixed idle time that is shorter than wait_timeoutA firewall, router or load balancer drops idle connectionsFix - 3
Uptime is short, or the MySQL error log shows a restart at the time of the failureThe server crashed, restarted, or killed the connectionFix
- 1
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=1200003Verify
Leave the app idle past the old limit and call it again.
curl -i http://localhost:8080/health/dbConfirmed by github.com
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/app3Verify
Idle the app past the firewall limit and repeat a request.
curl -i http://localhost:8080/health/dbConfirmed by dev.mysql.com
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.log2Fix
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
EntityNotFoundException when loading an entity that has a missing related row
ConstraintViolationException on an orphaned foreign key
EntityNotFoundException means Hibernate followed an id and no row was there.
3 causes, each with a fixDatabases
Hibernate fails on a CREATE or ALTER TABLE statement at startup
Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement
Hibernate generated a CREATE, ALTER or DROP statement and MySQL refused it.
3 causes, each with a fixDatabases
Java cannot log in to MySQL 8: authentication plugin caching_sha2_password not supported
ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
MySQL 8 accounts use the caching_sha2_password plugin by default, and Connector/J releases before 8.0.9 cannot use it.
3 causes, each with a fixDatabases
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 fixDatabases
JDBC cannot talk to MySQL: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.
Communications link failure means the TCP link to MySQL could not be opened or was closed.
3 causes, each with a fixDatabases
MySQL: could not create connection to database server
Could not create connection to database server. Attempted reconnect 3 times. Giving up.
These messages are wrappers.
3 causes, each with a fix