Juhan
Juhan

Reputation: 1291

Best Practice While creating objects of Solr HttpSolrServer object for Web projects

I am using Solr-5.0.0 in a web project. i am just creating HttpSolrServer object in my constructor as give below

static HttpSolrClient solr;

public SolrTestDAO() {
    if (solr == null) {
        solr = new ttpSolrClient("http://localhost:8983/solr/testDB");
    }
}

Is there any problem to use my HttpSolrServer object as static , because its a web project. If its not the correct way can anyone suggest me right way so my application performance will be high and memory usage will be too low.

Upvotes: 2

Views: 672

Answers (1)

Josh Edwards
Josh Edwards

Reputation: 908

Nope. In fact, it's the expected way to use HttpSolrServer. Here's a snippet of a comment from the HttpSolrServer's code:

HttpSolrServer is thread-safe and if you are using the following constructor, you MUST re-use the same instance for all requests. If instances are created on the fly, it can cause a connection leak. The recommended practice is to keep a static instance of HttpSolrServer per solr server url and share it for all requests.

See the SolrJ Wiki Page. There's also an interesting blog post about what happens if you don't make it a static here.

Upvotes: 1

Related Questions