Creating the MoveMoney Component

To implement this scenario, you will add a new class module, MoveMoney, to the Account project. MoveMoney has a single method, Perform, which creates an Account object to perform the credit, debit, or transfer.

To create the MoveMoney component
  1. Open the \MTx\Samples\Account.VB\Step4\Account.vbp project.

Click here to see the Perform method

  1. Build the component as a dynamic-link library (DLL) and save it as \MTx\Samples\Account.VB\Step4\VBAcct.dll.

By adding a new class module, you have added a new COM component to this DLL. Therefore, you will need to delete the Account component in the Microsoft Transaction Server Explorer and then install the Account and the MoveMoney components.

To reinstall your components
  1. Remove the Account and CreateTable components.

How?

  1. Add the new components.

How?

Use the DLL you created in the previous procedure. You can find it in \MTx\Samples\Account.VB\Step4\VBAcct.dll.

MTS enlists a component in a transaction as specified by the component's transaction attribute. For this scenario, Account and MoveMoney run within the same transaction.

To set the transaction attributes for your components
  1. For the Account and MoveMoney components, set the transaction attribute to Requires a transaction.
  2. For the CreateTable component, set the transaction attribute to Requires a new transaction.

How?

The MoveMoney object uses CreateInstance to create the Account object. CreateInstance is a method on the context object. By using CreateInstance, the Account object created by MoveMoney shares context with MoveMoney.

Dim myContext As ObjectContext
Set ctxObject = GetObjectContext()

Dim objAccount As Bank.Account
Set objAccount = _ 
    myContext.CreateInstance("Bank.Account")

Transactions are associated with an object's context. Because both MoveMoney and Account have a transaction attribute of Requires a transaction, the Account object will be enlisted within the same transaction as MoveMoney.

In Building Scalable Components, you learned how to use SetComplete to indicate that an object has finished its work and can be deactivated. For transactional components, calling SetComplete indicates that a transaction can be committed.

ctxObject.SetComplete

When the Perform method returns, the transaction attempts to commit. There is no guarantee that it will commit, however. If an error occurs, Perform instead calls SetAbort.

ctxObject.SetAbort

SetAbort also indicates that an object has finished its work, but that it isn't in a consistent state. When the Perform method returns after calling SetAbort, the attempt to commit the transaction won't succeed.

See Also

Transactions, Transaction Attributes, Context Objects, Creating MTS Objects, ObjectContext object, CreateInstance method, GetObjectContext method, SetAbort method, SetComplete method