VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 16

Using Transactions

If a component uses a Resource Dispenser that supports transactions, such as the ODBC Resource Dispenser, the transactional features of MTS can be exploited. MTS automates the details of implementing transactions. Essentially, transactions are initiated by the runtime environment rather than through code. MTS components can be configured to support transactions.

Setting Transaction Properties of Components

Transaction properties can be set for an MTS component in two ways: through either the MTS Explorer, or from the Visual Basic 6.0 development environment. The MTS Explorer provides the following four different settings for a component to have transaction support:

  • Requires a transaction. If this setting is selected, the component must always participate in a transaction. If the client already has a transaction associated with it, the object context will inherit the pre-existing transaction. Otherwise, MTS will begin a new transaction when the object is instantiated by the client.

  • Requires a new transaction. This setting causes an MTS to begin a new transaction every time an object is created, regardless of whether the client already has a transaction. This option ensures that an object always has its own transaction associated with it.

  • Supports transactions. Objects created from a component with this setting will be more flexible in that they might inherit a transaction or they might execute without a transaction. This is determined by the state of the client at instantiation time of the object. If the client already has a transaction in process, the object will use the transaction. If no transaction is present in the client’s context, the object will not use a transaction either.

  • Does not support transactions. The object will never use transactions in its execution. Even if the client has a transaction, the object will still run outside the context of the transaction.

To use MTS Explorer to set the level of transaction support for a component, execute the following steps:

16.4 Setting the Level of Transaction Support for a Component

  1. From the MTS Explorer, double-click the computer that contains the component for which you want to set transaction support properties.

  2. Double-click the package that contains the component.

  3. Double-click the Components folder and select the appropriate component.

  4. Right-click on the component and choose Properties.

  5. Click on the Transaction tab (see Figure 16.6).

    The Transaction Support option can be set through the component’s Properties dialog box in MTS Explorer
    FIGURE 16.6 The Transaction Support option can be set through the component’s Properties dialog box in MTS Explorer

  6. Set one of the options in the Transaction Support frame.

  7. Click OK.

Alternatively, transaction properties can be set from within the Visual Basic development environment. VB6 exposes the MTSTransactionMode property for all classes in an ActiveX DLL project. Because MTS components are always ActiveX DLLs, the MTSTransactionMode property is not available in any other type of project. After a component has been compiled with the appropriate MTSTransactionMode property value, it is no longer necessary to address the transaction properties from the MTS Explorer. It is also worth noting that the component will ignore the MTSTransactionMode property if it is not run in MTS. The possible values for the MTSTransactionMode are as follows:

  • NotAnMTSObject This setting is for components that will not run in MTS.

  • NoTransactions This setting is equivalent to the MTS Explorer setting of Does Not Support Transactions.

  • RequiresTransaction This setting is equivalent to the MTS Explorer setting of Requires a Transaction.

  • UsesTransaction This setting is equivalent to the MTS Explorer setting of Supports Transactions.

  • RequiresNewTransaction This setting is equivalent to the MTS Explorer setting of Requires a New Transaction.

After the transaction support for a component has been set, everything will be in place for a transaction to be started. Depending on the option selected, the transaction will be initiated when the object is instantiated, or it will be instantiated without a transaction. Committing or canceling a transaction is another matter, however. MTS provides the means to these actions through an object’s Context object. The Context object supports transaction features through two methods: SetComplete and SetAbort.

If the SetComplete method of an object’s context is called from within the component, a couple of things will happen:

  • The transaction initiated by the object will be committed.

  • The object will release all its current resources, including memory used for properties and variables.

If SetAbort is called, the transaction will be rolled back and resources will be released. It is precisely the use of these methods that turns a regular COM object into a stateless MTS-specific object.

It can easily be seen that calls to these methods must be carefully placed. Generally, SetComplete is called at the end of a method that performs some action on a database, and the action executes successfully. SetAbort might be called if the method fails for any reason.

The following code shows an example of a simple object method that uses SetComplete and SetAbort:

Public Name As String
Public Sub AddName()
Dim oContext As ObjectContext
On Error GoTo ErrorHandler
Set oContext = GetObjectContext()
‘<code to insert name into database>
Set oContext = Nothing
Exit Sub
‘<code to notify client of error>
Set oContext = Nothing
End Sub

In the preceding sample code, the class exposes a single property called Name. When the AddName method is called, the object will attempt to add the name to the database. If all the code in the AddName method executes successfully, the object calls SetComplete, which will instruct MTS to commit the transaction and release the resources associated with this object. If for any reason an error occurs, the error handler will notify the caller of the error and call SetAbort. The MTS runtime environment will roll back the transaction and release the object’s resources.


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved