# org.apache.http.ConnectionClosedException: Connection is closed

**URL:** <https://discuss.elastic.co/t/org-apache-http-connectionclosedexception-connection-is-closed/348402>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 1, 2023, 7:38am UTC](https://discuss.elastic.co/t/org-apache-http-connectionclosedexception-connection-is-closed/348402 "2023-12-01T07:38:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcpeiris](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tcpeiris](https://discuss.elastic.co/u/tcpeiris)\
**Post date:** [December 1, 2023, 7:38am UTC](https://discuss.elastic.co/t/org-apache-http-connectionclosedexception-connection-is-closed/348402/1 "2023-12-01T07:38:00Z")

</div>

org.apache.http.ConnectionClosedException: Connection is closed  
at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:920)  
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:300)  
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:288)  
at co.elastic.clients.transport.rest\_client.RestClientTransport.performRequest(RestClientTransport.java:154)  
at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1887)  
at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1904)getting this error in Java spring boot occasionally for elasticsearch-java:8.8.1

how can I handle this in Elasticsearch Config class

```auto
@Configuration
@ConfigurationProperties("es")
public class ESRestClient {

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

    @Bean
    public ElasticsearchClient getElasticSearchClient() {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(username, password));

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

        // Create the low-level client
        RestClient restClient = builder.build();

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

        // And create the API client
        return new ElasticsearchClient(transport);
    }
}

```

---

<div class="post-metadata">

**Author:** ![tcpeiris](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tcpeiris](https://discuss.elastic.co/u/tcpeiris)\
**Post date:** [December 4, 2023, 6:43pm UTC](https://discuss.elastic.co/t/org-apache-http-connectionclosedexception-connection-is-closed/348402/2 "2023-12-04T18:43:09Z")

</div>

```auto
                httpClientBuilder -> httpClientBuilder
                        .setDefaultCredentialsProvider(credentialsProvider)
                        .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                        .setMaxConnTotal(maxConnTotal)
                        .setMaxConnPerRoute(maxConnPerRoute));

```

I added the following code snippet to handle the connection pool

---

<div class="post-metadata">

**Author:** ![tcpeiris](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tcpeiris](https://discuss.elastic.co/u/tcpeiris)\
**Post date:** [December 6, 2023, 10:20am UTC](https://discuss.elastic.co/t/org-apache-http-connectionclosedexception-connection-is-closed/348402/3 "2023-12-06T10:20:23Z")

</div>

still getting the connection closed issue

---

<div class="post-metadata">

**Author:** ![Vishal\_Aggarwal](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@Vishal\_Aggarwal](https://discuss.elastic.co/u/Vishal_Aggarwal)\
**Post date:** [December 13, 2023, 12:15am UTC](https://discuss.elastic.co/t/org-apache-http-connectionclosedexception-connection-is-closed/348402/4 "2023-12-13T00:15:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [December 13, 2023, 9:06am UTC](https://discuss.elastic.co/t/org-apache-http-connectionclosedexception-connection-is-closed/348402/5 "2023-12-13T09:06:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 10, 2024, 9:06am UTC](https://discuss.elastic.co/t/org-apache-http-connectionclosedexception-connection-is-closed/348402/6 "2024-01-10T09:06:59Z")

</div>

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