Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple SubmitChanges and transaction rollback using Linq To SQL

I'm using TransactionScope to submit data in Linq to SQL. My question is, if I use multiple SubmitChanges in the same scope, will all scope roll back in case of an error or just the changes made after the last SubmitChanges? For example:

using (TransactionScope trans = new TransactionScope())
{
    using (dbDataContext db = new dbDataContext())
    {
        try
        {
            //do some insert
            db.SubmitChanges();

            //do some updates
            db.SubmitChanges();

            trans.Complete();
        }
        catch(Exception ex){}
    }
}

If update SubmitChanges throws an exception, will the insert SubmitChanges roll back too?

like image 986
dstr Avatar asked Oct 28 '09 11:10

dstr


1 Answers

ALL changes will roll back, not just the one from the last SubmitChanges().

You will often need to use this pattern when you have data that depends on other data -- like if you need to create an object and get it's auto-generated ID in order to do something else (though L2SQL supports that with one SubmitChanges call in most scenarios by just creating the object, but I digress...).

like image 80
Dave Markle Avatar answered Nov 15 '22 09:11

Dave Markle