Every Transaction Server object has a context object associated with it. The context object is automatically created at the same time the object itself is created. You can use an object's context to declare when the object's work is complete, as shown in the following illustration.
Calling either of these methods notifies the MTS run-time environment that it can safely deactivate the object, making its resources available for reuse.
To implement the scenario for this chapter, you will modify the Post method to obtain a reference to the Account object's context object. Then you will use SetComplete and SetAbort to enable just-in-time activation.
First, you call GetObjectContext to get a reference to the context object.
Dim ctxObject As ObjectContext
Set ctxObject = GetObjectContext()
When an object has completed its work successfully, it should call SetComplete:
ctxObject.SetComplete
SetComplete notifies the MTS run-time environment that the Account object should be deactivated as soon as it returns control to the Bank client.
If the object encountered an error, it should call SetAbort. SetAbort also notifies the MTS run-time environment that the Account object should be deactivated as soon as it returns control to the Bank client.
ctxObject.SetAbort
Click here to see the Post method
Before you can run your new component again in MTS, the registry needs to be updated with the new component information. To do this, refresh the MTS Explorer window.
If you install the Development version of Microsoft Transaction Server, you will get a Visual Basic – compatible add-in that automates this process for you (select the VB Addin box during Setup). The next time you run Visual Basic, the add-in is automatically installed in Visual Basic. The add-in automatically refreshes all of your MTS component DLLs whenever you recompile your project.
You can also turn this feature on and off on a per-project basis by using the toggle command on the Visual Basic Add-Ins menu. To turn it on, on the Visual Basic Add-Ins menu, point to MS Transaction Server, and click AutoRefresh after compile of active project. This puts a check mark next to the command, indicating that the feature is activated. If you want to refresh all of your MTS components at any given time, on the Visual Basic Add-Ins menu, point to MS Transaction Server, and then click Refresh all components now.
Now you'll run the Account component again from the Bank client, and monitor its execution in the MTS Explorer's Status window. Follow the same steps as in "Running and Monitoring the Account Component."
When the Bank client creates the Account object, the number 1 will appear under Objects and Activated. This indicates that one object is executing in the MTS run-time environment, and that it is currently activated. When the client calls the Post method, the number 1 appears, briefly, under In Call. This indicates that one object is currently executing a method call. When the Post method returns control to the client, the number under Objects is still 1, but the numbers under Activated and In Call return to 0. This is because after calling SetComplete, the object is deactivated as soon as it returns from the current method call.
Note Because the Post method executes so quickly, you may not actually see this sequence appear.
See Also
Context Objects, Deactivating Objects, Creating a Simple ActiveX Component, GetObjectContext method, SetAbort method, SetComplete method