# What is the correct way of handling client in Elasticsearch?

**URL:** <https://discuss.elastic.co/t/what-is-the-correct-way-of-handling-client-in-elasticsearch/43975>\
**Category:** Elasticsearch\
**Created:** [March 10, 2016, 4:04am UTC](https://discuss.elastic.co/t/what-is-the-correct-way-of-handling-client-in-elasticsearch/43975 "2016-03-10T04:04:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![javajpn](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@javajpn](https://discuss.elastic.co/u/javajpn)\
**Post date:** [March 10, 2016, 4:04am UTC](https://discuss.elastic.co/t/what-is-the-correct-way-of-handling-client-in-elasticsearch/43975/1 "2016-03-10T04:04:51Z")

</div>

is creating and closing client in each functions the correct way ? Let's take we have 3 functions createIndex(), createMapping(), importDataToES().

```
           initClientObject()
                     createIndex()
           client.close()
           //method 2
           initClientObject()
                     createMapping()
           client.close()
           //method3
           initClientObject()
                     importDataToES()
           client.close()
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 10, 2016, 6:21am UTC](https://discuss.elastic.co/t/what-is-the-correct-way-of-handling-client-in-elasticsearch/43975/2 "2016-03-10T06:21:45Z")

</div>

No. Create one single client instance for your VM. It's threadsafe.

---

<div class="post-metadata">

**Author:** ![javajpn](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@javajpn](https://discuss.elastic.co/u/javajpn)\
**Post date:** [March 10, 2016, 6:59am UTC](https://discuss.elastic.co/t/what-is-the-correct-way-of-handling-client-in-elasticsearch/43975/3 "2016-03-10T06:59:45Z")

</div>

Will there be a performance issue, if we keep the connection open for long time? Because in this case can create connection(i.e client) when we start the application and can destroy it when we shutdown the application.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 10, 2016, 7:25am UTC](https://discuss.elastic.co/t/what-is-the-correct-way-of-handling-client-in-elasticsearch/43975/4 "2016-03-10T07:25:42Z")

</div>

People are doing that for years now with the Java client.

Yes, create a client when the application starts and close it when the application stops.

---

<div class="post-metadata">

**Author:** ![javajpn](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@javajpn](https://discuss.elastic.co/u/javajpn)\
**Post date:** [March 10, 2016, 7:35am UTC](https://discuss.elastic.co/t/what-is-the-correct-way-of-handling-client-in-elasticsearch/43975/5 "2016-03-10T07:35:06Z")

</div>

Thanks for the reply.. Let us check

---

<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:** [July 5, 2017, 11:09pm UTC](https://discuss.elastic.co/t/what-is-the-correct-way-of-handling-client-in-elasticsearch/43975/6 "2017-07-05T23:09:41Z")

</div>


