Export (0) Print
Expand All

Stateful Components

Archived content. No warranty is made as to technical accuracy. Content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

This section discusses stateful components and outlines some of the issues associated with writing stateful application components.

 

viicsc

Scenario: Holding State in the MoveMoney Component
Consider the design alternative of holding state within objects.

 

viic01

Adding a New Method to the MoveMoney Component
Add the StatefulPerform method, which uses MoveMoney to maintain account number values.

 

viicno

Application Design Notes: The Trade-offs of Using Stateful Objects
Learn how holding state in objects affects the application behavior within the Microsoft Transaction Server run-time environment.

Scenario: Holding State in the MoveMoney Component

Building stateful objects is a useful approach in application design. However, such design can have performance trade-offs. This section demonstrates how holding state in objects affects the application behavior within the Microsoft Transaction Server run-time environment.

You will modify the MoveMoney component to be stateful by adding the StatefulPerform function to MoveMoney. StatefulPerform is called when you click Stateful MoveMoney on the Sample Bank client. This new function causes MoveMoney to retain data in member variables between method calls.

vi0901

Adding a New Method to the MoveMoney Component

To implement the scenario for this section, you will add a method similar to Perform, named StatefulPerform, which uses class member variables to set account numbers. Thus, MoveMoney becomes a stateful object when StatefulPerform is called.

To add a new function to the MoveMoney component

  1. Open the \Mts\Samples\Account.VB\Step6\Account.vbp project.

  2. Build the component as a dynamic-link library (DLL) and save it as \Mts\Samples\Account.VB\Step6\VBAcct.dll.

The code for StatefulPerform calls the Perform method. The methods differ in how the account numbers are set. Class member variables for each account must be set before calling StatefulPerform, whereas Perform passes the account numbers by value through function parameters.

When you click the MoveMoney option in the Sample Bank client, it calls the following code to initialize the function:

StatefulPerform = Perform(PrimeAccount, SecondAccount, lngAmount, lngTranType)

When you click the Stateful MoveMoney option, the Sample Bank client calls the following code to initialize the function:

obj.PrimeAccount = PrimeAcct
obj.SecondAccount = lSecondAcct
Res = obj.StatefulPerform(CLng(Amount), TranType)

The PrimeAccount and SecondAccount properties are actually separate class member variables on the MoveMoney object. Note that the PrimeAccount and SecondAccount properties aren't accessed through the Shared Property Manager properties; the MoveMoney object controls getting and setting the account number values, thus making the MoveMoney object stateful.

Run the Bank client with the MoveMoney option. Then run it again with the Stateful MoveMoney option. You should notice that the stateless version is slightly faster. Try running multiple Bank clients with concurrent transactions. You should notice that the stateless version performs significantly better. The next section explains why.

Application Design Notes: The Trade-offs of Using Stateful Objects

This section explains the trade-offs of using stateful objects in your applications.

Why does MoveMoney outperform Stateful MoveMoney?

In the previous section, you saw that the time per transaction in MoveMoney and Stateful MoveMoney using a single Sample Bank client is nearly the same. However, as the number of concurrent transactions increases, MoveMoney begins to outperform Stateful MoveMoney significantly . At first glance, the code doesn't seem to account for the lag.

Class member variables for each account must be set before calling StatefulPerform, whereas Perform passes the account numbers by value through function parameters. The call to return the value of the account number in the MoveMoney object isn't an intensive operation. So what explains the performance degradation?

The reason is that Microsoft Transaction Server cannot commit transactions until it completes a method call. To maintain internal state, additional method calls are made on the MoveMoney object, thereby delaying the object from completing its work. This delay may cause server resources, such as database connections, to be held longer, therefore decreasing the amount of resources available for other clients. In other words, the application won't scale well.

The following diagram illustrates this point. The arrow on the left indicates time, which translates into performance. The arrow on the right indicates the server resources consumed, which translates into throughput. Transaction A represents a call made to stateless objects. On return from the method call, Transaction Server determines that the transaction can be committed, allowing the object to release its resources and be deactivated. On the other hand, Transaction B holds state between method calls, which increases the time that the server holds onto resources for that transaction. As the number of clients increases, so does the time required for transactions to be completed.

Cc723276.vi0902(en-us,TechNet.10).gif

Another Pitfall When Using Stateful Objects

Examine the following excerpt from the Sample Bank client code (some code has been omitted for clarity).

For i = 1 To nTrans
     .
     .
     .
     obj.PrimeAccount = PrimeAcct
     obj.SecondAccount = lSecondAcct
     Res = obj.StatefulPerform(CLng(Amount), TranType
     .
     .
     .
Next i

Because the account numbers don't change, you might be inclined to rearrange the code as follows:

obj.PrimeAccount = PrimeAcct
obj.SecondAccount = lSecondAcct
For i = 1 To nTrans
     Res = obj.StatefulPerform(CLng(Amount), TranType
Next i

If you modify the code and then run the Sample Bank client for multiple transactions, the application fails on the second transaction. Why?

The answer is subtle. MoveMoney uses SetComplete to notify Transaction Server that it has completed its work. At this point, the MoveMoney object is deactivated. In the process of deactivation, all of the object's member variables are reinitialized. The next call to MoveMoney causes just-in-time activation. The activated object is now in its initial state, meaning the values of PrimeAccountNo and SecondAccountNo are both zero. Thus, the next call to StatefulPerform fails because of an invalid account number.

This is yet another reason to be careful when maintaining state in objects. Clients of application objects must be aware of how an object uses SetComplete to ensure that any state the object maintains won't be needed after the object undergoes just-in-time activation.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft