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