How to know Transaction Is Successful

Based on .Net Framework, it is convient to use transaction with System.Transactions.TransactionScope class. If no error occurs in the transaction scope, the TransactionScope object will complet, otherwise rollback.

In some situations, we need execute some business logics after transaction complet successfully. Note that we don’t add some business logic code lines after complet() method. In another word, we must place these business logics out of the transaction scope. However, the transaction object had been disposed out of using statement. So, how to know the transaction is successful?

Transaction object exposes the event called TransactionCompleted and provides TransactionStatus which is enumeration type. TransactionStatus enumeration type includes four values: Aborted, Committed, Active and InDoubt. Then we can make a decision depending on the value of TransactionStatus which is belong to TransactionInformation object. TransactionInformation object is a property of Transaction instance.

The sample code is as below: 

using (TransactionScope ts = new TransactionScope())

{

    Transaction transaction = Transaction.Current;

    transaction.TransactionCompleted += pro.OnCompleted;

    pro.UpdateEmployee();

    pro.DeleteCustomer();

    ts.Complete();           

}

 

void OnCompleted(object sender, TransactionEventArgs e)

{

    Debug.Assert(sender.GetType() == typeof(Transaction));

    Debug.Assert(object.ReferenceEquals(sender,e.Transaction));

    Transaction transaction = e.Transaction;

    switch (transaction.TransactionInformation.Status)

    {

        case TransactionStatus.Aborted:

            m_TransactionCommitted = false;

            //Not to do something when failed

            break;

        case TransactionStatus.Committed:

            m_TransactionCommitted = true;

            //do something when success

            break;

        default:

            m_TransactionCommitted = false;

            break;

    }

}

Advertisements

TransactionScope Troubleshooting

We always use TransactionScope to handle the transaction based on .Net framework. Before use it, you must add the System.Transaction reference and using it. In my project, we use the LINQ to SQL as ORM framework to access the database. It provides the transaction handling by default that is SubmitChanges() method. If you invoke it, and some errors occur, the datas which were changed will rollback. Besides, we can use the System.Data.Common.DbTransaction to handle the transaction. We can create the instance of it through by invoking the BeginTransaction() method of the connection property of DataContext, then set it to the Transaction property of DataContext. The code sippet like this:

        LinqSampleDataContext context = new LinqSampleDataContext();

        System.Data.Common.DbTransaction trans = null;

        try

        {

            context.Connection.Open();

            trans = context.Connection.BeginTransaction();

            context.Transaction = trans;

 

            context.Employees.InsertOnSubmit(emp);

            context.SubmitChanges();

 

            trans.Commit();

        }

        catch (Exception ex)

        {

            if (trans != null)

            {

                trans.Rollback();

            }

        }

 

However, the ways LINQ provides are only used in the scope of DataContext, if you want to use a couple of DataContext to execute the different operation, you have to use TransactionScope, like this:

        using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))

        {

            try

            {

 

                for (int i = 0; i < nominees.Count; ++i)

                {

                    Backup newBackup = nominees[i];

                    Ticket ticket = tickets[i];

 

                    //update the information of ticket

                    //mainly add the information of employee;

                    ticket.EmployeeID = newBackup.EmployeeID;

                    ticket.HaveNominated = true;

                    ticket.IsConfirmedByManager = true;

                    ticket.Status = TicketStatus.Enroll.ToString();

 

                    ticketAccessor.Update(ticket);

                }

 

                //update the IsSubmit of backup;

                ChangeSubmitStatue(backup);

 

                //remove the record of nominee in backup table

                Delete(nominees);

            }

            catch (Exception ex)

            {

                ThrowHelper.ThrowBackupException(“Finalizing occurs an error. The transcation will be rollback.”);

                return false;

            }

 

            scope.Complete();

        }

 

Running the code, maybe you will meet an error which is an System.Data.SqlClient.SqlException, and the message of it is “MSDTC on server ‘{Server Name}’ is unavailable.” What happen? It is necessary to make sure whether the Distributed Transaction Coordinator Service is start or not. Please notice I mean you should check your database server instead of the one your software is running. You can open the Administrative Tools and Service, find the Distributed Transaction Coordinator Service and Start it. You’d better set it to be Automatic.

Let’s consider about this situation. The server your software is running is not same machine with your database server. Now running the code, the terrible expection will be thrown maybe this time. The type of it is System.Transactions.TransactionManagerCommunicationException and the message is “Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool.”

Please pay attention to the type of exception. It’s a communication error. Although you start the correct service in Database server, but the server your software is running can’t communicate with your database server when we use the distributed transaction. In fact, it’s a security issue. The solution to this problem is set the appropriate MSDTC Security Configuration option on Operation System such as Windows Server 2003 sp1 or Windows XP sp2.

Note: The machine you wanna set should be the one your software is going to run.

To access the MSDTC Security Configuration dialog box follow these steps:
1. Click Start, click Run, and type dcomcnfg to launch the Component Services Management console.
2. Click to expand Component Services, click to expand Computers, right click My Computer, and click Properties.
3. Click the MSDTC tab of the My Computer Properties dialog and click the Security Configuration button to display the Security Configuration dialog box.

What are the correct values? Microsoft give the recommended values refer to the table as below:

Configuration Option Default Value Recommended Value
Network DTC Access Disabled Enabled
Client and Administration    
Allow Remote Clients Disabled Disabled
Allow Remote Administration Disabled Disabled
Transaction Manager Communication    
Allow Inbound Disabled Enabled
Allow Outbound Disabled Enabled
Mutual Authentication Required Enabled Enabled if all remote machines are running Win2K3 SP1 or XP SP2 or higher, and are configured with “Mutual Authentication Required”.
Incoming Caller Authentication Required Disabled Enabled if running MSDTC on cluster.
No Authentication Required Disabled Enabled if remote machines are pre-Windows Server 2003 SP1 or pre- Windows XP SP2.
Enable TIP Disabled Enabled if running the BAM Portal.
Enable XA Transactions Disabled Enabled if communicating with an XA based transactional system such as when communicating with IBM WebSphere MQ using the MQSeries adapter.

You may refer to the figure that I post as below: securityconfig

In fact, it is default value on Windows Server 2003. However, the machine your software is running always is on Windows XP during Unit Test. If you don’t find the key to this problem, maybe you will be confused.