5

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>>();
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ahsan Iqbal
  • 1,422
  • 5
  • 20
  • 39
  • what's the error or abnormal behavior you see? – Mauricio Scheffer Jul 26 '11 at 13:31
  • In my application, I have multiple solr indexes let say 5, and I have 5 separate classes that represents one solr document. now abnormal behavior is when In one particular case I send asynchronous request to application where it gives error key already exist in container. – Ahsan Iqbal Jul 26 '11 at 14:18
  • can you post a failing test, or at least the full stack trace? – Mauricio Scheffer Jul 26 '11 at 14:25
  • at SolrNet.Utils.Container.Register(String key, Type serviceType, Converter`2 factory) in D:\solrNet\SolrNet\Utils\Container.cs:line 83 at SolrNet.Utils.Container.Register[T](String key, Converter`2 factory) in D:\solrNet\SolrNet\Utils\Container.cs:line 71 at SolrNet.Startup.Init[T](ISolrConnection connection) in D:\solrNet\SolrNet\Startup.cs:line 98 at EntryPoint.Program.funa() in D:\solrNet\EntryPoint\Program.cs:line 51 at EntryPoint.Program.
    b__1() in D:\solrNet\EntryPoint\Program.cs:line 31 at System.Threading.ExecutionContext.Run
    – Ahsan Iqbal Jul 26 '11 at 14:35
  • can you post your Program.cs or at least the offending part? – Mauricio Scheffer Jul 26 '11 at 14:37
  • or more specifically, where are you calling Startup.Init are where are you calling the method that calls Startup.Init? – Mauricio Scheffer Jul 26 '11 at 15:01
  • This was just for reference! as you can think of it if container is static that is being shared at appDomain level then let say two asynchronous requests came and both can assume that connection is not initiated yet and both can go initializing connection in parallel there u can find the problem – Ahsan Iqbal Jul 26 '11 at 15:09
  • @MauricioScheffer let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1845/discussion-between-ahsan-iqbal-and-mauricio-scheffer) – Ahsan Iqbal Jul 26 '11 at 15:10
  • Sorry, I really don't have the time to chat. Please post a failing test or the offending code, otherwise I can't help you. I think you're using SolrNet incorrectly, but I can't say for sure until I see some code. – Mauricio Scheffer Jul 26 '11 at 15:15
  • 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(new SolrNet.Impl.SolrPostConnection("http://localhost:8983/solr3/")); ISolrOperations solrOperations2 = ServiceLocator.Current.GetInstance>(); } static void f2() { Startup.Init(new SolrNet.Impl.SolrPostConnection("http://localhost:8983/solr1/")); ISolrOperations solrOperations2 = ServiceLocator.Current.GetInstance>(); } } – Ahsan Iqbal Jul 26 '11 at 15:55
  • Please post the code in the question, not as a comment, so it can be properly formatted. – Mauricio Scheffer Jul 26 '11 at 16:36

1 Answers1

5
  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.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275