Different errors each run

Thread is closed for posting
3 posts, 0 answers
  1. DuelingCats
    DuelingCats avatar
    54 posts
    Member since:
    Jul 2012

    Posted 31 Jul 2012 Link to this post

    I just started using OpenAccess today after battling with Entity Framework in hopes I could resolve this problem. Alas, it still follows me. I have a Scheduler class that holds several Task objects. These Task objects each old a BackgroundWorker object that does long running processes. Once I get the event from the BackgroundWorker that its job is complete, I raise an event in the Task object. 

    The scheduler uses the same event handler for every one of these events called NewTaskTaskCompleted (Typo in my method name, will fix later). I keep getting errors in this handler, though. What makes it difficult is that I am getting a different error in different places. Maybe someone can help me out.

    I have attached a screenshot of the errors of 3 different runs.

    I found that the model generated from the database had some discrepancies and I now am saving successfully if I had one task running. However, if I have multiple tasks running and they all finish at the same time, My NewTaskTaskCompleted function is getting called in quick succession and is causing an
     InvalidOperationException - Not allowed because Commit / Rollback is in progress

    I am guessing this is because I am calling SaveChanges too fast? Is SaveChanges not a blocking call preventing further execution until the commit is complete? 
  2. Zoran
    Zoran avatar
    534 posts

    Posted 03 Aug 2012 Link to this post

    Hello Justin,

     You are calling this method in an asynchronous manner e.g. the first call of it has not ended before the second one is called. The OpenAccessContext as well as Entity Framework's ObjectContext are not synchronized (thread safe) objects which is shown in your case, when called asynchronously their behavior is really undefined. 

    What you should do in a callback method  handling the background worker finish call, is to create a context with a little scope of usage and not use the one that you use elsewhere in your synchronous operations. So if you consider a code like the following, you should not encounter those problems:

    private void NewTaskCompleted(object sender, TaskCompletedEvent e)
      using(EntitiesModel entities = new EntitiesModel())
         TASK scheduledTask = entities.Tasks.First(...);
          scheduledTask.NextRun = e.NextRun;
          TASK_LOG logMsg = new TASK_LOG(){...};

    Basically, the main pattern in context management for all ORM's is a context-per-thread approach, so you should use a context for each thread as you can see in this article, or just create a small-scoped context if applicable as shown in my code-snippet above.  Regards,
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
  3. DuelingCats
    DuelingCats avatar
    54 posts
    Member since:
    Jul 2012

    Posted 07 Aug 2012 Link to this post

    Thanks for the reply. I didn't realize that saving was occurring in a asynchronous manner.
Back to Top