Distributed transactions

The example in this topic uses pseudo-code to demonstrate how to use distributed transactions.


using System.EnterpriseServices;
using IBM.Data.Informix;

[assembly: AssemblyKeyFile("test.snk")]

    public static void Main()
    {
        
            /* The 'using' construct below results in a call to Dispose on
            exiting the curly braces. It is important to dispose of COM+
            objects as soon as possible, so that COM+ services such as
            Object Pooling work properly */

        using (TwoPhaseTxn txn = new TwoPhaseTxn)
        {
            txn.TestAutoComplete_Exception();
        }

        using (TwoPhaseTxn txn = new TwoPhaseTxn)
        {
            txn.TestAutoComplete_TransactionVote();
        }
        
    }

//Transaction attributes specify the type of transaction requested

[Transaction(TransactionOption.RequiresNew)]
 public class TwoPhaseTxn : ServicedComponent
 {
     [AutoComplete]
     public void TestAutoComplete_Exception()
     {
          IfxConnection ifxConn1 = new IfxConnection(db=db1;server=srv1;
             enlist=true;);
          IfxConnection ifxConn2 = new IfxConnection(db=db2;server=srv2;
             enlist=true;);

          try
          {
               // db operation on ifxConn1
          }
          catch
          {
               // throw exception
          } 

          try
          {
               // db operation on ifxConn2
          }
          catch
          {
               // throw exception
          }
     }
[AutoComplete]
     public void TestAutoComplete_TransactionVote()
     {
          IfxConnection ifxConn1 = new IfxConnection(db=db1;server=srv1;
             enlist=true;);
          IfxConnection ifxConn2 = new IfxConnection(db=db2;server=srv2;
             enlist=true;);

          try
          {
             // db operation on ifxConn1
          }
          catch
          {
             // In case of any failure, flag abort
             ContextUtil.MyTransactionVote = TransactionVote.Abort
          } 

          try
          {
             // db operation on ifxConn2
          }
          catch
          {
             // In case of any failure, flag abort
             ContextUtil.MyTransactionVote = TransactionVote.Abort
          }
     }