COM Transactions

In Visual Basic 6.0, you can use the MTSTransactionMode property to set the trans-actional behavior of a user class. This property is only used by components that are running in the Microsoft Transaction Server, and has no effect if the component is run outside of the MTS.

In Visual Basic .NET, developers can use System.EnterpriseServices namespace to get transactional functionality for their .NET classes. To be treated as a transactional class, a Visual Basic .NET class should inherit from ServicedComponent and include the proper transaction attribute. Transactional Visual Basic 6.0 classes should be upgraded to serviced components.

The MTSTransactionMode values correspond to the

System.EnterpriseServices.TransactionOption .NET Framework enumeration that contains the automatic transaction type requested by a COM+ component. The MTSTransactionMode property can be set to any of the constant values specified in Table 15.2, which presents the equivalences between both groups of values.

Table 15.2: MTSTransactionMode Value Equivalents in the System.EnterpriseServices.TransactionOption Namespace

MTSTransactionMode value

System.EnterpriseServices.TransactionOption equivalent

NotAnMTSObject (0)

Disabled

NoTransactions (1)

NotSupported

RequiresTransaction (2)

Required

UsesTransaction (3)

Supported

RequiresNewTransaction (4)

RequiresNew

Use the following information to create a Visual Basic .NET project that references transactional classes:

• Have transactional classes inherit from ServicedComponent.

• Include transactional attributes in transactional classes.

• Eliminate calls to GetObjectContext; instead use the ContextUtil class.

The Visual Basic 6.0 MTSTransactionMode attribute controls whether transactions are required for a class. This attribute should be mapped to the corresponding value from System.EnterpriseServices.TransactionOption.

All COM+ transaction related classes are supported by Visual Basic 6.0, and almost every class has an equivalent class in the .NET Framework. The only exception is the ITransaction interface, which does not have a semantic equivalent in Visual Basic .NET.

Adding transaction support to the simple COM+ server component as described in the "Using MTS/COM+ in Visual Basic 6.0" section earlier in this chapter results in the following Visual Basic 6.0 code:

Notice that the MTSTransactionMode property value is set to RequiresTransaction and that the name of the Server is COMTest.

Private Sub transferfunds(acc1 As String, acc2 As String, _ amount As Long) On Error GoTo ErrHandler withdrawFunds acc1, amount depositFunds acc2, amount GetObjectContext.SetComplete

Exit Sub ErrHandler:

GetObjectContext.SetAbort

Err.Raise Err.Number, Err.Source, Err.Description, _ Err.HelpFile, Err.HelpContext

End Sub

Private Sub withdrawFunds(acc As String, amount As Long)

' Perform withdrawal... End Sub

Private Sub depositFunds(acc As String, amount As Long)

' Perform deposit... End Sub

Upgrading this server component with the upgrade wizard results in the following Visual Basic .NET code.

Option Strict Off Option Explicit On

<System.Runtime.InteropServices.ProgId("COMTest_NET.COMTest")> _ Public Class COMTest

Public Sub transferfunds(ByRef acc1 As String, _ ByRef acc2 As String, ByRef amount As Integer) On Error GoTo ErrHandler withdrawFunds(acc1, amount) depositFunds(acc2, amount)

' UPGRADE_WARNING: Could not resolve default property of ' object GetObjectContext.SetComplete. GetObjectContext.SetCompleteO

Exit Sub ErrHandler:

' UPGRADE_WARNING: Could not resolve default property of ' object GetObjectContext.SetAbort. GetObjectContext.SetAbort()

Err.Raise(Err.Number, Err.Source, Err.Description, _ Err.HelpFile, Err.HelpContext)

End Sub

Private Sub withdrawFunds(ByRef acc As String, _ ByRef amount As Integer) ' Perform withdrawal... End Sub

Private Sub depositFunds(ByRef acc As String, _ ByRef amount As Integer) ' Perform deposit... End Sub End Class

0 0

Post a comment