Providing support for transactions

If a component supports transactions, COM+ ensures that the component’s database operations execute as part of a transaction.

Using the transaction service context object

PowerBuilder components running in COM+ can use a transaction context service to control transactions. A PowerBuilder COM object creates a context object that enables the component to take advantage of Microsoft DTC support. The TransactionServer object enables a COM object running in COM+ to access the context associated with the object, giving it tighter control of transactions and activation. It also provides some control of the security context.

For more information about the TransactionServer object and its methods, see Objects and Controls and the PowerScript Reference.

Before you can use the transaction context service, you need to declare a variable of type TransactionServer and call the GetContextService function to create an instance of the service:

TransactionServer txninfo_base
this.GetContextService("TransactionServer", &
    txninfo_base)

To access information about the component’s transaction context and control the transaction, call methods on the instance of the TransactionServer object—for example:

IF txninfo_base.IsInTransaction() THEN
    txninfo_base.DisableCommit()    
END IF
...
txninfo_base.SetComplete()

When you no longer need the reference to the service, you should destroy it:

DESTROY txninfo_base

Behavior of COMMIT and ROLLBACK

When a PowerBuilder component is running in COM+, the TransactionServer interface is used to control transactions when the UseContextObject DBParm parameter is set to Yes. When the UseContextObject DBParm parameter is set to Yes, COMMIT and ROLLBACK statements result in a database error. The transaction remains active until SetComplete or SetAbort is issued using an instance of the TransactionServer context object.

NoteMigrating PowerBuilder 6 components Components built with PowerBuilder 6 relied on the PowerBuilder database driver to issue SetComplete or SetAbort calls to COM+. If you are migrating these components to the current version of PowerBuilder, modify your code to use the TransactionServer interface. Alternatively, you can set the UseContextObject DBParm to No. In this case, COMMIT is equivalent to SetComplete and ROLLBACK is equivalent to SetAbort. This approach is recommended only when you want to migrate PowerBuilder 6 objects without modifying the code.

Specifying whether a component supports transactions

Each COM+ component has a transaction property that indicates how the component participates in transactions. PowerBuilder COM objects create a new context object when the component transaction property in COM+ is set to Requires a new transaction. A PowerBuilder COM object whose component transaction property is set to Requires a transaction or Supports transactions either inherits a transaction from an existing object or creates a new transaction.

You set this property in the COM/COM+ Project wizard or the Project painter. Table 27-4 describes the values you can set.

Table 27-4: Component transaction property values

Transaction type

Description

Not supported

The component never executes as part of a transaction. If the component is activated by another component that is executing within a transaction, the new instance’s work is performed outside the existing transaction.

Supports Transaction

The component can execute in the context of a COM+ transaction, but a connection is not required to execute the component’s methods. If the component is instantiated directly by a client, COM+ does not begin a transaction. If component A is instantiated by component B and component B is executing within a transaction, component A executes in the same transaction.

Requires Transaction

The component always executes in a transaction. When the component is instantiated directly by a client, a new transaction begins. If component A is activated by component B, and B is executing within a transaction, A executes within the same transaction; if B is not executing in a transaction, A executes in a new transaction.

Requires New Transaction

Whenever the component is instantiated, a new transaction begins. If component A is activated by component B, and B is executing within a transaction, then A begins a new transaction that is unaffected by the outcome of B’s transaction; if B is not executing in a transaction, A executes in a new transaction.

For more information about how transactions work, see the Microsoft COM+ documentation in the MSDN Library.