# How to manage high level client connection to Elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-manage-high-level-client-connection-to-elasticsearch/283410>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 6, 2021, 7:13am UTC](https://discuss.elastic.co/t/how-to-manage-high-level-client-connection-to-elasticsearch/283410 "2021-09-06T07:13:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![111505](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111505/32/94191_2.png) [@111505](https://discuss.elastic.co/u/111505)\
**Post date:** [September 6, 2021, 7:13am UTC](https://discuss.elastic.co/t/how-to-manage-high-level-client-connection-to-elasticsearch/283410/1 "2021-09-06T07:13:38Z")

</div>

I wonder how to manage connection when using RestHighlevelClient in Java. The old connection is automatically disconnected. So I know that i need to connect and close when i need it. Is this right?

So what is I to be managed as follows?  
For example, If i want to search for Search,

1. Connect with Elasticsearch
2. Run the search command
3. Close the connection.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 6, 2021, 8:07am UTC](https://discuss.elastic.co/t/how-to-manage-high-level-client-connection-to-elasticsearch/283410/2 "2021-09-06T08:07:40Z")

</div>

You should not open a new HTTP connection for every search that you are executing. Initiating a TCP connection, possibly a TLS connection on top of that requires time that adds to your search.

The general approach is to create a single High Level Rest Client in your application and reuse that one. You only need to close it when you shut down your application.

Hope that makes sense!

---

<div class="post-metadata">

**Author:** ![111505](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111505/32/94191_2.png) [@111505](https://discuss.elastic.co/u/111505)\
**Post date:** [September 6, 2021, 8:31am UTC](https://discuss.elastic.co/t/how-to-manage-high-level-client-connection-to-elasticsearch/283410/3 "2021-09-06T08:31:53Z")

</div>

Thank you for answer.  
There is a problem that you are using as if you are currently answered.  
If i do not use it for a long time, it will automatically disconnect.  
What should I do in this case?  
Now I call Ping () and Retry is performing three times. But this is not correct.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 6, 2021, 9:31am UTC](https://discuss.elastic.co/t/how-to-manage-high-level-client-connection-to-elasticsearch/283410/4 "2021-09-06T09:31:17Z")

</div>

Hey,

can you explain why it automatically disconnects? I expect there is a firewall in between, that is killing the TCP connections. However the low level client should retry after some time. Did this not work in your case?

--Alex

---

<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:** [October 4, 2021, 9:31am UTC](https://discuss.elastic.co/t/how-to-manage-high-level-client-connection-to-elasticsearch/283410/5 "2021-10-04T09:31:41Z")

</div>

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