Begintransaction vs Transactionscope

Asked

Viewed 382 times

2

I was analyzing that answer and the doubt arose:

What’s the difference between BeginTransaction and the TransactionScope? Are there specific situations for each one? What are their advantages and disadvantages compared to each other?

  • 1

    @Jedais, I can’t answer you now, but the difference between the two, as well as the pros and cons are explained in MSDN

1 answer

1

Jedais, if using Entity Framework version 5 or earlier, you will need to use TransactionScope if you want to use a single transaction for more than one transaction.

From the EF6, the same went to incoporou the methods BeginTransaction and UseTransaction to the property Database, this way it is possible to reuse the same transaction in more than one operation.

Another change in the EF6, is that it has come to accept an already open connection, so we can call the sqlConnection.BeginTransaction and then pass this connection as argument on the initialization of DbContext.

Another point is about Assicrone operations, if your targetFramework will be a version prior to 4.5.1, then the TransactionScope will not work, so your only option is BeginTransaction

For most scenarios, the BeginTransaction is more than enough, and there is no need for a TransactionScope, but it is easier to work with the TransactionScope in two scenarios.

Distributed Transactions

If your transaction involves more than one connection, the TransactionScope will convert your local transaction into an automatically distributed transaction.

Recources Volateis

BeginTransaction maintains to your transaction only at the level of Bando de Dados, if you need some object in memory also suffer a Rollback/Commit, this object can implement the interface IEnlistmentNotification and be added to transaction.

follows an example taken from MSDN

static void Main(string[] args)
{
    try
    {
        using (TransactionScope scope = new TransactionScope())
        {

            //Create an enlistment object
            myEnlistmentClass myElistment = new myEnlistmentClass();

            //Enlist on the current transaction with the enlistment object
            Transaction.Current.EnlistVolatile(myElistment, EnlistmentOptions.None);

            //Perform transactional work here.

            //Call complete on the TransactionScope based on console input
            ConsoleKeyInfo c;
            while(true)
            {
                Console.Write("Complete the transaction scope? [Y|N] ");
                c = Console.ReadKey();
                Console.WriteLine();

                if ((c.KeyChar == 'Y') || (c.KeyChar == 'y'))
                {
                    scope.Complete();
                    break;
                }
                else if ((c.KeyChar == 'N') || (c.KeyChar == 'n'))
                {
                    break;
                }
            }
        }
    }
    catch (System.Transactions.TransactionException ex)
    {
        Console.WriteLine(ex);
    }
    catch
    {
        Console.WriteLine("Cannot complete transaction");
        throw;
    }
}

class myEnlistmentClass : IEnlistmentNotification
{
    public void Prepare(PreparingEnlistment preparingEnlistment)
    {
        Console.WriteLine("Prepare notification received");

        //Perform transactional work

        //If work finished correctly, reply prepared
        preparingEnlistment.Prepared();

        // otherwise, do a ForceRollback
        preparingEnlistment.ForceRollback();
    }

    public void Commit(Enlistment enlistment)
    {
        Console.WriteLine("Commit notification received");

        //Do any work necessary when commit notification is received

        //Declare done on the enlistment
        enlistment.Done();
    }

    public void Rollback(Enlistment enlistment)
    {
        Console.WriteLine("Rollback notification received");

        //Do any work necessary when rollback notification is received

        //Declare done on the enlistment
        enlistment.Done();
    }

    public void InDoubt(Enlistment enlistment)
    {
        Console.WriteLine("In doubt notification received");

        //Do any work necessary when indout notification is received

        //Declare done on the enlistment
        enlistment.Done();
    }
}
  • Excellent placement between the two EF versions!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.