Saturday, July 24, 2021

How to Fix Broken pipe in Java - Cause and Solution

Hello guys, If you have worked in a client-server Java application then you may be familiar with the  " Broken pipe" error, It is one of the many socket-related errors you will see on the server-side application log.  This error comes intermittently when many clients are connecting to a server and many clients closing the connection before a response is fully transferred. Some of the common causes of Broken pipe exception includes
  • The user closed the browser before the page loaded.
  • Their internet connection failed during loading.
  • They went to another page before the page loaded.
  • The browser timed the connection out before the page loaded (would have to be a large page).

Btw, this is my third article in the series on how to deal with Socket related exceptions in Java. If you haven't read the other articles of this series, here are them

And, is how the error looks like when it comes into your application log file: Broken pipe
at Method)

This error says that the connection was closed when the server was writing a response, possibly due to timeout.  Btw, I expect that you are familiar with errors and exceptions in Java and how to handle them. If you are a complete beginner, I would suggest you join a comprehensive Java course to first understand basics like how to handle errors and exceptions.

If you need a recommendation then I suggest you check out The Complete Java Programming MasterClass course by Tim Buchalaka on Udemy. It's not only the most comprehensive course but also the most up-to-date and covers news features from recent Java releases. It's also very affordable and you can buy in just $10 on many Udemy flash sales which happen every now and then.

What to do when " Broken pipe" error comes?

This is an important question, what are you really going to do when you see this error in your log file? Are you going to ignore it, or are you going to find out the root cause, will you inform developers? I'll give you some idea and context to deal with Broken pipe error in a server-side Java application. 

In general, for a Server application, it's important to handle this kind of error so that server is not affected by the client.  As a support person, most of the time, you can ignore this error because the client is disconnected before the response is fully sent.

The warning log basically means that the connection to the client browser is aborted before the response is fully transferred. It is a harmless warning as it can be due to transient network problems or the browser aborts/refreshes the page before it loaded.

These broken pipe exceptions happen when the client (browser) has closed the connection, but the server (your tag) continues to try to write to the stream.  This usually happens when someone clicks Back, Stop, etc. in the browser and it disconnects from the server before the request is finished. Sometimes, it can happen because, for example, the Content-Length header is incorrect (and the browser takes its value as true).

Btw, If you want to learn more about Socket Programming and Network Programming in Java the I also suggest you check the Java Network Programming - TCP/IP Socket Programming course on Udemy.

Dealing with Broken pipe in Java

Usually, this is a non-event, and nothing to worry about. But if you are seeing them in your dev environment when you know you have not interrupted your browser, you might dig a bit more to find out why.

The first is that many databases have a timeout for inactive connections ( the MySQL default is 8 hours, but on some hosts, this may be configured in seconds!). After some time, the database kills this idle connection in an attempt to save resources, as it concludes that someone simply forgot it open.

When, on the client-side, your pool decides to use it, the socket quickly realizes that the connection was closed on the other side, it was broken (hence the name broken pipe ). Very common when starting to use a pool!

The second reason is that you may be handling your transactions without due care: forgetting to commit or rollback in some cases. The database can then kill this connection after some transaction timeout, but your pool does not know it, and when you use that connection, it's broken!

This is a very low-level issue, so if your application is using socket indirectly like via JDBC for opening database connection, or by using Hibernate which opens and closes the database connection, or by using a connection pool library like C3PO which does that.

Other Java troubleshooting articles you may like:

  • java.sql.SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error? (hint)
  • java.sql.SQLServerException: The index 58 is out of range - JDBC (solution)
  • How to fix Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger (solution)
  • How to fix 'javac' is not recognized as an internal or external command (solution)
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory error (solution)
  • Common reasons of java.lang.ArrayIndexOutOfBoundsException in Java? (solution)
  • java.lang.ClassNotFoundException : 
  • How to avoid ConcurrentModificationException in Java? (tutorial)
  • org.Springframework.Web.Context.ContextLoaderListener (solution)
  • How to connect to MySQL database in Java? (tutorial)
  • Fixing java.lang.UnsupportedClassVersionError Unsupported major.minor version 50.0 (solution)
  • How to solve "could not create the Java virtual machine" error in Java? (solution)
  • How to solve java.lang.OutOfMemoryError: Java Heap Space in Eclipse, Tomcat? (solution)
  • How to fix "Error: Could not find or load main class" in Eclipse? (guide)
  • How to solve java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver? (solution)
  • 10 common reasons for java.lang.NumberFormatException in Java? (tutorial)
  • How to fix the "illegal start of expression" compile-time error in Java? (tutorial)
  • Cause and solution of "class, interface, or enum expected" compiler error in Java? (fix)
  • How to solve "variable might not have initialized" compile-time error in Java? (answer)

Thanks for reading this article, if you like this article then please share it with your friends and colleagues too. If you have any doubt or questions then please drop a comment.

P. S. -  It's worth noting that both Socket and Network Programming is advanced skills and worth learning. If you want to learn more about Socket Programming in Java, I suggest you check the Java: Socket Programming Simplified, a free course on Udemy.


  1. I didnt find any solution in the article, can you please state what should be the resolution? is there any property i should add in application , I am closing the connections as soon as the query executed and provided results . Please suggest

    1. Hello Anonymous, can you tell more about what error are you getting? may be just post the stack trace? also a bit more about your application? Do you have any Timeout, how many clients are connecting at a time? As most of the Socket errors are due to network, timeout, and client side errors so having a context around is important. From you message, you are doing find if you are closing the connections as soon as your request is completed but just make sure that you receive the response before you close the connection


Feel free to comment, ask questions if you have any doubt.