# ElasticSearch Java API client - connection reset by peer

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-api-client-connection-reset-by-peer/313454>\
**Category:** Elasticsearch\
**Created:** [September 1, 2022, 2:29pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-connection-reset-by-peer/313454 "2022-09-01T14:29:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ch\_mohansai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ch_mohansai/32/110340_2.png) [@ch\_mohansai](https://discuss.elastic.co/u/ch_mohansai)\
**Post date:** [September 1, 2022, 2:29pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-connection-reset-by-peer/313454/1 "2022-09-01T14:29:29Z")

</div>

I have noticed that after upgrading my service to Java api client it is giving random connection reset by peer error, where the 1 st request always failed , later it worked and after 10 minutes this happens again. My service started and sent a query to ES and everything worked well. After 10 minutes I sent insert request and it failed on connection reset by peer.

I have tried updating the connection timeout parameters as mentioned in the configuration [here](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.4/_timeouts.html), but it didn't work.

Any thoughts?

```auto
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\n\t... 38 common frames omitted\nCaused by: java.io.IOException: Connection reset by peer\n\tat org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:885)\n\tat org.elasticsearch.client.RestClient.performRequest(RestClient.java:283)\n\tat org.elasticsearch.client.RestClient.performRequest(RestClient.java:270)\n\tat co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:146)\n\tat co.elastic.clients.elasticsearch.ElasticsearchClient.index(ElasticsearchClient.java:946)\n\tat com.xxx.xxx.search.manager.data.ElasticSearchRepository.indexEntity(ElasticSearchRepository.java:51)\n\t... 52 common frames omitted\nCaused by: java.io.IOException: Connection reset by peer\n\tat sun.nio.ch.FileDispatcherImpl.read0(Native Method)\n\tat sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)\n\tat sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)\n\tat sun.nio.ch.IOUtil.read(IOUtil.java:197)\n\tat sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)\n\tat org.apache.http.nio.reactor.ssl.SSLIOSession.receiveEncryptedData(SSLIOSession.java:478)\n\tat org.apache.http.nio.reactor.ssl.SSLIOSession.isAppInputReady(SSLIOSession.java:540)\n\tat org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:120)\n\tat org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)\n\tat org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)\n\tat org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)\n\tat org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)\n\tat org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)\n\tat org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)\n\t... 1 common frames 

```

Configuration:

```auto

@Configuration
@ConfigurationProperties(prefix = "es")
@ComponentScan(basePackages = {"com.xxx.xxx"})
public class ElasticSearchConfiguration {

    @Value("#{'${es.hosts}'.split(',')}")
    private List<String> hosts;

    @Value("${es.port}")
    private Integer port;

    @Value("${es.scheme}")
    private String scheme;

    @Value("${es.username}")
    private String username;

    @Value("${es.password}")
    private String password;

    @Bean
    public ElasticsearchClient client() {
        return new ElasticsearchClient(transport());
    }

    private ElasticsearchTransport transport() {
        return new RestClientTransport(restClient(),new JacksonJsonpMapper());
    }

    @Bean
    public RestClient restClient() {
        HttpHost[] extractHosts = extractHosts(hosts, scheme);

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

        return RestClient.builder(extractHosts)
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
                        .setConnectTimeout(10000)
                        .setSocketTimeout(60000)).build();
    }

    private HttpHost[] extractHosts(List<String> hosts, String scheme) {
        return hosts.stream().map(extractHost -> new HttpHost(extractHost, port, scheme)).toArray(HttpHost[]::new);
    }
}

```

---

<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:** [September 29, 2022, 2:30pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-connection-reset-by-peer/313454/2 "2022-09-29T14:30:13Z")

</div>

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