none
Call SubmitChanges Async RRS feed

  • Question

  • Is it possible to call SubmitChanges in an async manner?

    I am importing 1000s of objects into a datacontext and want to batch up the inserts so that whilst it is inserting data, I can be making up some more.

    I though that I could call the SubmitChanges() method in a BackgroudWorker and wait to call it again in the BGWorker until the previous one had finished but I wanted to check that this wont have any long term repercussions.


    Regards,

    Richard
    Monday, July 14, 2008 10:42 AM

Answers

  • DataContext is not thread-safe. If you can find a way to insert the 1000s of objects as rows into the underlying SQL table, you might want to look into writing a stored procedure that does the same thing.

     

    Thanks,

    --Samir

     

    Monday, July 14, 2008 6:54 PM

All replies

  • DataContext is not thread-safe. If you can find a way to insert the 1000s of objects as rows into the underlying SQL table, you might want to look into writing a stored procedure that does the same thing.

     

    Thanks,

    --Samir

     

    Monday, July 14, 2008 6:54 PM
  • Since as said that dc is not thread safe, this also implies the fact that if you are going to handcraft it you need to deal with db level locks and app level concurrency issues on your own.

     

    If you are not going to divide the objects into chunks, this and thread safety at all may not be an issue for you.

     

    Alternatively, instead of calling submitchanges, when you are about to call it by calling context.GetCommand() you can get the SqlCommand to be executed and use this in a plain ADO.NET way to make your calls multithreaded.

     

    Hope it makes sense.

     

    Sidar

    Tuesday, July 15, 2008 2:57 AM
  • Thanks for that little nugget of information.  After doing some further research into this area, it would appear that the intention is that you would use a new DataContext for each operation or for each object (or at least thats my understanding anyway)!

    To get round this, my Singleton that contains my static reference to the DataContext was returning the reference to the object.  Now it returns a new instance of the DataContext and the user disposes of it later so that things aren't attached to it any more.

    This gets round the thread safety problem in my case but there are a number of other ways round this issue.

    I got my further info from this link.  Have a read through the chat at the bottom too.

    Thanks,

    Rich
    Wednesday, July 16, 2008 8:40 AM