How to fix java.net.SocketException: Software caused connection abort: recv failed

Out of many client servers related socket errors here is one more interesting socket related error from Java program, "java.net.SocketException: Software caused connection abort: recv failed". The key point in this error message is "abort" and "recv", which means is someone (client or server) is trying to read from a closed connection.This error usually comes at the client socket end, when server closed the connection before the client has read the response, but, in general, it can come to any end of TCP socket, so you must check the log files for both client and server to find out who is complaining. If Server is complaining then it's fine and the client has closed the TCP connection may be due to timeout or any RuntimeException at the client end.

If client's log file contains this error it means your client is fine and it's the server who is closing the connection prematurely. This generally happens when Server is waiting for the response from some other process e.g. another and that is overloaded and in the meantime, client request has timed out at Server end.

Many Java developers encounter "java.net.SocketException: Software caused connection abort: recv failed" error at different places e.g. it can come when your Java program is connected to MySQL database and left running idle for many hours. You may get this while using products written in Java e.g. Tomcat, Apache Cassandra or Apache Axis 2. It can also be related to database problems. Just check if Server is healthy and why it's closing the connection.



Scenario 1:  java.net.SocketException: Software caused connection abort: recv failed

If you are using a client-server application than doing following will reproduces the problem:
  1. Client sends a request req#1
  2. Server reads the first request req#1, processes, writes response
  3. Server closes connection
  4. Client sends second request req#2
  5. Client tries to read server response: "java.net.SocketException: Software caused connection abort: recv failed" as shown below:

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(SocketInputStream.java:0)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.net.SocketInputStream.read(SocketInputStream.java:182)

Solution 1:
Check why your client is sending the second request before reading the response of the first request. Look for any multi-threading angle if the error is intermittent.

How to solve java.net.SocketException: Software caused connection abort: recv failed


Scenario 1:  java.net.SocketException: Software caused connection abort: recv failed

I have also seen this error while working with a Java Web application connecting to database. When you left  your application overnight and there is no request to process during the night, in the morning it was throwing "java.sql.SQLException: Communication link failure: java.net.SocketException, underlying cause: Software caused connection abort: recv failed" error.

The real cause of the problem was that database timed out the connection when left idle and when Java application tries to connect to the database using those stale connection it throws java.sql.SQLException: Communication link failure: java.net.SocketException, underlying cause: Software caused connection abort: recv failed error.

Solution 2:
Since we were using the c3po connection pool from Hibernate and enabled connection.autoReconnectconnection.autoReconnectForPools, and connection.is-connection-validation-required set, I was wondering why this issue is coming even if we auto connect enabled.  The real reason was that we have not provided the validationQuery property and JDBC drivers need validation query to test the connection during the idle period.

You can use queries like "SELECT 1" or "SELECT 1 from DUAL" depending upon which database you are connecting In our case, it was Microsoft SQL SERVER 2008 but this error can happen with any database e.g MySQL or Oracle. Use the second validation query if you are connecting to Oracle database from Java application using the c3p0 connection pool.

If you are not using c3p0 but using Apache DBCP connection pool i.e. using org.apache.commons.dbcp.BasicDataSource  in your Spring config file then uses relevant property so that JDBC connection pool can automatically test the pooled database connection.


That's all about java.net.SocketException: Software caused connection abort: recv failed". You see the root cause is that one party, usually the server has closed the connection but the client is trying to read from that connection. I understand the every situation is different and it may require different solution but knowing this fundamental will help you to quickly troubleshoot your issue.


Other Java troubleshooting Guides
  • java.net.SocketException: Failed to read from SocketChannel: Connection reset by peer [fix]
  • java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet [fix]
  • 2 ways to solve Unsupported major.minor version 51.0 error in Java [solutions]
  • How to solve java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver [solution]
  • java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver [solution
  • How to solve java.lang.UnsatisfiedLinkError: no ocijdbc11 in Java [solution]
  • java.lang.ClassNotFoundException: org.postgresql.Driver [fix]
  • Fixing Unsupported major.minor version 52.0 Error in Java [solution]
  • java.io.IOException: Map failed and java.lang.OutOfMemoryError: Map failed  [fix]
  • Exception in thread "main" java.lang.ExceptionInInitializerError in Java Program [fix]
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 [solution]
  • org.hibernate.MappingException: Unknown entity Exception in Java [solution]
  • java.net.BindException: Cannot assign requested address: JVM_Bind [fix]
  • java.net.SocketException: Too many files open java.io.IOException [solution]

Further Reading
TCP/IP Sockets in Java
Java Network Programming


No comments :

Post a Comment