HttpURLConnection - disable keep alive

I thought to share with you a simple http connection sense and a solution to a very annoying problem which wasn't so simple to find its source.
This problem/bug occurred once in a while, approx once to 10K requests.
short description of the system: i built an application server that have workers for various services, these workers create http requests to the server in order to get jobs, these jobs are sent to the workers in the response using JSON and so on..
The bug: some client requests were lost in stress test.
The solution: after connecting using HttpURLConnection to the server, it isn't enough just to close the BufferedReader and the DataOutputStream
    for example:      

if(reader!=null) reader.close();
if(output != null) output.close();

it is essential to disconnect the connection it self:

if(connection != null) connection.disconnect();

but also to disable the keep alive (before connecting)

connection.setRequestProperty("Connection", "close");

if the connection will not be disconnected, the server can use the same connection twice, so requests can be over-ridded, the keep alive is disabled also for preventing reusing connections.
Hope it helped and will save someone some time.

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at