org.apache.http.ConnectionClosedException: Connection is closed

org.apache.http.ConnectionClosedException: Connection is closed
at org.elasticsearch.client.RestClient.extractAndWrapCause(
at org.elasticsearch.client.RestClient.performRequest(
at org.elasticsearch.client.RestClient.performRequest(
at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(
at this error in Java spring boot occasionally for elasticsearch-java:8.8.1

how can I handle this in Elasticsearch Config class

public class ESRestClient {

    private String hostName;
    private int port;
    private String username;
    private String password;

    public ElasticsearchClient getElasticSearchClient() {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                new UsernamePasswordCredentials(username, password));

        RestClientBuilder builder = RestClient.builder(new HttpHost(hostName, port, "HTTPS"))
                        httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

        // Create the low-level client
        RestClient restClient =;

        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

        // And create the API client
        return new ElasticsearchClient(transport);
                httpClientBuilder -> httpClientBuilder

I added the following code snippet to handle the connection pool

still getting the connection closed issue

I also recently observe this issue in production not sure why it happened but it was intermittent. Would like to know the root cause of this and how it can be avoided in future.

A ConnectionClosedException is raised by the http library when a connection has been closed unexpectedly.

This is likely caused by a network connectivity issue between the application and the Elasticsearch server.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.