BYOT (Bring Your Own Transaction)

The COM+ BYOT feature allows COM+ components to set a preexisting Microsoft Distributed Transaction Coordinator (DTC) or Transaction Internet Protocol (TIP) transaction as the transaction property of a new component's context. This allows COM+ components to be associated with transactions whose lifetimes are controlled by a transaction processing monitor, object transaction service, or database management system. BYOT is also useful in integrating with transactions coordinated by TIP.

Note   Automatic transactions, rather than BYOT transactions, are the preferred programming model for writing business components. BYOT transactions must be used with caution. In certain situations, they can result in a transaction spanning multiple synchronization domains (that is, allowing parallelism with a transaction).

The BYOT class exposes two methods: CreateWithTransaction and CreateWithTipTransaction. The following example shows how to create an object that uses the transaction from another object.

BYOT Demonstration

Imports System
Imports System.Reflection
Imports System.EnterpriseServices

<assembly: AssemblyKeyFile("BYOTDemo.snk")>

Public Class Base 
      Inherits ServicedComponent
      Public Readonly Property Transaction() as Object
        Get
            Return ContextUtil.Transaction
        End Get
      End Property
End Class

<Transaction()> Public Class CTransaction1 
Inherits Base
' Insert your transaction logic here.
End Class

<Transaction()> Public Class CTransaction2 
Inherits Base
' Insert your transaction logic here.
End Class

Class BYOTDemonstration
      Public Shared Sub Main()
            ' Create a transactional object, and then get its
            ' transaction.
            Dim tx1 as New CTransaction1
            Console.WriteLine("Created transaction1.")
            Dim tx as Object = tx1.Transaction
            Console.WriteLine("Got the transaction of transaction1.")       
            Dim tx2 as CTransaction2 = ctype(BYOT.CreateWithTransaction(tx, _ gettype(CTransaction2)),CTransaction2)
            Console.WriteLine("Created transaction2 with the _
            transaction of transaction1.")
            End Sub
End Class
[C#]
using System;
using System.Reflection;
using System.EnterpriseServices;

[assembly: AssemblyKeyFileAttribute("byotdemo.snk")]

public class Base : ServicedComponent
{
    public Object Transaction 
    { 
        get { return ContextUtil.Transaction; } 
    }    
}

[Transaction]
public class CTransaction1 : Base 
{
      // Insert your transaction logic here.
}

[Transaction]
public class CTransaction2 : Base 
{
      // Insert your transaction logic here.
}

class BYOTDemonstration
{
      static void Main()
      {
            /* Create a transactional object, and then get its
                  transaction. */
              CTransaction1 tx1 = new CTransaction1();
            Console.WriteLine("Created transaction1.");
              Object tx = tx1.Transaction;
            Console.WriteLine("Got the transaction of transaction1.");        
              CTransaction2 tx2 = (CTransaction2)BYOT.CreateWithTransaction(tx, typeof(CTransaction2));
            Console.WriteLine("Created transaction2 using the transaction of transaction1.");
      }
}

Makefile.bat

You can compile the example as follows:

sn –k BYOTDemo.snk
vbc /r:System.EnterpriseServices.dll BYOTDemo.vb
[C#]
sn –k BYOTDemo.snk
csc BYOTDemo.cs

See Also

Summary of Available COM+ Services | System.EnterpriseServices Namespace