# Releasing elasticsearch connection

**URL:** https://discuss.elastic.co/t/releasing-elasticsearch-connection/20990
**Category:** Elasticsearch
**Created:** [November 30, 2014, 12:23pm UTC](https://discuss.elastic.co/t/releasing-elasticsearch-connection/20990 "2014-11-30T12:23:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![prachicsa](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@prachicsa](https://discuss.elastic.co/u/prachicsa)
#### Post date: [November 30, 2014, 12:23pm UTC](https://discuss.elastic.co/t/releasing-elasticsearch-connection/20990/1 "2014-11-30T12:23:58Z")

</div>

Hi,

I am deploying a webapp, which internally queries elasticsearch. I am  
creating only one connection, and I want to close it when the webapp shuts  
down. I am using singleton pattern for creating the ES client and how do I  
destroy/close ES client? When the application shuts down, will the client  
get closed or I have to specify it/ where should I have the code to close  
the connection?

This is my code:

```
public class ESClientManager {

    public static Client client = null;
    private static Settings settings = null;
    private static Object mutex = new Object();
    private static final String CONFIG_CLUSTER_NAME = "cluster.name";
    private static final String CLUSTER_NAME = "qatool_es";
    private static final String[] transportAddress = {
           "127.0.0.1"
    };

    private static final int transportPort = 9300;

    private ESClientManager() {
    }

    public static Client getClient() {
        if (client == null) {
            synchronized (mutex) {
                settings = ImmutableSettings.settingsBuilder()
                        .put(CONFIG_CLUSTER_NAME, CLUSTER_NAME).build();

                client = new TransportClient(settings);
                for (int i = 0; i < transportAddress.length - 1; i++) {
                    ((TransportClient) client).addTransportAddress(new 

```

InetSocketTransportAddress(transportAddress[i], transportPort));  
}  
logger.info("Elastic search client initialized");  
}  
}  
logger.info("Returning the existing client");  
return client;  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a25c2c6e-82d3-4d7b-a9f0-2717dbc59225%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a25c2c6e-82d3-4d7b-a9f0-2717dbc59225%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Jason\_Wee](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jason\_Wee](https://discuss.elastic.co/u/Jason_Wee)
#### Post date: [November 30, 2014, 5:04pm UTC](https://discuss.elastic.co/t/releasing-elasticsearch-connection/20990/2 "2014-11-30T17:04:31Z")

</div>

Have you try using client.close() ?

Jason

On Sun, Nov 30, 2014 at 8:23 PM, [prachicsa@gmail.com](mailto:prachicsa@gmail.com) wrote:

> Hi,
> 
> I am deploying a webapp, which internally queries elasticsearch. I am  
> creating only one connection, and I want to close it when the webapp shuts  
> down. I am using singleton pattern for creating the ES client and how do I  
> destroy/close ES client? When the application shuts down, will the client  
> get closed or I have to specify it/ where should I have the code to close  
> the connection?
> 
> This is my code:
> 
> ```
> public class ESClientManager {
> 
> public static Client client = null;
> private static Settings settings = null;
> private static Object mutex = new Object();
> private static final String CONFIG_CLUSTER_NAME = "cluster.name";
> private static final String CLUSTER_NAME = "qatool_es";
> private static final String[] transportAddress = {
> "127.0.0.1"
> };
> 
> private static final int transportPort = 9300;
> 
> private ESClientManager() {
> }
> 
> public static Client getClient() {
> if (client == null) {
> synchronized (mutex) {
> settings = ImmutableSettings.settingsBuilder()
> .put(CONFIG_CLUSTER_NAME,
> 
> ```
> 
> CLUSTER\_NAME).build();
> 
> ```
> client = new TransportClient(settings);
> for (int i = 0; i < transportAddress.length - 1; i++) {
> ((TransportClient) client).addTransportAddress(new
> 
> ```
> 
> InetSocketTransportAddress(transportAddress[i], transportPort));  
> }  
> logger.info("Elastic search client initialized");  
> }  
> }  
> logger.info("Returning the existing client");  
> return client;  
> }  
> }
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/a25c2c6e-82d3-4d7b-a9f0-2717dbc59225%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a25c2c6e-82d3-4d7b-a9f0-2717dbc59225%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/a25c2c6e-82d3-4d7b-a9f0-2717dbc59225%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/a25c2c6e-82d3-4d7b-a9f0-2717dbc59225%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAHO4itwhO%3D3QvnZd1%2BgqdXmHS%3DokjBL0mUqQ3LA-aO9B-nT%3D9w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHO4itwhO%3D3QvnZd1%2BgqdXmHS%3DokjBL0mUqQ3LA-aO9B-nT%3D9w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:47am UTC](https://discuss.elastic.co/t/releasing-elasticsearch-connection/20990/3 "2017-07-06T00:47:00Z")

</div>


