Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of SolrNet connection

Tags:

c#

solrnet

Why is the container of the SolrNet connections kept static? This is a very big fault, as when, in our application, we send an asynchronous request to our application, SolrNet behaves abnormally. How I can avoid this issue in SolrNet?

class P
{
    static void M(string[] a)
    { 
        Thread t = new Thread(delegate()
        {
            f1();
        });
        Thread t1 = new Thread(delegate()
        {
            f2();
        });

        t.Start();
        t1.Start();
        t.Join();
        t1.Join();
    }

    static void f1()
    {
        Startup.Init<Doc>(new SolrNet.Impl.SolrPostConnection("http://localhost:8983/solr3/"));
        ISolrOperations<Doc> solrOperations2 = ServiceLocator.Current.GetInstance<ISolrOperations<Document>>();
    }

    static void f2()
    {
        Startup.Init<Doc>(new SolrNet.Impl.SolrPostConnection("http://localhost:8983/solr1/"));
        ISolrOperations<Doc> solrOperations2 = ServiceLocator.Current.GetInstance<ISolrOperations<Document>>();
    }
}
like image 609
Ahsan Iqbal Avatar asked Jul 26 '11 13:07

Ahsan Iqbal


1 Answers

  1. As explained in the wiki, the built-in container (Startup) is currently limited to access multiple cores/instances with different mapped types. If you want more flexibility about this, either switch to Windsor / StructureMap / Autofac, or help implement this feature.

  2. Registrations in the built-in container may not be thread-safe as you have discovered, but you gain nothing by registering / initializing SolrNet in different threads. Just move all initialization to a single thread, the actual heavy work is performed when you do solr.Query(...) or solr.Add(...) which is thread-safe.

like image 148
Mauricio Scheffer Avatar answered Oct 11 '22 16:10

Mauricio Scheffer