This is a migrated thread and some comments may be shown as answers.

Different errors each run

2 Answers 63 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
DuelingCats
Top achievements
Rank 2
DuelingCats asked on 31 Jul 2012, 02:23 PM
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.

EDIT: 
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 Answers, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 03 Aug 2012, 06:39 AM
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(){...};
       entities.Add(logMsg);
       entities.SaveChanges();
  }
 
}

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,
Zoran
the Telerik team
OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
0
DuelingCats
Top achievements
Rank 2
answered on 07 Aug 2012, 12:10 PM
Thanks for the reply. I didn't realize that saving was occurring in a asynchronous manner.
Tags
Getting Started
Asked by
DuelingCats
Top achievements
Rank 2
Answers by
Zoran
Telerik team
DuelingCats
Top achievements
Rank 2
Share this question
or