0

I have a WP8 app that has multiple (at times, up to 40) threads that have to get the data from a webservice and then commit to a localdb.

I have implemented an AutoResetEvent-based pattern where each Repository method looks somewhat like this:

 public class MySuperAppRepository
  {
   public static AutoResetEvent DataAccess = new AutoResetEvent(true);

   public MyFancyObject CreateMyFancyObject(string path, int something)
   {
    DataAccess.WaitOne();
    try
    {
      using (var dbContext = new   MySuperAppDataContext(MySuperAppDataContext.DbConnectionString))
      {
        var mfo = new MyFancyObject();
        dbContext.MyFancyObjects.InsertOnSubmit(mfo);
        mfo.Path = path;
        mfo.Something = something;
        dbContext.SubmitChanges();
        return mfo;
      }
    }
    finally
    {
      DataAccess.Set();
    }
  }
}

This is all nice and clean, but as soon as i get multiple threads (as mentioned above), the performance is PATHETIC. i can get lots of requests come down and then they're all waiting for db to be free.

Is there a better alternative? Would using lock(object) improve the performance?

zaitsman
  • 8,984
  • 6
  • 47
  • 79
  • Collect the data to a list object, and use InsertAllOnSubmit – ErikEJ Apr 01 '14 at 12:27
  • @ErikEJ So the data is interdependant (e.g. parent objects, child objects etc) and the FK's are based of Ids in the local db(im using Guids). Therefore until i insert parents, i can't really insert children. Further to that, that list would be enormous and i will most like run out of memory. – zaitsman Apr 01 '14 at 12:54
  • Assign the GUIDs in code, and submit in batches – ErikEJ Apr 01 '14 at 13:19
  • @ErikEJ, if i submit in batches, the results would be about the same - new batches will wait for the previous batches. I might win a little in the number of transactions, but not a dramatic amount of time. – zaitsman Apr 01 '14 at 13:25

1 Answers1

0

Can you try not creating a new DataContext on every data operation.

Also try out some of the best practices mentioned here.

in particular :

Enabling fast updates with a version column One of the easiest ways to optimize the performance of an update operation on a table is to add a version column. This optimization is specific to LINQ to SQL for Windows Phone. For example, in an entity, add the following code.

    [Column(IsVersion=true)]
    private Binary _version;
Abhilash
  • 367
  • 3
  • 16
  • its a good idea about DataContext, i will try that. It came from the linq2entities implementation that i did in asp.net, so it may be a bottleneck. And i do already have version columns in each table. – zaitsman Apr 02 '14 at 09:20
  • this did improve the performance somewhat, but really marginally. Also, as mentioned in the link you referenced, not recreating the context causes memory issues (so after my operation i have to force it disposed and recreated). I will wait a bit for any other suggestions before acceptin. Quite sad, though, that there wasn't anything immediately wrong with what i was doing. – zaitsman Apr 03 '14 at 12:57
  • I'm accepting this as nobody suggested anything better. The performance is still pretty pathetic, but having combined everything together and using the suggested approach to not recreate dbContext for each call i was able to gain an improvement enough to get the solution to be just usable... – zaitsman Apr 08 '14 at 13:38
  • Probably the performance bottleneck lies elsewhere cant really speculate much without looking at some code, the pattern that you have implemented is pretty much standard usage, haven't faced a lot of issues with it. – Abhilash Apr 08 '14 at 14:45