# Client connection closed when using factory pattern

**URL:** <https://discuss.elastic.co/t/client-connection-closed-when-using-factory-pattern/9389>\
**Category:** Elasticsearch\
**Created:** [October 17, 2012, 12:44pm UTC](https://discuss.elastic.co/t/client-connection-closed-when-using-factory-pattern/9389 "2012-10-17T12:44:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hans1](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@Hans1](https://discuss.elastic.co/u/Hans1)\
**Post date:** [October 17, 2012, 12:44pm UTC](https://discuss.elastic.co/t/client-connection-closed-when-using-factory-pattern/9389/1 "2012-10-17T12:44:44Z")

</div>

When getting the ES client by calling static method Es.getClient() - which  
in turn calls static method Es.createClient() to create the client and  
populates the corresponding static field if not existing - the  
nodesService.closed property of the returned client is true.

When calling static method Es.createClient() directly everything works just  
fine.

Has anyone any idea about why this is happening?

Using 0.18.7.

--

---

<div class="post-metadata">

**Author:** ![Hans1](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@Hans1](https://discuss.elastic.co/u/Hans1)\
**Post date:** [October 17, 2012, 12:55pm UTC](https://discuss.elastic.co/t/client-connection-closed-when-using-factory-pattern/9389/2 "2012-10-17T12:55:28Z")

</div>

Seems like JVM by default persists static members between requests - can  
anyone confirm this? Quite surprising from my PHP perspective ...

Is there a legal way to check the private nodesService.closed property  
w/out using reflection?

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 17, 2012, 9:44pm UTC](https://discuss.elastic.co/t/client-connection-closed-when-using-factory-pattern/9389/3 "2012-10-17T21:44:00Z")

</div>

Which library are you using? To my knowledge, the official Java library  
does not have getClient() or createClient() calls

It would be up to your application to control how a client is  
created/accessed/persisted. IMHO, an application should maintain a  
singleton instance to an Elasticsearch client. And yes, a static member is  
persisted for the lifetime of the JVM. PHP is primarily a web-only  
language, so many of its concepts relate to the HTTP request lifecycle. Not  
so with the JVM, which is far more than HTTP.

Ivan

On Wed, Oct 17, 2012 at 5:55 AM, Hans [hansmandal@googlemail.com](mailto:hansmandal@googlemail.com) wrote:

> Seems like JVM by default persists static members between requests - can  
> anyone confirm this? Quite surprising from my PHP perspective ...
> 
> Is there a legal way to check the private nodesService.closed property  
> w/out using reflection?
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Hans1](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@Hans1](https://discuss.elastic.co/u/Hans1)\
**Post date:** [October 30, 2012, 11:40am UTC](https://discuss.elastic.co/t/client-connection-closed-when-using-factory-pattern/9389/4 "2012-10-30T11:40:08Z")

</div>

Thx for your answer! After being hit by the insight that JVM persists  
static members between requests I could easily work around - you just have  
to know (as always) ... 🙂

--

---

<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, 3:06am UTC](https://discuss.elastic.co/t/client-connection-closed-when-using-factory-pattern/9389/5 "2017-07-06T03:06:48Z")

</div>

Closed on July 6, 2017, 3:06am UTC


