raytiley
raytiley

Reputation: 684

Entity Framework - Effect of MultipleActiveResultSets on Caching

So I have a Class that looks something like the following. There is a thread that does some work using an Entity Framework Code First DbContext.

The problem I'm having is that it seems like m_DB context is caching data even though it should be disposed and recreated for every processing loop.

What I've seen is that some data in a relationship isn't present in the models loaded. If I kill and restart the process suddenly the data is found just like it should.

The only thing I can think of is this app is using the MultipleActiveResultSets=true in the database connection string, but I can't find anything stating clearly that this would cause the behavior I'm seeing.

Any insight would be appreciated.


public class ProcessingService
{

  private MyContext m_DB = null
  private bool m_Run = true;

  private void ThreadLoop()
  {
    while(m_Run)
    {
      try
      {
        if(m_DB == null)
          m_DB = new MyContext();
      }
      catch(Exception ex)
      {
        //Log Error
      }
      finally
      {
        if(m_DB != null)
        {
          m_DB.Dispose();
          m_DB = null;
        }
      }
    }
  }

  private void ProcessingStepOne()
  {
    // Do some work with m_DB
  }

  private void ProcessingStepTwo()
  {
    // Do some work with m_DB
  }
}

Upvotes: 9

Views: 14159

Answers (2)

Ahsan
Ahsan

Reputation: 2518

Multiple Active Result Sets or MARS is a feature of SQL 2005/2008 and ADO.NET where one connection can be used by multiple active result sets (Just as the name implies). try switching this off on the connection string and observe the behaviour of the app, i am guessing that this could be the likely cause of your problem. read the following MSDN link for more on MARS

MSDN - Multiple Active Result Sets

Edit: Try:

var results = context.SomeEntitiy.AsNoTracking() where this = that select s;

AsNoTracking() switches off internal change tracking of entities and it should also force Entity Framework to reload entities every time.

Whatever said and done you will require some amount of re-factoring since there's obviously a design flaw in your code.

Upvotes: 5

raytiley
raytiley

Reputation: 684

I hate answering my own question, especially when I don't have a good explanation of why it fixes the problem.

I ended up removing MARS and it did resolve my issue. The best explanation I have is this:

Always read to the end of results for procedural requests regardless of whether they return results or not, and for batches that return multiple results. (http://technet.microsoft.com/en-us/library/ms131686.aspx)

My application doesn't always read through all the results returned, so its my theory that this some how caused data to get cached and reused the new DbContext.

Upvotes: 2

Related Questions