Hello Shawn,

I did exactly as you told, created a public static synchronized method. The
problem still exists.

Maybe returning the client object if it is not null is causing
" java.lang.IllegalStateException: Connection pool shut down" error. It
does run fine for just one time.

pseudo code:
private static SolrClient client = null;
public static synchronized SolrClient getSolrClient(collectionName) {
        if (client != null) {
            return client;
        }
        if (Boolean.parseBoolean(isSolrCloudEnabled)) {
            client = new
CloudSolrClient.Builder().withZkHost(ZKHOSTS).build();
            ((CloudSolrClient)client).setDefaultCollection(collectionName);
        } else {
            client = new HttpSolrClient.Builder(getSolrHost(delegator,
collectionName)).build();
        }
        return client;
    }

If I remove this code and simply create a new client object still keeping
the method synchronized, everything seems to be running fine.

Am I missing something?

On Tue, Jul 3, 2018 at 6:04 AM Shawn Heisey <apa...@elyograg.org> wrote:

> On 7/2/2018 7:35 AM, Ritesh Kumar wrote:
> > I have got a static method which returns CloudSolrClient object if Solr
> is
> > running in Cloud mode and HttpSolrClient object otherwise.
>
> Declare that method as synchronized, so that multiple usages do not step
> on each other's toes.  This will also eliminate object visibility issues
> in multi-threaded code.  The modifiers for the method will probably end
> up being "public static synchronized".
>
> In the class where that method lives, create a "private static
> SolrClient" field and set it to null.  In the method, if the class-level
> field is not null, return it.  If it is null, create the HttpSolrClient
> or CloudSolrClient object just as you do now, set the default collection
> if that's required, then assign that client object to the class-level
> field and return it.
>
> Remove any client.close() calls that you have currently.  You can close
> the client at application shutdown, but this is not actually necessary
> if application shutdown also halts the JVM.
>
> You could also use the singleton paradigm that Erick mentioned, but
> since you already have code to obtain a client object, it's probably
> more straightforward to just modify that code as I have described, and
> don't close the client after you use it.
>
> Thanks,
> Shawn
>
>

Reply via email to