Friday, July 16, 2021

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 the 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 the server closed the connection before the client has read the response, but, in general, it can come to an end of the TCP socket, so you must check the log files for both clients and server to find out who is complaining. If the 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 the 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 the Server is waiting for the response from some other process e.g. another and that is overloaded and in the meantime, the client request has timed out at the 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 like 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 then doing following will reproduce the problem:
  1. Client sends a request req#1
  2. Server reads the first request req#1, processes, writes a 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 a 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 the database timed out the connection when left idle and when the Java application tries to connect to the database using those stale connections 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 queries 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 the 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 the 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 every situation is different and it may require different solutions 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]


1 comment :

Unknown said...

How to know if host server supports jtds.jdbc or jdbc for android application

Post a Comment