[ACCEPTED]-How to use transactions with a datacontext-datacontext

Accepted answer
Score: 19

I use them in testing all the time :)

try
{
  dc.Connection.Open();
  dc.Transaction = dc.Connection.BeginTransaction();

  dc.SubmitChanges();
}
finally
{
  dc.Transaction.Rollback();
}

UPDATE

This 2 will ALWAYS rollback after the fact. I use 1 this in testing.

Score: 15

A DataContext will pick up an ambient transaction 13 by default, so it is just a matter of ensuring 12 there is a Transaction in scope. The details 11 become the main issue:

  • What options do you need (e.g. isolation level)
  • Do you want a new transaction or reuse an existing transaction (e.g. an audit/logging operation might require a new transaction so it can be committed even if the overall business operation fails and thus the outer transaction is rolled back).

This is simplified 10 some prototype code, the real code uses 9 helpers to create the transactions with 8 policy driven options (one of the purposes 7 of the prototype was to examine the impact 6 of these options).

using (var trans = new TransactionScope(
                           TransactionScopeOption.Required,
                           new TransactionOptions {
                               IsolationLevel = IsolationLevel.ReadCommitted
                           },
                           EnterpriseServicesInteropOption.Automatic)) {
    // Perform operations using your DC, including submitting changes

    if (allOK) {
        trans.Complete();
    }
}

If Complete() is not called 5 then the transaction will be rolled back. If 4 there is a containing transaction scope 3 then both the inner and outer transactions 2 need to Complete for the changes on the 1 database to be committed.

Score: 14

It's not as simple as the TransactionScope 7 method but, as I understand it, this is 6 the "correct" way to do it for LINQ-to-SQL. It 5 doesn't require any reference to System.Transactions.

dataContext.Connection.Open();
using (dataContext.Transaction = dataContext.Connection.BeginTransaction())
{
    dataContext.SubmitChanges();

    if (allOK)
    {
        dataContext.Transaction.Commit();
    }
    else
    {
        dataContext.Transaction.RollBack();
    }
}

Of 4 course, the RollBack is only required if 3 you intend to do further data operations 2 within the using, otherwise changes will 1 be automatically discarded.

Score: 10

Something like this, probably:

try
{
    using (TransactionScope scope = new TransactionScope())
    {
        //Do some stuff

        //Submit changes, use ConflictMode to specify what to do
        context.SubmitChanges(ConflictMode.ContinueOnConflict);

        scope.Complete();
    }
}
catch (ChangeConflictException cce)
{
        //Exception, as the scope was not completed it will rollback
}

0

Score: 0

Is something like this:

using (YourDatacontext m_DB = new YourDatacontext())
using (TransactionScope tran = new TransactionScope())
{
   try
   {
      //make here the changes
      m_DB.SubmitChanges();
      tran.Complete();
   }
   catch (Exception ex)
   {
       Transaction.Current.Rollback();
   }
}

0

More Related questions