# Elasticsearch - transport client singleton

**URL:** <https://discuss.elastic.co/t/elasticsearch-transport-client-singleton/22719>\
**Category:** Elasticsearch\
**Created:** [March 17, 2015, 6:23pm UTC](https://discuss.elastic.co/t/elasticsearch-transport-client-singleton/22719 "2015-03-17T18:23:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11181](https://avatars.discourse-cdn.com/v4/letter/1/34f0e0/32.png) [@11181](https://discuss.elastic.co/u/11181)\
**Post date:** [March 17, 2015, 6:23pm UTC](https://discuss.elastic.co/t/elasticsearch-transport-client-singleton/22719/1 "2015-03-17T18:23:25Z")

</div>

I am newbie in elastic and I don't understand how should I work with transport client connections. Should I use singleton for Client, something like

class ElasticClientManager {  
private static Client client;

public static Client getClient(){  
if (client==null) {  
Settings settings = ImmutableSettings.settingsBuilder()  
.put("cluster.name", "elasticsearch")  
.put("client.transport.sniff", true).build();

```
   client = new TransportClient(settings)
        .addTransportAddress(new InetSocketTransportAddress("localhost",9300));
}

```

return client;  
}  
}

By other words - I create one client and keep the reference in it in singleton. Every time I need to query elastic I do

Client client = ElasticClientManager.getClient();  
GetResponse getResponse = client.prepareGet(....).execute().actionGet();

Is such approach right?

--  
Александр Свиридов

--  
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/1426616605.710285922%40f217.i.mail.ru](https://groups.google.com/d/msgid/elasticsearch/1426616605.710285922%40f217.i.mail.ru).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 17, 2015, 10:17pm UTC](https://discuss.elastic.co/t/elasticsearch-transport-client-singleton/22719/2 "2015-03-17T22:17:56Z")

</div>

Yes!

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

> Le 17 mars 2015 à 11:23, Александр Свиридов [ooo\_saturn7@mail.ru](mailto:ooo_saturn7@mail.ru) a écrit :
> 
> I am newbie in elastic and I don't understand how should I work with transport client connections. Should I use singleton for Client, something like
> 
> class ElasticClientManager {  
> private static Client client;
> 
> public static Client getClient(){  
> if (client==null) {  
> Settings settings = ImmutableSettings.settingsBuilder()  
> .put("cluster.name", "elasticsearch")  
> .put("client.transport.sniff", true).build();
> 
> ```
> client = new TransportClient(settings)
> .addTransportAddress(new InetSocketTransportAddress("localhost",9300));
> }
> 
> ```
> 
> return client;  
> }  
> }
> 
> By other words - I create one client and keep the reference in it in singleton. Every time I need to query elastic I do
> 
> Client client = ElasticClientManager.getClient();  
> GetResponse getResponse = client.prepareGet(....).execute().actionGet();
> 
> Is such approach right?
> 
> ## -- Александр Свиридов
> 
> 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/1426616605.710285922%40f217.i.mail.ru](https://groups.google.com/d/msgid/elasticsearch/1426616605.710285922%40f217.i.mail.ru).  
> 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/6268C917-24DD-4081-A012-9BE565539438%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/6268C917-24DD-4081-A012-9BE565539438%40pilato.fr).  
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:26am UTC](https://discuss.elastic.co/t/elasticsearch-transport-client-singleton/22719/3 "2017-07-06T00:26:06Z")

</div>


