0

I have a web-service which gets called every few seconds, which does a bunch of things and then performs a bulk insert which currently takes half a second on average to call SaveChanges(). I'd like to save all the changes asynchronously because then I can call my web-service multiple times per second and just buffer any database changes to be saved by a quartz timer, which should lead to better performance.

I've observed that after a web-service function executes, if the "SaveChanges" was not called on the entity database model object, any pending database changes will be rolled back.

Is it possible for me to asynchronously save my changes to the database and how would I go about doing it?

JSideris
  • 5,101
  • 3
  • 32
  • 50
  • PS: I'm planning on using quartz.net to call the save changes on an asynchronous timer, but I'm open to suggestions. – JSideris Jan 22 '13 at 19:53

1 Answers1

0

In principle, you could use any of asynchronous approaches available in .NET for such purpose. If you could use .NET 4.5 then I'd suggest you to take a look at async/await methods, because they're quite easy to use. Entity framework doesn't support async/await from the box, but you could add your simple wrappers by using TaskCompletionSource class.

Haspemulator
  • 11,050
  • 9
  • 49
  • 76
  • If I use any of these methods, will my entity changes get reverted as soon as the web-service thread completes? – JSideris Jan 22 '13 at 20:35
  • If you're using *await* keyword in ASP.NET, your thread doesn't actually completes until all awaited Tasks are completed. So you shouldn't have such a problem at all. Have a look at this accepted answer: http://stackoverflow.com/questions/11777978/asp-net-async-await-part-2 – Haspemulator Jan 22 '13 at 20:52
  • Hmm this could be a problem - the web-service calls seem to time out after a short time. Checking the logs of my application, I get a lot of exceptions stating "Thread was being aborted", which is my motivation for doing the database transactions asynchronously. – JSideris Jan 22 '13 at 21:21
  • With async/await pattern this should not be the case, as the thread which awaiting the task is reused by the ASP runtime. Please check the link I gave! – Haspemulator Jan 22 '13 at 22:11