title: New transaction is not allowed because there are other threads running in the session
body: <p>Getting "new transaction is not allowed because there are other threads running in the session".</p>
  
  <p>It has nothing to do with foreach loops or anything people usually have problems with in conjunction with this message.</p>
  
  <p>I using a EF4 with a repositoy pattern and common context open throughout the request. Something happens, can't determine exactly what, and I get this message as soon as I try to savechanges with the context, across requests, and it only dissappears once I recycle the app pool.</p>
  
  <p>Am I closing the connection? How can I tell? Am I using a fresh context for every request? Yes.</p>
  
  <p>What's going on? Is there a work-around?</p>
  
  <p>Edit: (context factory)</p>
  
  <pre><code>    private static Dictionary&lt;string, CoinEntities&gt; _instances;
  
      public static CoinEntities DefaultInstance
      {
          get
          {
              if (HttpContext.Current == null)
              { //todo: mock instead. testing.
                  if (!Instances.ContainsKey("_TEST"))
                      Instances["_TEST"] = new CoinEntities();
                  return Instances["_TEST"];
              }
  
              if (!Instances.ContainsKey("_DEFAULT"))
                  Instances["_DEFAULT"] = new CoinEntities();
  
              return Instances["_DEFAULT"];
          }
      }
  </code></pre>