Creating the Receipt Component

The Receipt component contains a single method, GetNextReceipt. The Receipt object itself doesn't maintain the value of the receipt number between calls. The Shared Property Manager maintains these values. The Receipt object calls a SharedProperty object to get a new receipt number.

You will also add code to the MoveMoney component to call the Receipt component.

To create the Receipt Component
  1. Start Microsoft Visual Basic and open the \MTx\Samples\Account.VB\Step5\Account.vbp project.

Click here to see the code for the Receipt component

Click here to see the code for the MoveMoney component

  1. Build the component as a DLL and save it as \MTx\Samples\Account.VB\Step5\VBAcct.dll.

By adding a new class module, you add a new COM component to this DLL. Therefore, you need to delete the existing components in the Microsoft Transaction Server Explorer and then install the new components.

To reinstall your components
  1. Remove the Account, MoveMoney, and CreateTable components from the Transaction Server Explorer.

How?

  1. Add the new components. Use the DLL you created in \MTx\Samples\Account.VB\Step5\VBAcct.dll.

How?

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

How?

  1. For the CreateTable component, set the transaction attribute to Requires a new transaction.
  2. For the Receipt component, set the transaction attribute to Does not support transactions. This is the default value.

Note that the Receipt component is not transactional because the receipts are maintained as properties in memory and aren't durable.

When you run the Bank Client, select the MoveMoney button under Component. You should see the response Credit, balance is $ 1. (VB); Receipt No: #####.

The various object creation methods for Shared Property Manager objects are designed for simplified coding. If the object doesn't exist, it will be created. If it already exists, the object is returned. GetNextReceipt makes the following method call to access the shared property group manager:

Set spmMgr = CreateObject _
    ("MTxSpm.SharedPropertyGroupManager.1")

This code works every time it is called. There is no need to check if the shared property group manager has already been created. Such behavior also ensures that only one instance of the SharedPropertyGroupManager object exists per server process.

For the SharedPropertyGroup and SharedProperty objects, a flag is returned to indicate whether the property group or property already exists. The following code shows how this flag is used to determine if the property needs to be initialized:

' Create the SharedProperty. 
Set spmPropNextReceipt = _
    spmGroup.CreateProperty("Next", bResult)

' Set the initial value of the SharedProperty to
' 0 if the SharedProperty didn't already exist.
If bResult = False Then
    spmPropNextReceipt.Value = 0
End If

Access to shared properties is controlled through the CreatePropertyGroup method:

Set spmGroup = _
    spmMgr.CreatePropertyGroup("Receipt", _
    LockSetGet, Process, bResult)

CreatePropertyGroup has two parameters, isolation mode and release mode. The isolation mode for the Receipt property group is set to LockSetGet, which ensures that two instances of the Receipt object can't read or write to the same property at the same time. The release mode for the Receipt property group is set to Process, which maintains the property group until the server process is terminated.

See Also

Application Design Notes: Sharing State by Using the Shared Property Manager, SharedPropertyGroupManager object, CreateProperty methodasmthCreatePropertyvb, CreatePropertyGroup methodasmthCreatePropertyGroupvb